use clap::{Parser, Subcommand, ValueEnum};
use eos_eapi::*;
#[derive(Subcommand)]
enum Commands {
Uds {
#[arg(required = true)]
commands: Vec<String>,
},
Http {
hostname: String,
#[arg(short, long, requires = "password")]
user: Option<String>,
#[arg(short, long, requires = "user")]
password: Option<String>,
#[arg(required = true)]
commands: Vec<String>,
},
Https {
#[arg(short = 'k', long)]
insecure: bool,
hostname: String,
#[arg(short, long, requires = "password")]
user: Option<String>,
#[arg(short, long, requires = "user")]
password: Option<String>,
#[arg(required = true)]
commands: Vec<String>,
},
}
#[derive(ValueEnum, Clone)]
enum Format {
Json,
Text,
}
#[derive(Parser)]
struct Args {
#[command(subcommand)]
subcmd: Commands,
#[arg(short, long, default_value = "text")]
format: Format,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let args = Args::parse();
let format = match args.format {
Format::Json => ResultFormat::Json,
Format::Text => ResultFormat::Text,
};
let result = match args.subcmd {
Commands::Uds { commands } => ClientBuilder::unix_socket()
.build_blocking()?
.run(&commands, format)?,
Commands::Http {
hostname,
user,
password,
commands,
} => if let (Some(user), Some(password)) = (user, password) {
ClientBuilder::http(hostname).set_authentication(user, password)
} else {
ClientBuilder::http(hostname)
}
.build_blocking()
.run(&commands, format)?,
Commands::Https {
insecure,
hostname,
user,
password,
commands,
} => if let (Some(user), Some(password)) = (user, password) {
ClientBuilder::http(hostname).set_authentication(user, password)
} else {
ClientBuilder::http(hostname)
}
.enable_https()
.set_insecure(insecure)
.build_blocking()
.run(&commands, format)?,
};
match result {
Response::Result(v) => println!("{v:?}"),
Response::Error {
message,
code,
errors,
} => println!("error code: {code}, message: {message}, outputs: {errors:#?}"),
};
Ok(())
}