99 "os"
1010 "path"
1111 "path/filepath"
12+ "sync/atomic"
1213
1314 "github.com/facebookgo/errgroup"
1415 "github.com/facebookgo/stackerr"
@@ -48,8 +49,10 @@ func (d *downloadCmd) moveFiles(
4849 var wg errgroup.Group
4950
5051 maxParallel := make (chan struct {}, maxOpenFD )
51- wg .Add (len (release .Versions .Cloud ) + len (release .Versions .Public ))
52+ numFiles := len (release .Versions .Cloud ) + len (release .Versions .Public )
53+ wg .Add (numFiles )
5254
55+ var numErrors int32
5356 moveFile := func (tempDir , kind , file , checksum string ) {
5457 defer func () {
5558 wg .Done ()
@@ -61,6 +64,7 @@ func (d *downloadCmd) moveFiles(
6164 0755 ,
6265 )
6366 if err != nil {
67+ atomic .AddInt32 (& numErrors , 1 )
6468 wg .Error (stackerr .Wrap (err ))
6569 return
6670 }
@@ -70,6 +74,7 @@ func (d *downloadCmd) moveFiles(
7074 filepath .Join (e .Root , kind , file ),
7175 )
7276 if err != nil {
77+ atomic .AddInt32 (& numErrors , 1 )
7378 wg .Error (stackerr .Wrap (err ))
7479 return
7580 }
@@ -79,6 +84,7 @@ func (d *downloadCmd) moveFiles(
7984 checksum ,
8085 )
8186 if err != nil {
87+ atomic .AddInt32 (& numErrors , 1 )
8288 wg .Error (err )
8389 return
8490 }
@@ -102,7 +108,37 @@ func (d *downloadCmd) moveFiles(
102108 checksum ,
103109 )
104110 }
105- return wg .Wait ()
111+
112+ if err := wg .Wait (); err != nil {
113+ // could not move a single file so no corruption:w
114+ if int (numErrors ) == numFiles {
115+ fmt .Fprintf (
116+ e .Out ,
117+ `Failed to download Cloud Code to
118+ %q
119+ Try "parse download" and manually move contents from
120+ the temporary download location.
121+ ` ,
122+ e .Root ,
123+ )
124+ return nil
125+ }
126+
127+ fmt .Fprintf (
128+ e .Out ,
129+ `Failed to download Cloud Code to
130+ %q
131+
132+ It might have corrupted contents, due to partially moved files.
133+
134+ Try "parse download" and manually move contents from
135+ the temporary download location.
136+ ` ,
137+ e .Root ,
138+ )
139+ return err
140+ }
141+ return nil
106142}
107143
108144func (d * downloadCmd ) download (e * env , tempDir string , release * deployInfo ) error {
@@ -221,21 +257,7 @@ func (d *downloadCmd) run(e *env, c *context) error {
221257 return nil
222258 }
223259
224- err = d .moveFiles (e , tempDir , d .release )
225- if err != nil {
226- fmt .Fprintf (
227- e .Out ,
228- `Failed to download Cloud Code to %q.
229- Sorry! but %s might have corrupted contents.
230- If you want to download Cloud Code from Parse,
231- try again without the "-f" option.
232- ` ,
233- e .Root ,
234- e .Root ,
235- )
236- return stackerr .Wrap (err )
237- }
238- return nil
260+ return stackerr .Wrap (d .moveFiles (e , tempDir , d .release ))
239261}
240262
241263func newDownloadCmd (e * env ) * cobra.Command {
@@ -247,6 +269,6 @@ func newDownloadCmd(e *env) *cobra.Command {
247269 Run : runWithClient (e , d .run ),
248270 }
249271 cmd .Flags ().BoolVarP (& d .force , "force" , "f" , d .force ,
250- "Force will overwrite any content in the current project directory" )
272+ "Force will overwrite any files in the current project directory" )
251273 return cmd
252274}
0 commit comments