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",
"clap",
"serde",
"serde_json",
"ureq",
]

View file

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

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(())
}