Make --json option global
This commit is contained in:
parent
1a2a96911e
commit
4abc08a34b
1 changed files with 22 additions and 21 deletions
43
src/main.rs
43
src/main.rs
|
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
@ -57,7 +54,7 @@ struct Locations {
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
struct CheckResult {
|
struct CheckResult {
|
||||||
connected: bool,
|
connected: bool,
|
||||||
ip: String, // XXX
|
ip: String, // XXX
|
||||||
location: String,
|
location: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -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()?;
|
||||||
println!("Connected: {:?}", result.connected);
|
if opts.json {
|
||||||
println!("IP: {}", result.ip);
|
println!("{}", serde_json::to_string(&result)?);
|
||||||
println!("Location: {}", result.location);
|
} else {
|
||||||
|
println!("Connected: {:?}", result.connected);
|
||||||
|
println!("IP: {}", result.ip);
|
||||||
|
println!("Location: {}", result.location);
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue