@@ -199,6 +199,13 @@ struct GetArgs {
199199 #[ arg( long, value_enum, default_value = "auto" ) ]
200200 format : Format ,
201201
202+ /// Check that the got package matches the existing file at the output
203+ /// path. Output path will not be modified. Program exits with codes
204+ /// simmilar to diff(1): exits with 1 if there were differences, and 0
205+ /// means no differences.
206+ #[ arg( long) ]
207+ check : bool ,
208+
202209 /// Overwrite any existing output file.
203210 #[ arg( long) ]
204211 overwrite : bool ,
@@ -269,6 +276,11 @@ enum Format {
269276
270277impl GetArgs {
271278 pub async fn run ( self ) -> anyhow:: Result < ( ) > {
279+ ensure ! (
280+ !( self . overwrite && self . check) ,
281+ "Not allowed to specify both --check and --overwrite"
282+ ) ;
283+
272284 let PackageSpec { package, version } = self . package_spec ;
273285 let mut config = self . common . load_config ( ) . await ?;
274286 if let Some ( registry) = self . registry_args . registry . clone ( ) {
@@ -371,21 +383,34 @@ impl GetArgs {
371383 } else {
372384 self . output
373385 } ;
374- ensure ! (
375- self . overwrite || !output_path. exists( ) ,
376- "{output_path:?} already exists; you can use '--overwrite' to overwrite it"
377- ) ;
378386
379- if let Some ( wit) = wit {
380- std:: fs:: write ( & output_path, wit)
381- . with_context ( || format ! ( "Failed to write WIT to {output_path:?}" ) ) ?
387+ if self . check {
388+ let existing = std:: fs:: read ( & output_path)
389+ . with_context ( || format ! ( "Failed to read {output_path:?}" ) ) ?;
390+ let latest = if let Some ( wit) = wit {
391+ wit. into_bytes ( )
392+ } else {
393+ std:: fs:: read ( & tmp_path) . with_context ( || format ! ( "Failed to read {tmp_path:?}" ) ) ?
394+ } ;
395+ if existing != latest {
396+ anyhow:: bail!( "Differences between retrieved and {output_path:?}" ) ;
397+ }
382398 } else {
383- tmp_path
384- . persist ( & output_path)
385- . with_context ( || format ! ( "Failed to persist WASM to {output_path:?}" ) ) ?
386- }
387- println ! ( "Wrote '{}'" , output_path. display( ) ) ;
399+ ensure ! (
400+ self . overwrite || !output_path. exists( ) ,
401+ "{output_path:?} already exists; you can use '--overwrite' to overwrite it"
402+ ) ;
388403
404+ if let Some ( wit) = wit {
405+ std:: fs:: write ( & output_path, wit)
406+ . with_context ( || format ! ( "Failed to write WIT to {output_path:?}" ) ) ?
407+ } else {
408+ tmp_path
409+ . persist ( & output_path)
410+ . with_context ( || format ! ( "Failed to persist WASM to {output_path:?}" ) ) ?
411+ }
412+ println ! ( "Wrote '{}'" , output_path. display( ) ) ;
413+ }
389414 Ok ( ( ) )
390415 }
391416}
0 commit comments