-
Notifications
You must be signed in to change notification settings - Fork 366
/
command_arbiter.go
192 lines (166 loc) · 4.85 KB
/
command_arbiter.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
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/ryanuber/go-glob"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb"
"golang.org/x/sync/errgroup"
"github.com/Shopify/themekit/kit"
)
type (
commandArbiter struct {
progress *mpb.Progress
verbose bool
force bool
configPath string
allenvs bool
environments stringArgArray
notifyFile string
flagConfig kit.Configuration
disableIgnore bool
ignoredFiles stringArgArray
ignores stringArgArray
activeThemeClients []kit.ThemeClient
manifest *fileManifest
}
cobraCmdE func(*cobra.Command, []string) error
arbitratedCmd func(kit.ThemeClient, []string) error
assetAction struct {
asset kit.Asset
event kit.EventType
}
)
func newCommandArbiter() *commandArbiter {
pwd, _ := os.Getwd()
return &commandArbiter{
progress: mpb.New(nil),
configPath: filepath.Join(pwd, "config.yml"),
flagConfig: kit.Configuration{},
}
}
func (arbiter *commandArbiter) generateManifest() error {
var err error
arbiter.manifest, err = newFileManifest(filepath.Dir(arbiter.configPath), arbiter.activeThemeClients)
return err
}
func (arbiter *commandArbiter) generateThemeClients(cmd *cobra.Command, args []string) error {
arbiter.activeThemeClients = []kit.ThemeClient{}
configEnvs, err := kit.LoadEnvironments(arbiter.configPath)
if err != nil && os.IsNotExist(err) {
return fmt.Errorf("Could not find config file at %v", arbiter.configPath)
} else if err != nil {
return err
}
for env := range configEnvs {
if !arbiter.shouldUseEnvironment(env) {
continue
}
config, err := configEnvs.GetConfiguration(env)
if err != nil {
continue
}
if arbiter.disableIgnore {
config.IgnoredFiles = []string{}
config.Ignores = []string{}
}
if config.Proxy != "" {
stdOut.Printf(
"[%s] Proxy URL detected from Configuration: %s SSL Certificate Validation will be disabled!",
green(config.Environment),
yellow(config.Proxy),
)
}
client, err := kit.NewThemeClient(config)
if err != nil {
return err
}
arbiter.activeThemeClients = append(arbiter.activeThemeClients, client)
}
if len(arbiter.activeThemeClients) == 0 {
return fmt.Errorf("Could not load any valid environments")
}
return arbiter.generateManifest()
}
func (arbiter *commandArbiter) shouldUseEnvironment(envName string) bool {
flagEnvs := arbiter.environments.Value()
if arbiter.allenvs || (len(flagEnvs) == 0 && envName == kit.DefaultEnvironment) {
return true
}
for _, env := range flagEnvs {
if env == envName || glob.Glob(env, envName) {
return true
}
}
return false
}
func (arbiter *commandArbiter) forEachClient(handler arbitratedCmd) cobraCmdE {
return func(cmd *cobra.Command, args []string) error {
var handlerGroup errgroup.Group
for _, client := range arbiter.activeThemeClients {
client := client
handlerGroup.Go(func() error {
return handler(client, args)
})
}
return handlerGroup.Wait()
}
}
func (arbiter *commandArbiter) forSingleClient(handler arbitratedCmd) cobraCmdE {
return func(cmd *cobra.Command, args []string) error {
if len(arbiter.activeThemeClients) > 1 {
return fmt.Errorf("more than one environment specified for a single environment command")
}
return handler(arbiter.activeThemeClients[0], args)
}
}
func (arbiter *commandArbiter) setFlagConfig() {
if !arbiter.disableIgnore {
arbiter.flagConfig.IgnoredFiles = arbiter.ignoredFiles.Value()
arbiter.flagConfig.Ignores = arbiter.ignores.Value()
}
kit.SetFlagConfig(arbiter.flagConfig)
}
func (arbiter *commandArbiter) newProgressBar(count int, name string) *mpb.Bar {
var bar *mpb.Bar
if !arbiter.verbose && count > 0 {
bar = arbiter.progress.AddBar(int64(count)).
PrependName(fmt.Sprintf("[%s]: ", name), 0).
AppendPercentage().
PrependCounters(0, 0)
}
return bar
}
func (arbiter *commandArbiter) generateAssetActions(client kit.ThemeClient, filenames []string, destructive bool) (map[string]assetAction, error) {
assetsActions := map[string]assetAction{}
var err error
var assets []kit.Asset
if len(filenames) == 0 && destructive {
if assets, err = client.AssetList(); err != nil {
return nil, err
}
for _, asset := range assets {
assetsActions[asset.Key] = assetAction{asset: asset, event: kit.Remove}
}
}
if assets, err = client.LocalAssets(filenames...); err != nil {
return nil, err
}
for _, asset := range assets {
assetsActions[asset.Key] = assetAction{asset: asset, event: kit.Update}
}
return assetsActions, nil
}
func (arbiter *commandArbiter) preflightCheck(actions map[string]assetAction, destructive bool) error {
if arbiter.force {
return nil
}
for _, client := range arbiter.activeThemeClients {
diff := arbiter.manifest.Diff(actions, client.Config.Environment)
if diff.Any(destructive) {
return diff
}
}
return nil
}