Skip to content

Commit

Permalink
Add format argument to version printer
Browse files Browse the repository at this point in the history
  • Loading branch information
FlareFlo committed Jan 29, 2025
1 parent b43701d commit 80d2c55
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/cli/vromf_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,12 @@ pub fn vromf_version() -> Command {
.required(true)
.value_hint(ValueHint::AnyPath),
)
.arg(
Arg::new("format")
.short('f')
.long("format")
.help("Prints the version either in plain or json text format")
.default_value("json"),
)
.about("Prints version(s) from file or folder of vromfs")
}
47 changes: 35 additions & 12 deletions src/subcommands/vromf_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,41 @@ pub fn vromf_version(args: &ArgMatches) -> color_eyre::Result<()> {
}
versions
};
let json = Value::Array(
versions
.into_iter()
.map(|e| {
Value::Object(Map::from_iter(once((
e.0,
json!(e.1.map(|e| e.to_string())),
))))
})
.collect(),
);
println!("{}", serde_json::to_string_pretty(&json)?);

match args.get_one::<String>("format").expect("infallible").as_ref() {
"json" => {
let json = Value::Array(
versions
.into_iter()
.map(|e| {
Value::Object(Map::from_iter(once((
e.0,
json!(e.1.map(|e| e.to_string())),
))))
})
.collect(),
);
println!("{}", serde_json::to_string_pretty(&json)?);
},
"plain" => {
if versions.len() == 1 {
for (file, maybe_version) in versions {
if let Some(version) = maybe_version {
println!("{version}");
} else {
println!("null");
}
}
} else {
if let Some((name, version)) = versions.get(0) {
println!("{} {}", name, version.map(|e|e.to_string()).unwrap_or("null".to_owned()));
}
}
},
_ => {
panic!("Unrecognized output format");
},
}

Ok(())
}

0 comments on commit 80d2c55

Please sign in to comment.