Compare commits

..

No commits in common. "1400334ce013bfdf37b0fa8cc87361003220bb9e" and "b4b605861a6879e3a211dc2c94efb08023299d99" have entirely different histories.

2 changed files with 10 additions and 43 deletions

View File

@ -4,3 +4,5 @@ rest_url = "{}"
[auth] [auth]
username = "{}" username = "{}"
# password = "{}"
oauth2_token = "{}"

View File

@ -19,41 +19,17 @@ struct Cli {
action: Action, action: Action,
} }
#[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<String>,
/// Specify the OAuth2 token of the bot
#[arg(long, env = "MW_OAUTH2")]
oauth2_token: Option<String>
}
enum AuthMethod {
Password(String),
OAuth2Token(String)
}
impl From<AuthMethodParse> for AuthMethod {
fn from (parsed_args: AuthMethodParse) -> AuthMethod {
match parsed_args.password {
None => Self::OAuth2Token(parsed_args.oauth2_token.unwrap()),
Some(password) => Self::Password(password)
}
}
}
#[derive(Parser, Clone, Eq, PartialEq, PartialOrd, Ord)] #[derive(Parser, Clone, Eq, PartialEq, PartialOrd, Ord)]
struct SetupArgs { struct SetupArgs {
/// Specify the username of the bot /// Specify the username of the bot
#[arg(long, env = "MW_USERNAME")] #[arg(long, env = "MW_USERNAME")]
username: String, username: String,
/// Specify the botpassword of the bot
/// Specify the password or OAuth2 key for the bot #[arg(long, env = "MW_BOTPASSWORD")]
#[clap(flatten)] botpassword: Option<String>,
auth_phrase: AuthMethodParse, /// Specify the OAuth2 token of the bot
#[arg(long, env = "MW_OAUTH2")]
oauth2_token: String,
/// Specify the API URL of the bot /// Specify the API URL of the bot
#[arg(long, env = "MW_API_URL")] #[arg(long, env = "MW_API_URL")]
api_url: String, api_url: String,
@ -65,7 +41,7 @@ struct SetupArgs {
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)]
enum Action { enum Action {
/// Output a bot config to system's config directory /// Move config to ~/.config
Setup(SetupArgs), Setup(SetupArgs),
/// Run the bot /// Run the bot
Run, Run,
@ -76,18 +52,7 @@ fn read_config_template() -> Result<String, std::io::Error> {
} }
fn fill_config_template(config_template: String, args: SetupArgs) -> String { fn fill_config_template(config_template: String, args: SetupArgs) -> String {
let mut filled = formatx!(config_template, args.api_url, args.rest_url, args.username).unwrap(); formatx!(config_template, args.api_url, args.rest_url, args.username, args.botpassword.unwrap_or("".into()), args.oauth2_token).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. // Fix permissions on UNIX-like systems, since mwbot-rs doesn't like to read configs with loose permissions.