@@ -28,6 +28,15 @@ pub struct PackageDownload {
2828 #[ clap( long) ]
2929 pub quiet : bool ,
3030
31+ /// Unpack the downloaded package.
32+ ///
33+ /// The output directory will be next to the downloaded file.
34+ ///
35+ /// Note: unpacking can also be done manually with the `wasmer package unpack`
36+ /// command.
37+ #[ clap( long) ]
38+ unpack : bool ,
39+
3140 /// The package to download.
3241 package : PackageSource ,
3342}
@@ -62,6 +71,28 @@ impl PackageDownload {
6271
6372 step_num += 1 ;
6473
74+ let out_dir = if let Some ( parent) = self . out_path . as_ref ( ) . and_then ( |p| p. parent ( ) ) {
75+ match parent. metadata ( ) {
76+ Ok ( m) => {
77+ if !m. is_dir ( ) {
78+ bail ! (
79+ "parent of output file is not a directory: '{}'" ,
80+ parent. display( )
81+ ) ;
82+ }
83+ parent. to_owned ( )
84+ }
85+ Err ( err) if err. kind ( ) == std:: io:: ErrorKind :: NotFound => {
86+ std:: fs:: create_dir_all ( parent)
87+ . context ( "could not create parent directory of output file" ) ?;
88+ parent. to_owned ( )
89+ }
90+ Err ( err) => return Err ( err. into ( ) ) ,
91+ }
92+ } else {
93+ current_dir ( ) ?
94+ } ;
95+
6596 if let Some ( parent) = self . out_path . as_ref ( ) . and_then ( |p| p. parent ( ) ) {
6697 match parent. metadata ( ) {
6798 Ok ( m) => {
@@ -195,11 +226,7 @@ impl PackageDownload {
195226 // Set the length of the progress bar
196227 pb. set_length ( webc_total_size) ;
197228
198- let mut tmpfile = if let Some ( parent) = self . out_path . as_ref ( ) . and_then ( |p| p. parent ( ) ) {
199- NamedTempFile :: new_in ( parent) ?
200- } else {
201- NamedTempFile :: new ( ) ?
202- } ;
229+ let mut tmpfile = NamedTempFile :: new_in ( & out_dir) ?;
203230 let accepted_contenttypes = vec ! [
204231 "application/webc" ,
205232 "application/octet-stream" ,
@@ -239,7 +266,7 @@ impl PackageDownload {
239266 let out_path = if let Some ( out_path) = & self . out_path {
240267 out_path. clone ( )
241268 } else {
242- current_dir ( ) ? . join ( filename)
269+ out_dir . join ( filename)
243270 } ;
244271
245272 tmpfile. persist ( & out_path) . with_context ( || {
@@ -258,6 +285,23 @@ impl PackageDownload {
258285 // We're done, so finish the progress bar
259286 pb. finish ( ) ;
260287
288+ if self . unpack {
289+ let out_dir = if out_path. extension ( ) . is_some ( ) {
290+ out_path. with_extension ( "" )
291+ } else {
292+ out_path. with_extension ( "unpacked" )
293+ } ;
294+
295+ let unpack_cmd = super :: unpack:: PackageUnpack {
296+ out_dir,
297+ overwrite : false ,
298+ quiet : self . quiet ,
299+ package_path : out_path,
300+ format : super :: unpack:: Format :: Package ,
301+ } ;
302+ unpack_cmd. execute ( ) ?;
303+ }
304+
261305 Ok ( ( ) )
262306 }
263307}
@@ -283,11 +327,14 @@ mod tests {
283327 validate : true ,
284328 out_path : Some ( out_path. clone ( ) ) ,
285329 package : "wasmer/[email protected] " . parse ( ) . unwrap ( ) , 330+ unpack : true ,
286331 quiet : true ,
287332 } ;
288333
289334 cmd. execute ( ) . unwrap ( ) ;
290335
291336 from_disk ( out_path) . unwrap ( ) ;
337+
338+ assert ! ( dir. path( ) . join( "hello/wasmer.toml" ) . is_file( ) ) ;
292339 }
293340}
0 commit comments