This repository was archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathdownload.go
More file actions
291 lines (256 loc) · 6.19 KB
/
download.go
File metadata and controls
291 lines (256 loc) · 6.19 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package parsecmd
import (
"crypto/md5"
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path"
"path/filepath"
"sync/atomic"
"github.com/ParsePlatform/parse-cli/parsecli"
"github.com/facebookgo/errgroup"
"github.com/facebookgo/stackerr"
"github.com/spf13/cobra"
)
type downloadCmd struct {
release *deployInfo
destination string
force bool
}
var errNoFiles = errors.New("Nothing to download. Not yet deployed to the app.")
func (d *downloadCmd) verifyChecksum(path, checksum string) error {
file, err := os.Open(path)
if err != nil {
return stackerr.Wrap(err)
}
defer file.Close()
h := md5.New()
_, err = io.Copy(h, file)
if err != nil {
return stackerr.Wrap(err)
}
sum := fmt.Sprintf("%x", h.Sum(nil))
if sum != checksum {
return stackerr.Newf("Invalid checksum for %s", path)
}
return nil
}
func (d *downloadCmd) moveFiles(
e *parsecli.Env,
destination string,
release *deployInfo) error {
var wg errgroup.Group
maxParallel := make(chan struct{}, maxOpenFD)
numFiles := len(release.Versions.Cloud) + len(release.Versions.Public)
wg.Add(numFiles)
var numErrors int32
moveFile := func(destination, kind, file, checksum string) {
defer func() {
wg.Done()
<-maxParallel
}()
err := os.MkdirAll(
filepath.Join(e.Root, kind, filepath.Dir(file)),
0755,
)
if err != nil {
atomic.AddInt32(&numErrors, 1)
wg.Error(stackerr.Wrap(err))
return
}
err = os.Rename(
filepath.Join(destination, kind, file),
filepath.Join(e.Root, kind, file),
)
if err != nil {
atomic.AddInt32(&numErrors, 1)
wg.Error(stackerr.Wrap(err))
return
}
err = d.verifyChecksum(
filepath.Join(e.Root, kind, file),
checksum,
)
if err != nil {
atomic.AddInt32(&numErrors, 1)
wg.Error(err)
return
}
}
for file, checksum := range release.Checksums.Cloud {
maxParallel <- struct{}{}
go moveFile(
destination,
parsecli.CloudDir,
file,
checksum,
)
}
for file, checksum := range release.Checksums.Public {
maxParallel <- struct{}{}
go moveFile(
destination,
parsecli.HostingDir,
file,
checksum,
)
}
if err := wg.Wait(); err != nil {
// could not move a single file so no corruption:w
if int(numErrors) == numFiles {
fmt.Fprintf(
e.Out,
`Failed to download Cloud Code to
%q
Try "parse download" and manually move contents from
the temporary download location.
`,
e.Root,
)
return nil
}
fmt.Fprintf(
e.Out,
`Failed to download Cloud Code to
%q
It might have corrupted contents, due to partially moved files.
Try "parse download" and manually move contents from
the temporary download location.
`,
e.Root,
)
return err
}
return nil
}
func (d *downloadCmd) download(e *parsecli.Env, destination string, release *deployInfo) error {
var wg errgroup.Group
maxParallel := make(chan struct{}, maxOpenFD)
wg.Add(len(release.Versions.Cloud) + len(release.Versions.Public))
downloadHosted := func(file, version, checksum string) {
defer func() {
wg.Done()
<-maxParallel
}()
v := make(url.Values)
v.Set("version", version)
v.Set("checksum", checksum)
u := &url.URL{
Path: path.Join("hosted_files", file),
RawQuery: v.Encode(),
}
var content []byte
_, err := e.ParseAPIClient.Get(u, &content)
if err != nil {
wg.Error(err)
return
}
path := path.Join(destination, parsecli.HostingDir, file)
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
wg.Error(stackerr.Wrap(err))
return
}
if err := ioutil.WriteFile(path, content, 0644); err != nil {
wg.Error(stackerr.Wrap(err))
return
}
if err := d.verifyChecksum(path, checksum); err != nil {
wg.Error(err)
return
}
}
downloadScript := func(file, version, checksum string) {
defer func() {
wg.Done()
<-maxParallel
}()
v := make(url.Values)
v.Set("version", version)
v.Set("checksum", checksum)
u := &url.URL{
Path: path.Join("scripts", file),
RawQuery: v.Encode(),
}
var content string
_, err := e.ParseAPIClient.Get(u, &content)
if err != nil {
wg.Error(err)
return
}
path := path.Join(destination, parsecli.CloudDir, file)
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
wg.Error(stackerr.Wrap(err))
return
}
if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil {
wg.Error(stackerr.Wrap(err))
return
}
}
for file, version := range release.Versions.Public {
checksum, ok := release.Checksums.Public[file]
if !ok {
continue
}
maxParallel <- struct{}{}
go downloadHosted(file, version, checksum)
}
for file, version := range release.Versions.Cloud {
checksum, ok := release.Checksums.Cloud[file]
if !ok {
continue
}
maxParallel <- struct{}{}
go downloadScript(file, version, checksum)
}
return wg.Wait()
}
func (d *downloadCmd) run(e *parsecli.Env, c *parsecli.Context) error {
var err error
latestRelease := d.release
if latestRelease == nil {
latestRelease, err = (&deployCmd{}).getPrevDeplInfo(e)
if err != nil {
return err
}
if len(latestRelease.Versions.Cloud) == 0 && len(latestRelease.Versions.Public) == 0 {
return errNoFiles
}
}
destination := d.destination
if destination == "" {
destination, err = ioutil.TempDir("", "parse_code_")
if err != nil {
return stackerr.Wrap(err)
}
}
err = d.download(e, destination, latestRelease)
if err != nil {
fmt.Fprintln(e.Err, "Failed to download Cloud Code.")
return stackerr.Wrap(err)
}
if !d.force {
fmt.Fprintf(e.Out, "Successfully downloaded Cloud Code to %q.\n", destination)
return nil
}
return stackerr.Wrap(d.moveFiles(e, destination, latestRelease))
}
func NewDownloadCmd(e *parsecli.Env) *cobra.Command {
d := &downloadCmd{}
cmd := &cobra.Command{
Use: "download [app]",
Short: "Downloads the Cloud Code project",
Long: `Downloads the Cloud Code project at a given location,
or at a temporary location if nothing is explicitly provided through the -l flag.
`,
Run: parsecli.RunWithClient(e, d.run),
}
cmd.Flags().BoolVarP(&d.force, "force", "f", d.force,
"Force will overwrite any files in the current project directory")
cmd.Flags().StringVarP(&d.destination, "location", "l", d.destination,
"Download Cloud Code project at the given location.")
return cmd
}