0
0
mirror of https://github.com/thegeeklab/wp-s3-action.git synced 2024-11-22 01:00:40 +00:00

Refactor main for testing and organization

This commit is contained in:
Eric Anderson 2016-03-10 00:54:22 -05:00
parent 94c7d6b998
commit 8b8f1248e6

104
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -23,25 +24,72 @@ type result struct {
err error err error
} }
type app struct {
vargs *PluginArgs
workspace *drone.Workspace
client AWS
jobs []job
}
var ( var (
buildCommit string buildCommit string
) )
var MissingAwsValuesMessage = "Must set access_key, secret_key, and bucket"
func main() { func main() {
fmt.Printf("Drone S3 Sync Plugin built from %s\n", buildCommit) fmt.Printf("Drone S3 Sync Plugin built from %s\n", buildCommit)
vargs := PluginArgs{} a := newApp()
workspace := drone.Workspace{}
plugin.Param("vargs", &vargs) err := a.loadVargs()
plugin.Param("workspace", &workspace) if err != nil {
if err := plugin.Parse(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
if len(vargs.Key) == 0 || len(vargs.Secret) == 0 || len(vargs.Bucket) == 0 { err = a.sanitizeInputs()
return if err != nil {
fmt.Println(err)
os.Exit(1)
}
a.createClient()
a.createSyncJobs()
a.createInvalidateJob()
a.runJobs()
fmt.Println("done!")
}
func newApp() *app {
return &app{
vargs: &PluginArgs{},
workspace: &drone.Workspace{},
jobs: make([]job, 1, 1),
}
}
func (a *app) loadVargs() error {
plugin.Param("vargs", a.vargs)
plugin.Param("workspace", a.workspace)
err := plugin.Parse()
return err
}
func (a *app) createClient() {
a.client = NewAWS(*a.vargs)
}
func (a *app) sanitizeInputs() error {
vargs := a.vargs
workspace := a.workspace
if len(a.vargs.Key) == 0 || len(a.vargs.Secret) == 0 || len(a.vargs.Bucket) == 0 {
return errors.New(MissingAwsValuesMessage)
} }
if len(vargs.Region) == 0 { if len(vargs.Region) == 0 {
@ -57,15 +105,19 @@ func main() {
vargs.Target = vargs.Target[1:] vargs.Target = vargs.Target[1:]
} }
client := NewAWS(vargs) return nil
remote, err := client.List(vargs.Target) }
func (a *app) createSyncJobs() {
vargs := a.vargs
remote, err := a.client.List(vargs.Target)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
local := make([]string, 1, 1) local := make([]string, 1, 1)
jobs := make([]job, 1, 1)
err = filepath.Walk(vargs.Source, func(path string, info os.FileInfo, err error) error { err = filepath.Walk(vargs.Source, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() { if err != nil || info.IsDir() {
return err return err
@ -79,7 +131,7 @@ func main() {
} }
} }
local = append(local, localPath) local = append(local, localPath)
jobs = append(jobs, job{ a.jobs = append(a.jobs, job{
local: filepath.Join(vargs.Source, localPath), local: filepath.Join(vargs.Source, localPath),
remote: filepath.Join(vargs.Target, localPath), remote: filepath.Join(vargs.Target, localPath),
action: "upload", action: "upload",
@ -95,13 +147,13 @@ func main() {
for path, location := range vargs.Redirects { for path, location := range vargs.Redirects {
path = strings.TrimPrefix(path, "/") path = strings.TrimPrefix(path, "/")
local = append(local, path) local = append(local, path)
jobs = append(jobs, job{ a.jobs = append(a.jobs, job{
local: path, local: path,
remote: location, remote: location,
action: "redirect", action: "redirect",
}) })
} }
if (a.vargs.Delete) {
for _, r := range remote { for _, r := range remote {
found := false found := false
for _, l := range local { for _, l := range local {
@ -112,34 +164,42 @@ func main() {
} }
if !found { if !found {
jobs = append(jobs, job{ a.jobs = append(a.jobs, job{
local: "", local: "",
remote: r, remote: r,
action: "delete", action: "delete",
}) })
} }
} }
}
}
if len(vargs.CloudFrontDistribution) > 0 { func (a *app) createInvalidateJob() {
jobs = append(jobs, job{ if len(a.vargs.CloudFrontDistribution) > 0 {
a.jobs = append(a.jobs, job{
local: "", local: "",
remote: filepath.Join("/", vargs.Target, "*"), remote: filepath.Join("/", a.vargs.Target, "*"),
action: "invalidateCloudFront", action: "invalidateCloudFront",
}) })
} }
}
func (a *app) runJobs() {
vargs := a.vargs
client := a.client
jobChan := make(chan struct{}, maxConcurrent) jobChan := make(chan struct{}, maxConcurrent)
results := make(chan *result, len(jobs)) results := make(chan *result, len(a.jobs))
fmt.Printf("Synchronizing with bucket \"%s\"\n", vargs.Bucket) fmt.Printf("Synchronizing with bucket \"%s\"\n", vargs.Bucket)
for _, j := range jobs { for _, j := range a.jobs {
jobChan <- struct{}{} jobChan <- struct{}{}
go func(j job) { go func(j job) {
var err error
if j.action == "upload" { if j.action == "upload" {
err = client.Upload(j.local, j.remote) err = client.Upload(j.local, j.remote)
} else if j.action == "redirect" { } else if j.action == "redirect" {
err = client.Redirect(j.local, j.remote) err = client.Redirect(j.local, j.remote)
} else if j.action == "delete" && vargs.Delete { } else if j.action == "delete" {
err = client.Delete(j.remote) err = client.Delete(j.remote)
} else if j.action == "invalidateCloudFront" { } else if j.action == "invalidateCloudFront" {
client.Invalidate(j.remote) client.Invalidate(j.remote)
@ -151,15 +211,13 @@ func main() {
}(j) }(j)
} }
for _ = range jobs { for _ = range a.jobs {
r := <-results r := <-results
if r.err != nil { if r.err != nil {
fmt.Printf("ERROR: failed to %s %s to %s: %+v\n", r.j.action, r.j.local, r.j.remote, r.err) fmt.Printf("ERROR: failed to %s %s to %s: %+v\n", r.j.action, r.j.local, r.j.remote, r.err)
os.Exit(1) os.Exit(1)
} }
} }
fmt.Println("done!")
} }
func debug(format string, args ...interface{}) { func debug(format string, args ...interface{}) {