-
Notifications
You must be signed in to change notification settings - Fork 375
/
upload.go
93 lines (78 loc) · 2.17 KB
/
upload.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
package cmd
import (
"sync"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb"
"github.com/Shopify/themekit/kit"
)
const settingsDataKey = "config/settings_data.json"
var uploadCmd = &cobra.Command{
Use: "upload <filenames>",
Short: "Upload theme file(s) to shopify",
Long: `Upload will upload specific files to shopify servers if provided file names.
If no filenames are provided then upload will upload every file in the project
to shopify.
For more documentation please see http://shopify.github.io/themekit/commands/#upload
`,
RunE: forEachClient(upload, uploadSettingsData),
}
func upload(client kit.ThemeClient, filenames []string, wg *sync.WaitGroup) {
defer wg.Done()
if client.Config.ReadOnly {
kit.LogErrorf("[%s]environment is reaonly", kit.GreenText(client.Config.Environment))
return
}
localAssets, err := client.LocalAssets(filenames...)
if err != nil {
kit.LogErrorf("[%s] %v", kit.GreenText(client.Config.Environment), err)
return
}
bar := newProgressBar(len(localAssets), client.Config.Environment)
for _, asset := range localAssets {
if asset.Key == settingsDataKey {
incBar(bar) //pretend we did this one we will do it later
continue
}
wg.Add(1)
go performUpload(client, asset, bar, wg)
}
}
func performUpload(client kit.ThemeClient, asset kit.Asset, bar *mpb.Bar, wg *sync.WaitGroup) {
defer wg.Done()
defer incBar(bar)
resp, err := client.UpdateAsset(asset)
if err != nil {
kit.LogErrorf("[%s]%s", kit.GreenText(client.Config.Environment), err)
} else if verbose {
kit.Printf(
"[%s] Successfully performed Update on file %s from %s",
kit.GreenText(client.Config.Environment),
kit.GreenText(asset.Key),
kit.YellowText(resp.Host),
)
}
}
func uploadSettingsData(client kit.ThemeClient, filenames []string, wg *sync.WaitGroup) {
if client.Config.ReadOnly {
return
}
doupload := func() {
asset, err := client.LocalAsset(settingsDataKey)
if err != nil {
kit.LogError(err)
return
}
wg.Add(1)
go performUpload(client, asset, nil, wg)
}
if len(filenames) == 0 {
doupload()
} else {
for _, filename := range filenames {
if filename == settingsDataKey {
doupload()
return
}
}
}
}