@@ -4,10 +4,12 @@ import (
4
4
"fmt"
5
5
"io/ioutil"
6
6
"k9bookshelf/generated"
7
+ "os"
7
8
"path"
8
9
"path/filepath"
9
10
"sync"
10
11
12
+ shopify "github.com/bold-commerce/go-shopify"
11
13
"github.com/gomarkdown/markdown"
12
14
"github.com/vbauerster/mpb"
13
15
"github.com/vbauerster/mpb/decor"
@@ -61,51 +63,148 @@ func deployProducts(contents []Content, bar *mpb.Bar) error {
61
63
return err
62
64
}
63
65
64
- // Deploy uploads contents to store
65
- func Deploy (input string ) error {
66
- rawProducts , err := ioutil .ReadDir (path .Join (input , "products" ))
67
- if err != nil {
68
- return err
66
+ func deployPages (contents []Content , bar * mpb.Bar ) error {
67
+ var err error
68
+ adminClient := establishRestClient ()
69
+ wg := sync.WaitGroup {}
70
+ c := make (chan error )
71
+ for _ , content := range contents {
72
+ wg .Add (1 )
73
+
74
+ go func (handle , html string ) {
75
+ defer wg .Done ()
76
+ defer bar .Increment ()
77
+
78
+ pages , err := adminClient .Page .List (nil )
79
+ if err != nil {
80
+ c <- err
81
+ return
82
+ }
83
+ var page shopify.Page
84
+ for _ , p := range pages {
85
+ if p .Handle == handle {
86
+ page = p
87
+ break
88
+ }
89
+ }
90
+
91
+ // NOTE: Because Page struct isn't tagged by `omitempty` and Metafields are initialized with nil,
92
+ metafields := []shopify.Metafield {}
93
+ if page .Metafields != nil {
94
+ metafields = page .Metafields
95
+ }
96
+ _ , err = adminClient .Page .Update (shopify.Page {
97
+ ID : page .ID ,
98
+ Author : page .Author ,
99
+ Handle : handle ,
100
+ Title : page .Title ,
101
+ CreatedAt : page .CreatedAt ,
102
+ UpdatedAt : page .UpdatedAt ,
103
+ BodyHTML : html ,
104
+ TemplateSuffix : page .TemplateSuffix ,
105
+ PublishedAt : page .PublishedAt ,
106
+ ShopID : page .ShopID ,
107
+ Metafields : metafields ,
108
+ })
109
+
110
+ if err != nil {
111
+ c <- err
112
+ return
113
+ }
114
+ }(content .handle , content .html )
69
115
}
70
- products := []Content {}
71
- for _ , file := range rawProducts {
116
+ go func () {
117
+ wg .Wait ()
118
+ c <- nil
119
+ }()
120
+
121
+ err = <- c
122
+ return err
123
+ }
124
+
125
+ func filesToContents (inputDir string , files []os.FileInfo ) ([]Content , error ) {
126
+ contents := []Content {}
127
+ for _ , file := range files {
72
128
filename := file .Name ()
73
129
handle := filename [0 : len (filename )- len (filepath .Ext (filename ))]
74
- md , err := ioutil .ReadFile (path .Join (input , "products" , filename ))
130
+ md , err := ioutil .ReadFile (path .Join (inputDir , filename ))
75
131
if err != nil {
76
- return err
132
+ return nil , err
77
133
}
78
134
html := string (markdown .ToHTML (md , nil , nil ))
79
- products = append (products , Content {
135
+ contents = append (contents , Content {
80
136
handle : handle ,
81
137
html : html ,
82
138
})
83
139
}
140
+ return contents , nil
141
+ }
84
142
85
- // pages, err := ioutil.ReadDir(path.Join(input, "pages"))
86
- // if err != nil {
87
- // return err
88
- // }
143
+ type tmpIterable struct {
144
+ f func ( contents [] Content , bar * mpb. Bar ) error
145
+ contents [] Content
146
+ }
89
147
90
- wg := sync.WaitGroup {}
91
- p := mpb .New (mpb .WithWaitGroup (& wg ))
92
- bar := p .AddBar (int64 (len (products )),
93
- mpb .PrependDecorators (
94
- decor .Name (path .Join (input , "products" )),
95
- decor .Percentage (decor .WCSyncSpace ),
96
- ),
97
- mpb .AppendDecorators (
98
- decor .OnComplete (
99
- decor .EwmaETA (decor .ET_STYLE_GO , 60 ), "done" ,
100
- ),
101
- ),
102
- )
148
+ // Deploy uploads contents to store
149
+ func Deploy (input string ) error {
150
+ rawProducts , err := ioutil .ReadDir (path .Join (input , "products" ))
151
+ if err != nil {
152
+ return err
153
+ }
154
+ products , err := filesToContents (path .Join (input , "products" ), rawProducts )
155
+ if err != nil {
156
+ return err
157
+ }
103
158
104
- err = deployProducts ( products , bar )
159
+ rawPages , err := ioutil . ReadDir ( path . Join ( input , "pages" ) )
105
160
if err != nil {
106
161
return err
107
162
}
163
+ pages , err := filesToContents (path .Join (input , "pages" ), rawPages )
108
164
109
- p .Wait ()
110
- return nil
165
+ wg := sync.WaitGroup {}
166
+ p := mpb .New (mpb .WithWaitGroup (& wg ))
167
+
168
+ c := make (chan error )
169
+ for name , _f := range map [string ]tmpIterable {
170
+ "products" : {
171
+ f : func (contents []Content , bar * mpb.Bar ) error {
172
+ return deployProducts (products , bar )
173
+ },
174
+ contents : products ,
175
+ },
176
+ "pages" : {
177
+ f : func (contents []Content , bar * mpb.Bar ) error {
178
+ return deployPages (pages , bar )
179
+ },
180
+ contents : pages ,
181
+ },
182
+ } {
183
+ wg .Add (1 )
184
+ bar := p .AddBar (int64 (len (_f .contents )),
185
+ mpb .PrependDecorators (
186
+ decor .Name (path .Join (input , name )),
187
+ decor .Percentage (decor .WCSyncSpace ),
188
+ ),
189
+ mpb .AppendDecorators (
190
+ decor .OnComplete (
191
+ decor .EwmaETA (decor .ET_STYLE_GO , 60 ), "done" ,
192
+ ),
193
+ ),
194
+ )
195
+ go func (f func (contents []Content , bar * mpb.Bar ) error ) {
196
+ defer wg .Done ()
197
+ if err = f (pages , bar ); err != nil {
198
+ c <- err
199
+ return
200
+ }
201
+ }(_f .f )
202
+ }
203
+ go func () {
204
+ p .Wait ()
205
+ c <- nil
206
+ }()
207
+
208
+ err = <- c
209
+ return err
111
210
}
0 commit comments