@@ -17,8 +17,9 @@ import (
1717)
1818
1919type downloadCmd struct {
20- force bool
21- release * deployInfo
20+ release * deployInfo
21+ destination string
22+ force bool
2223}
2324
2425func (d * downloadCmd ) verifyChecksum (path , checksum string ) error {
@@ -44,7 +45,7 @@ func (d *downloadCmd) verifyChecksum(path, checksum string) error {
4445
4546func (d * downloadCmd ) moveFiles (
4647 e * env ,
47- tempDir string ,
48+ destination string ,
4849 release * deployInfo ) error {
4950 var wg errgroup.Group
5051
@@ -53,7 +54,7 @@ func (d *downloadCmd) moveFiles(
5354 wg .Add (numFiles )
5455
5556 var numErrors int32
56- moveFile := func (tempDir , kind , file , checksum string ) {
57+ moveFile := func (destination , kind , file , checksum string ) {
5758 defer func () {
5859 wg .Done ()
5960 <- maxParallel
@@ -70,7 +71,7 @@ func (d *downloadCmd) moveFiles(
7071 }
7172
7273 err = os .Rename (
73- filepath .Join (tempDir , kind , file ),
74+ filepath .Join (destination , kind , file ),
7475 filepath .Join (e .Root , kind , file ),
7576 )
7677 if err != nil {
@@ -93,7 +94,7 @@ func (d *downloadCmd) moveFiles(
9394 for file , checksum := range release .Checksums .Cloud {
9495 maxParallel <- struct {}{}
9596 go moveFile (
96- tempDir ,
97+ destination ,
9798 cloudDir ,
9899 file ,
99100 checksum ,
@@ -102,7 +103,7 @@ func (d *downloadCmd) moveFiles(
102103 for file , checksum := range release .Checksums .Public {
103104 maxParallel <- struct {}{}
104105 go moveFile (
105- tempDir ,
106+ destination ,
106107 hostingDir ,
107108 file ,
108109 checksum ,
@@ -141,7 +142,7 @@ the temporary download location.
141142 return nil
142143}
143144
144- func (d * downloadCmd ) download (e * env , tempDir string , release * deployInfo ) error {
145+ func (d * downloadCmd ) download (e * env , destination string , release * deployInfo ) error {
145146 var wg errgroup.Group
146147 maxParallel := make (chan struct {}, maxOpenFD )
147148 wg .Add (len (release .Versions .Cloud ) + len (release .Versions .Public ))
@@ -166,7 +167,7 @@ func (d *downloadCmd) download(e *env, tempDir string, release *deployInfo) erro
166167 return
167168 }
168169
169- path := path .Join (tempDir , hostingDir , file )
170+ path := path .Join (destination , hostingDir , file )
170171 if err := os .MkdirAll (filepath .Dir (path ), 0755 ); err != nil {
171172 wg .Error (stackerr .Wrap (err ))
172173 return
@@ -201,7 +202,7 @@ func (d *downloadCmd) download(e *env, tempDir string, release *deployInfo) erro
201202 return
202203 }
203204
204- path := path .Join (tempDir , cloudDir , file )
205+ path := path .Join (destination , cloudDir , file )
205206 if err := os .MkdirAll (filepath .Dir (path ), 0755 ); err != nil {
206207 wg .Error (stackerr .Wrap (err ))
207208 return
@@ -234,41 +235,50 @@ func (d *downloadCmd) download(e *env, tempDir string, release *deployInfo) erro
234235}
235236
236237func (d * downloadCmd ) run (e * env , c * context ) error {
237- if d .release == nil {
238- latestRelease , err := (& deployCmd {}).getPrevDeplInfo (e )
238+ var err error
239+
240+ latestRelease := d .release
241+ if latestRelease == nil {
242+ latestRelease , err = (& deployCmd {}).getPrevDeplInfo (e )
239243 if err != nil {
240244 return err
241245 }
242- d .release = latestRelease
243246 }
244247
245- tempDir , err := ioutil .TempDir ("" , "parse_code_" )
246- if err != nil {
247- return stackerr .Wrap (err )
248+ destination := d .destination
249+ if destination == "" {
250+ destination , err = ioutil .TempDir ("" , "parse_code_" )
251+ if err != nil {
252+ return stackerr .Wrap (err )
253+ }
248254 }
249255
250- err = d .download (e , tempDir , d . release )
256+ err = d .download (e , destination , latestRelease )
251257 if err != nil {
252258 fmt .Fprintln (e .Err , "Failed to download Cloud Code." )
253259 return stackerr .Wrap (err )
254260 }
255261 if ! d .force {
256- fmt .Fprintf (e .Out , "Successfully downloaded Cloud Code to %q.\n " , tempDir )
262+ fmt .Fprintf (e .Out , "Successfully downloaded Cloud Code to %q.\n " , destination )
257263 return nil
258264 }
259265
260- return stackerr .Wrap (d .moveFiles (e , tempDir , d . release ))
266+ return stackerr .Wrap (d .moveFiles (e , destination , latestRelease ))
261267}
262268
263269func newDownloadCmd (e * env ) * cobra.Command {
264270 d := & downloadCmd {}
265271 cmd := & cobra.Command {
266272 Use : "download [app]" ,
267273 Short : "Downloads the Cloud Code project" ,
268- Long : "Downloads the Cloud Code project at a temporary location." ,
269- Run : runWithClient (e , d .run ),
274+ Long : `Downloads the Cloud Code project at a given location,
275+ or at a temporary location if nothing is explicitly provided through the -l flag.
276+ ` ,
277+ Run : runWithClient (e , d .run ),
270278 }
271279 cmd .Flags ().BoolVarP (& d .force , "force" , "f" , d .force ,
272280 "Force will overwrite any files in the current project directory" )
281+ cmd .Flags ().StringVarP (& d .destination , "location" , "l" , d .destination ,
282+ "Download Cloud Code project at the given location." )
273283 return cmd
274284}
0 commit comments