-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
202 lines (178 loc) · 4.23 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
type Plugin struct {
Endpoint string
Key string
Secret string
Bucket string
Region string
Source string
Target string
Delete bool
Access map[string]string
CacheControl map[string]string
ContentType map[string]string
ContentEncoding map[string]string
Metadata map[string]map[string]string
Redirects map[string]string
CloudFrontDistribution string
DryRun bool
PathStyle bool
client AWS
jobs []job
MaxConcurrency int
}
type job struct {
local string
remote string
paths []string
action string
}
type result struct {
j job
err error
}
var MissingAwsValuesMessage = "Must set 'bucket'"
func (p *Plugin) Exec() error {
err := p.sanitizeInputs()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
p.jobs = make([]job, 0)
p.client = NewAWS(p)
p.createSyncJobs()
p.createInvalidateJob()
p.runJobs()
return nil
}
func (p *Plugin) sanitizeInputs() error {
if len(p.Bucket) == 0 {
return errors.New(MissingAwsValuesMessage)
}
wd, err := os.Getwd()
if err != nil {
return err
}
p.Source = filepath.Join(wd, p.Source)
p.Target = strings.TrimPrefix(p.Target, string(os.PathSeparator))
return nil
}
func (p *Plugin) createSyncJobs() {
remote, err := p.client.List(p.Target)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
local := make([]string, 0)
err = filepath.Walk(p.Source, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
localPath := path
if p.Source != "." {
localPath = strings.TrimPrefix(path, p.Source)
localPath = strings.TrimPrefix(localPath, string(os.PathSeparator))
}
local = append(local, localPath)
p.jobs = append(p.jobs, job{
local: filepath.Join(p.Source, localPath),
remote: filepath.Join(p.Target, localPath),
action: "upload",
})
return nil
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for path, location := range p.Redirects {
path = strings.TrimPrefix(path, string(os.PathSeparator))
local = append(local, path)
p.jobs = append(p.jobs, job{
local: path,
remote: location,
action: "redirect",
})
}
if p.Delete {
for _, r := range remote {
found := false
matcher := strings.TrimPrefix(r, p.Target)
matcher = strings.TrimPrefix(matcher, string(os.PathSeparator))
for _, l := range local {
if l == matcher {
found = true
break
}
}
if !found {
p.jobs = append(p.jobs, job{
local: "",
remote: r,
action: "delete",
})
}
}
}
}
func (p *Plugin) createInvalidateJob() {
if len(p.CloudFrontDistribution) > 0 {
p.jobs = append(p.jobs, job{
paths: []string{filepath.Join(string(os.PathSeparator), p.Target, "*")},
action: "invalidateCloudFront",
})
}
}
func (p *Plugin) runJobs() {
client := p.client
jobChan := make(chan struct{}, p.MaxConcurrency)
results := make(chan *result, len(p.jobs))
var invalidateJob *job
fmt.Printf("Synchronizing with bucket \"%s\"\n", p.Bucket)
for _, j := range p.jobs {
jobChan <- struct{}{}
go func(j job) {
var err error
if j.action == "upload" {
err = client.Upload(j.local, j.remote)
} else if j.action == "redirect" {
err = client.Redirect(j.local, j.remote)
} else if j.action == "delete" {
err = client.Delete(j.remote)
} else if j.action == "invalidateCloudFront" {
invalidateJob = &j
} else {
err = nil
}
results <- &result{j, err}
<-jobChan
}(j)
}
for range p.jobs {
r := <-results
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)
os.Exit(1)
}
}
if invalidateJob != nil {
fmt.Printf("Invalidating CloudFront distribution\n")
err := client.Invalidate(invalidateJob.paths)
if err != nil {
fmt.Printf("ERROR: failed to %s %s: %+v\n", invalidateJob.action, invalidateJob.paths, err)
os.Exit(1)
}
}
}
func debug(format string, args ...interface{}) {
if os.Getenv("DEBUG") != "" {
fmt.Printf(format+"\n", args...)
}
}