Add list --json option

This commit is contained in:
Andrey Golovizin 2021-06-27 13:16:39 +02:00
parent a382269281
commit fc0b55a207
3 changed files with 17 additions and 5 deletions

View file

@ -13,6 +13,12 @@ struct Opts {
command: Command,
}
#[derive(Clap, Debug)]
struct ListOpts {
#[clap(short, long)]
json: bool,
}
#[derive(Clap, Debug)]
struct GetConfigOpts {
location: String,
@ -22,7 +28,7 @@ struct GetConfigOpts {
#[derive(Clap, Debug)]
enum Command {
List,
List(ListOpts),
GetConfig(GetConfigOpts),
}
@ -67,16 +73,20 @@ struct WireguardKeyPair {
fn main() -> Result<(), anyhow::Error> {
let opts = Opts::parse();
match opts.command {
Command::List => list()?,
match &opts.command {
Command::List(list_opts) => list(&list_opts)?,
Command::GetConfig(get_config_opts) => get_config(&get_config_opts)?,
}
Ok(())
}
fn list() -> Result<(), anyhow::Error> {
fn list(opts: &ListOpts) -> Result<(), anyhow::Error> {
let locations: Locations = get_locations()?;
println!("{:?}", &locations);
if opts.json {
println!("{}", serde_json::to_string(&locations)?);
} else {
println!("{:?}", &locations);
}
Ok(())
}