Make --json option global

This commit is contained in:
Andrey Golovizin 2021-06-29 23:15:02 +02:00
parent 1a2a96911e
commit 4abc08a34b

View file

@ -11,14 +11,11 @@ const BASE_URL: &str = "https://api.azirevpn.com/v1";
#[derive(Clap, Debug)] #[derive(Clap, Debug)]
struct Opts { struct Opts {
#[clap(subcommand)]
command: Command,
}
#[derive(Clap, Debug)]
struct EndpointsOpts {
#[clap(short, long)] #[clap(short, long)]
json: bool, json: bool,
#[clap(subcommand)]
command: Command,
} }
#[derive(Clap, Debug)] #[derive(Clap, Debug)]
@ -30,7 +27,7 @@ struct ConfigOpts {
#[derive(Clap, Debug)] #[derive(Clap, Debug)]
enum Command { enum Command {
Endpoints(EndpointsOpts), Endpoints,
Config(ConfigOpts), Config(ConfigOpts),
Check, Check,
} }
@ -85,14 +82,14 @@ fn main() -> Result<(), anyhow::Error> {
env_logger::init(); env_logger::init();
let opts = Opts::parse(); let opts = Opts::parse();
match &opts.command { match &opts.command {
Command::Endpoints(list_opts) => list(&list_opts)?, Command::Endpoints => list(&opts)?,
Command::Config(get_config_opts) => get_config(&get_config_opts)?, Command::Config(get_config_opts) => get_config(&opts, &get_config_opts)?,
Command::Check => check()?, Command::Check => check(&opts)?,
} }
Ok(()) Ok(())
} }
fn list(opts: &EndpointsOpts) -> Result<(), anyhow::Error> { fn list(opts: &Opts) -> Result<(), anyhow::Error> {
let locations: Locations = get_locations()?; let locations: Locations = get_locations()?;
if opts.json { if opts.json {
println!("{}", serde_json::to_string(&locations)?); println!("{}", serde_json::to_string(&locations)?);
@ -117,20 +114,20 @@ fn get_locations() -> Result<Locations, anyhow::Error> {
Ok(locations) Ok(locations)
} }
fn get_config(opts: &ConfigOpts) -> Result<(), anyhow::Error> { fn get_config(_opts: &Opts, config_opts: &ConfigOpts) -> Result<(), anyhow::Error> {
let locations = get_locations()?; let locations = get_locations()?;
let location = locations let location = locations
.locations .locations
.iter() .iter()
.find(|location| location.name == opts.location) .find(|location| location.name == config_opts.location)
.ok_or_else(|| anyhow::anyhow!("no such location: {}", opts.location))?; .ok_or_else(|| anyhow::anyhow!("no such location: {}", config_opts.location))?;
debug!("location = {:?}", &location); debug!("location = {:?}", &location);
let keys = generage_keys()?; let keys = generage_keys()?;
debug!("keys = {:?}", &keys); debug!("keys = {:?}", &keys);
let config: WireguardConfig = ureq::post(&location.endpoints.wireguard) let config: WireguardConfig = ureq::post(&location.endpoints.wireguard)
.send_form(&[ .send_form(&[
("username", &opts.username), ("username", &config_opts.username),
("token", &opts.token), ("token", &config_opts.token),
("pubkey", &keys.public_key), ("pubkey", &keys.public_key),
])? ])?
.into_json()?; .into_json()?;
@ -154,12 +151,16 @@ AllowedIPs = 0.0.0.0/0 #, ::/0",
Ok(()) Ok(())
} }
fn check() -> Result<(), anyhow::Error> { fn check(opts: &Opts) -> Result<(), anyhow::Error> {
let url = format!("{}/check", BASE_URL); let url = format!("{}/check", BASE_URL);
let result: CheckResult = ureq::get(&url).call()?.into_json()?; let result: CheckResult = ureq::get(&url).call()?.into_json()?;
if opts.json {
println!("{}", serde_json::to_string(&result)?);
} else {
println!("Connected: {:?}", result.connected); println!("Connected: {:?}", result.connected);
println!("IP: {}", result.ip); println!("IP: {}", result.ip);
println!("Location: {}", result.location); println!("Location: {}", result.location);
}
Ok(()) Ok(())
} }