From 1400334ce013bfdf37b0fa8cc87361003220bb9e Mon Sep 17 00:00:00 2001 From: Kiril Kovachev Date: Fri, 9 May 2025 12:24:10 +0100 Subject: [PATCH] feat: Now allow supplying either password or OAuth2 token in config generation --- mwbot_template.toml | 2 -- src/main.rs | 28 +++++++++++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/mwbot_template.toml b/mwbot_template.toml index 9a7d13f..085391d 100644 --- a/mwbot_template.toml +++ b/mwbot_template.toml @@ -4,5 +4,3 @@ rest_url = "{}" [auth] username = "{}" -# password = "{}" -oauth2_token = "{}" diff --git a/src/main.rs b/src/main.rs index d62b730..fc8e500 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,11 @@ struct Cli { #[derive(clap::Args, Debug, Clone, Eq, PartialEq, PartialOrd, Ord)] #[group(required = true, multiple = false)] struct AuthMethodParse { + /// Specify the password of the bot + #[arg(long, env = "MW_BOTPASSWORD")] password: Option, + /// Specify the OAuth2 token of the bot + #[arg(long, env = "MW_OAUTH2")] oauth2_token: Option } @@ -45,12 +49,11 @@ 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: Option, - /// Specify the OAuth2 token of the bot - #[arg(long, env = "MW_OAUTH2")] - oauth2_token: String, + + /// Specify the password or OAuth2 key for the bot + #[clap(flatten)] + auth_phrase: AuthMethodParse, + /// Specify the API URL of the bot #[arg(long, env = "MW_API_URL")] api_url: String, @@ -73,7 +76,18 @@ fn read_config_template() -> Result { } fn fill_config_template(config_template: String, args: SetupArgs) -> String { - formatx!(config_template, args.api_url, args.rest_url, args.username, args.botpassword.unwrap_or("".into()), args.oauth2_token).unwrap() + let mut filled = formatx!(config_template, args.api_url, args.rest_url, args.username).unwrap(); + let chosen_method: AuthMethod = args.auth_phrase.into(); + match chosen_method { + AuthMethod::Password(password) => { + filled.push_str(format!("password = \"{}\"", password).as_str()); + }, + AuthMethod::OAuth2Token(oauth2_token) => { + filled.push_str(format!("oauth2_token = \"{}\"", oauth2_token).as_str()); + } + } + filled.push('\n'); + filled } // Fix permissions on UNIX-like systems, since mwbot-rs doesn't like to read configs with loose permissions.