-
Notifications
You must be signed in to change notification settings - Fork 375
/
replace.go
90 lines (75 loc) · 2.25 KB
/
replace.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
package cmd
import (
"sync"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb"
"github.com/Shopify/themekit/kit"
)
type assetAction struct {
asset kit.Asset
event kit.EventType
}
var replaceCmd = &cobra.Command{
Use: "replace <filenames>",
Short: "Overwrite theme file(s)",
Long: `Replace will overwrite specific files if provided with file names.
If replace is not provided with file names then it will replace all
the files on shopify with your local files. Any files that do not
exist on your local machine will be removed from shopify.
For more documentation please see http://shopify.github.io/themekit/commands/#replace
`,
RunE: forEachClient(replace, uploadSettingsData),
}
func replace(client kit.ThemeClient, filenames []string, wg *sync.WaitGroup) {
if len(filenames) > 0 {
upload(client, filenames, wg)
return
}
defer wg.Done()
if client.Config.ReadOnly {
kit.LogErrorf("[%s]environment is reaonly", kit.GreenText(client.Config.Environment))
return
}
assetsActions := map[string]assetAction{}
assets, remoteErr := client.AssetList()
if remoteErr != nil {
kit.LogError(remoteErr)
return
}
for _, asset := range assets {
assetsActions[asset.Key] = assetAction{asset: asset, event: kit.Remove}
}
localAssets, localErr := client.LocalAssets()
if localErr != nil {
kit.LogError(localErr)
return
}
for _, asset := range localAssets {
assetsActions[asset.Key] = assetAction{asset: asset, event: kit.Update}
}
bar := newProgressBar(len(assetsActions), client.Config.Environment)
for key, action := range assetsActions {
if key == settingsDataKey {
incBar(bar) //pretend we did this one we will do it later
continue
}
wg.Add(1)
go performReplace(client, action.asset, action.event, bar, wg)
}
}
func performReplace(client kit.ThemeClient, asset kit.Asset, event kit.EventType, bar *mpb.Bar, wg *sync.WaitGroup) {
defer wg.Done()
defer incBar(bar)
resp, err := client.Perform(asset, event)
if err != nil {
kit.LogErrorf("[%s]%s", kit.GreenText(client.Config.Environment), err)
} else if verbose {
kit.Printf(
"[%s] Successfully performed %s on file %s from %s",
kit.GreenText(client.Config.Environment),
kit.GreenText(resp.EventType),
kit.GreenText(resp.Asset.Key),
kit.YellowText(resp.Host),
)
}
}