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,10 +12,8 @@ struct Cli {
action: Action,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)]
enum Action {
/// Move config to ~/.config
Setup {
#[derive(Parser, Clone, Eq, PartialEq, PartialOrd, Ord)]
struct SetupArgs {
/// Specify the username of the bot
#[arg(long, env = "MW_USERNAME")]
username: String,
@ -31,20 +29,21 @@ enum Action {
/// Specify the REST URL of the bot
#[arg(long, env = "MW_REST_URL")]
rest_url: String,
},
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)]
enum Action {
/// Move config to ~/.config
Setup(SetupArgs),
/// Run the bot
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") {
match action {
Action::Run => {panic!();}
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();
let filled_in_config = formatx!(config_template, args.api_url, args.rest_url, args.username, args.botpassword, args.oauth2_token).unwrap();
std::fs::write(shellexpand::tilde(BOT_CONFIG_PATH).into_owned(), filled_in_config).unwrap();
}
}
Ok(())
} else {
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();
match cli.action {
action @ Action::Setup {..} => {
setup(action)?;
Action::Setup(args) => {
setup(args)?;
eprintln!("Successfully set up {BOT_CONFIG_PATH}.")
},
Action::Run => {