diff --git a/Cargo.lock b/Cargo.lock index 219c108..1a976ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,6 +30,7 @@ dependencies = [ "anyhow", "clap", "serde", + "serde_json", "ureq", ] diff --git a/Cargo.toml b/Cargo.toml index 75e4811..3edc57e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/main.rs b/src/main.rs index c78977c..06ab060 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(()) }