use clap::{Arg, ArgGroup, Command};
use toggl_api::Credentials;
pub(crate) struct Config {
pub(crate) credentials: Credentials,
}
impl Config {
pub(crate) fn parse() -> Self {
let app = Command::new(clap::crate_name!())
.version(clap::crate_version!())
.arg(
Arg::new("api-token")
.long("api-token")
.env("TOGGL_API_TOKEN")
.value_name("TOKEN")
.conflicts_with("password")
.takes_value(true),
)
.arg(
Arg::new("username")
.long("username")
.value_name("USERNAME")
.requires("password")
.takes_value(true),
)
.arg(
Arg::new("password")
.long("password")
.value_name("PASSWORD")
.takes_value(true),
)
.group(
ArgGroup::new("user-id")
.required(true)
.args(&["api-token", "username"]),
);
let matches = app.get_matches();
let credentials = if let Some(api_token) = matches.value_of("api-token") {
Credentials::ApiToken(api_token.to_string())
} else {
let username = matches.value_of("username").unwrap().to_string();
let password = matches.value_of("password").unwrap().to_string();
Credentials::Password { username, password }
};
Self { credentials }
}
}