@@ -12,6 +12,8 @@ use std::path::Path;
12
12
use std:: process:: Command ;
13
13
use term2;
14
14
15
+ use serde_derive:: Serialize ;
16
+
15
17
use crate :: json_dump;
16
18
17
19
pub fn main ( ) -> Result < ( ) > {
@@ -135,7 +137,10 @@ pub fn cli() -> App<'static, 'static> {
135
137
. after_help ( TOOLCHAIN_GC_HELP )
136
138
. arg ( Arg :: with_name ( "delete" )
137
139
. long ( "delete" )
138
- . help ( "Delete collected toolchains instead of only reporting them" ) ) ) )
140
+ . help ( "Delete collected toolchains instead of only reporting them" ) )
141
+ . arg ( Arg :: with_name ( "json" )
142
+ . long ( "json" )
143
+ . help ( "Format output as JSON" ) ) ) )
139
144
. subcommand ( SubCommand :: with_name ( "override" )
140
145
. about ( "Modify directory toolchain overrides" )
141
146
. after_help ( OVERRIDE_HELP )
@@ -444,9 +449,38 @@ fn toolchain_remove(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
444
449
Ok ( ( ) )
445
450
}
446
451
452
+ #[ derive( Serialize ) ]
453
+ struct UsedToolchain {
454
+ // project root or "default toolchain"
455
+ user : String ,
456
+ toolchain : String ,
457
+ }
458
+
459
+ #[ derive( Serialize ) ]
460
+ struct GCResult {
461
+ unused_toolchains : Vec < String > ,
462
+ used_toolchains : Vec < UsedToolchain > ,
463
+ }
464
+
447
465
fn toolchain_gc ( cfg : & Cfg , m : & ArgMatches < ' _ > ) -> Result < ( ) > {
448
466
let ( unused_toolchains, used_toolchains) = gc:: analyze_toolchains ( cfg) ?;
449
467
let delete = m. is_present ( "delete" ) ;
468
+ let json = m. is_present ( "json" ) ;
469
+ if json {
470
+ let result = GCResult {
471
+ unused_toolchains : unused_toolchains. iter ( ) . map ( |t| t. desc . to_string ( ) ) . collect ( ) ,
472
+ used_toolchains : used_toolchains
473
+ . iter ( )
474
+ . map ( |( root, tc) | UsedToolchain {
475
+ user : root. clone ( ) ,
476
+ toolchain : tc. to_string ( ) ,
477
+ } )
478
+ . collect ( ) ,
479
+ } ;
480
+ println ! ( "{}" , serde_json:: to_string_pretty( & result) . chain_err( || "failed to print JSON" ) ?) ;
481
+ return Ok ( ( ) ) ;
482
+ }
483
+
450
484
if unused_toolchains. is_empty ( ) {
451
485
println ! ( "No unused toolchains found" ) ;
452
486
} else {
0 commit comments