#[derive(clap::Parser)]
#[clap(author, version, long_about = None)]
#[clap(about = "Yet Another rolesanywhere-credential-helper")]
#[clap(propagate_version = true)]
struct Cli {
#[clap(long, global = true)]
url: Option<String>,
#[clap(long, global = true)]
configuration_directory: Option<String>,
#[clap(subcommand)]
command: Commands,
}
#[derive(clap::Subcommand)]
enum Commands {
CredentialProcess(needroleshere::cmd::credential_process::CredentialProcessArgs),
Bind(needroleshere::cmd::bind::BindArgs),
Unbind(needroleshere::cmd::unbind::UnbindArgs),
Serve(needroleshere::cmd::serve::ServeArgs),
}
impl TryInto<needroleshere::config::Config> for &Cli {
type Error = needroleshere::error::Error;
fn try_into(self) -> Result<needroleshere::config::Config, Self::Error> {
needroleshere::config::Config::new(
self.configuration_directory.clone().map(|v| v.into()),
needroleshere::config::ConfigData {
url: self.url.clone(),
},
)
}
}
fn main() -> Result<(), anyhow::Error> {
use clap::Parser;
let cli = Cli::parse();
match &cli.command {
Commands::CredentialProcess(params) => {
enable_tracing(true);
needroleshere::cmd::credential_process::run(params)?;
}
Commands::Bind(params) => {
enable_tracing(false);
needroleshere::cmd::bind::run(&(&cli).try_into()?, params)?;
}
Commands::Unbind(params) => {
enable_tracing(false);
needroleshere::cmd::unbind::run(&(&cli).try_into()?, params)?;
}
Commands::Serve(params) => {
enable_tracing(false);
needroleshere::cmd::serve::run(&(&cli).try_into()?, params)?;
}
};
Ok(())
}
fn enable_tracing(stderr: bool) {
if stderr {
tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
} else {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "info");
}
tracing_subscriber::fmt::init();
}
}