Skip to content

Commit bc4c0de

Browse files
committed
feat: elan toolchain gc --json
1 parent 14110ad commit bc4c0de

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/elan-cli/elan_mode.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use std::path::Path;
1212
use std::process::Command;
1313
use term2;
1414

15+
use serde_derive::Serialize;
16+
1517
use crate::json_dump;
1618

1719
pub fn main() -> Result<()> {
@@ -135,7 +137,10 @@ pub fn cli() -> App<'static, 'static> {
135137
.after_help(TOOLCHAIN_GC_HELP)
136138
.arg(Arg::with_name("delete")
137139
.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"))))
139144
.subcommand(SubCommand::with_name("override")
140145
.about("Modify directory toolchain overrides")
141146
.after_help(OVERRIDE_HELP)
@@ -444,9 +449,38 @@ fn toolchain_remove(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
444449
Ok(())
445450
}
446451

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+
447465
fn toolchain_gc(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
448466
let (unused_toolchains, used_toolchains) = gc::analyze_toolchains(cfg)?;
449467
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+
450484
if unused_toolchains.is_empty() {
451485
println!("No unused toolchains found");
452486
} else {

0 commit comments

Comments
 (0)