Compare commits

...

3 Commits

2 changed files with 43 additions and 10 deletions

View File

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

View File

@ -19,17 +19,41 @@ struct Cli {
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)]
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<String>,
/// 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,
@ -41,7 +65,7 @@ struct SetupArgs {
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)]
enum Action {
/// Move config to ~/.config
/// Output a bot config to system's config directory
Setup(SetupArgs),
/// Run the bot
Run,
@ -52,7 +76,18 @@ fn read_config_template() -> Result<String, std::io::Error> {
}
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.