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

1
Cargo.lock generated
View file

@ -30,6 +30,7 @@ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"serde", "serde",
"serde_json",
"ureq", "ureq",
] ]

View file

@ -10,4 +10,5 @@ edition = "2018"
anyhow = "1.0.41" anyhow = "1.0.41"
clap = "3.0.0-beta.2" clap = "3.0.0-beta.2"
serde = { version = "1.0.126", features = ["derive"] } serde = { version = "1.0.126", features = ["derive"] }
serde_json = "1.0.64"
ureq = { version = "2.1.1", features = ["json"] } ureq = { version = "2.1.1", features = ["json"] }

View file

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