refactor: Factor arguments of Setup variant into their own struct and remove prior janky matching

This commit is contained in:
Kiril Kovachev 2025-05-08 00:37:35 +01:00
parent a6e9bce223
commit a15b23a70a

View File

@ -12,39 +12,38 @@ struct Cli {
action: Action, action: Action,
} }
#[derive(Parser, Clone, Eq, PartialEq, PartialOrd, Ord)]
struct SetupArgs {
/// Specify the username of the bot
#[arg(long, env = "MW_USERNAME")]
username: String,
/// Specify the botpassword of the bot
#[arg(long, env = "MW_BOTPASSWORD")]
botpassword: String,
/// Specify the OAuth2 token of the bot
#[arg(long, env = "MW_OAUTH2")]
oauth2_token: String,
/// Specify the API URL of the bot
#[arg(long, env = "MW_API_URL")]
api_url: String,
/// Specify the REST URL of the bot
#[arg(long, env = "MW_REST_URL")]
rest_url: String,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)]
enum Action { enum Action {
/// Move config to ~/.config /// Move config to ~/.config
Setup { Setup(SetupArgs),
/// Specify the username of the bot
#[arg(long, env = "MW_USERNAME")]
username: String,
/// Specify the botpassword of the bot
#[arg(long, env = "MW_BOTPASSWORD")]
botpassword: String,
/// Specify the OAuth2 token of the bot
#[arg(long, env = "MW_OAUTH2")]
oauth2_token: String,
/// Specify the API URL of the bot
#[arg(long, env = "MW_API_URL")]
api_url: String,
/// Specify the REST URL of the bot
#[arg(long, env = "MW_REST_URL")]
rest_url: String,
},
/// Run the bot /// Run the bot
Run, Run,
} }
fn setup(action: Action) -> Result<(), std::io::Error> { fn setup(args: SetupArgs) -> Result<(), std::io::Error> {
if let Ok(config_template) = std::fs::read_to_string("mwbot.toml") { if let Ok(config_template) = std::fs::read_to_string("mwbot.toml") {
match action { let filled_in_config = formatx!(config_template, args.api_url, args.rest_url, args.username, args.botpassword, args.oauth2_token).unwrap();
Action::Run => {panic!();} std::fs::write(shellexpand::tilde(BOT_CONFIG_PATH).into_owned(), filled_in_config).unwrap();
Action::Setup { username, botpassword, oauth2_token, api_url, rest_url } => {
let filled_in_config = formatx!(config_template, api_url, rest_url, username, botpassword, oauth2_token).unwrap();
std::fs::write(shellexpand::tilde(BOT_CONFIG_PATH).into_owned(), filled_in_config).unwrap();
}
}
Ok(()) Ok(())
} else { } else {
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "Unable to find mwbot.toml; did you execute the script from the same directory?")) Err(std::io::Error::new(std::io::ErrorKind::NotFound, "Unable to find mwbot.toml; did you execute the script from the same directory?"))
@ -58,8 +57,8 @@ async fn main() -> Result<(), std::io::Error> {
let cli = Cli::parse(); let cli = Cli::parse();
match cli.action { match cli.action {
action @ Action::Setup {..} => { Action::Setup(args) => {
setup(action)?; setup(args)?;
eprintln!("Successfully set up {BOT_CONFIG_PATH}.") eprintln!("Successfully set up {BOT_CONFIG_PATH}.")
}, },
Action::Run => { Action::Run => {