use anyhow::anyhow;
use clap::Parser;
use gh_config::{retrieve_token_secure, Config, Hosts};
use std::path::PathBuf;
#[derive(Debug, clap::Subcommand)]
enum ConfigAction {
Show,
}
#[derive(Debug, clap::Subcommand)]
enum AuthnAction {
List,
Get {
host: String,
#[clap(long)]
token_only: bool,
#[clap(long)]
secure: bool,
},
}
#[derive(Debug, clap::Subcommand)]
enum Location {
Config {
#[clap(subcommand)]
action: ConfigAction,
},
Authn {
#[clap(subcommand)]
action: AuthnAction,
},
}
#[derive(Debug, clap::Parser)]
#[clap(author, version, about, long_about = None)]
struct Args {
#[clap(subcommand)]
location: Location,
#[clap(short, long)]
json: bool,
path: Option<PathBuf>,
}
macro_rules! output {
($args: expr, $value: expr) => {
Ok::<(), anyhow::Error>(print!(
"{}",
match $args.json {
true => serde_json::to_string($value)?,
false => serde_yaml::to_string($value)?,
}
))
};
}
fn run() -> Result<(), anyhow::Error> {
let args: Args = Parser::parse();
match args.location {
Location::Config { action } => {
let config = match args.path {
Some(p) => Config::load_from(p),
_ => Config::load(),
}?;
match action {
ConfigAction::Show => output!(args, &config)?,
}
}
Location::Authn { action } => {
let hosts = match args.path {
Some(p) => Hosts::load_from(p),
_ => Hosts::load(),
}?;
match action {
AuthnAction::List => output!(args, &hosts)?,
AuthnAction::Get {
host,
token_only,
secure,
} => match hosts.get(&host) {
Some(h) => match token_only {
true => match match secure {
true => retrieve_token_secure(&host),
_ => hosts.retrieve_token(&host),
}? {
Some(t) => print!("{}", t),
_ => return Err(anyhow!("Token was not found for the host.")),
},
_ => output!(args, &h)?,
},
_ => Err(anyhow!(
"The specified host not found in the configuration."
))?,
},
}
}
}
Ok(())
}
fn main() {
if let Err(e) = run() {
eprintln!("ERROR: {}", e);
std::process::exit(1);
}
}