Compare commits
3 Commits
b4b605861a
...
1400334ce0
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1400334ce0 | ||
![]() |
357e012525 | ||
![]() |
9c7bcc8f99 |
@ -4,5 +4,3 @@ rest_url = "{}"
|
||||
|
||||
[auth]
|
||||
username = "{}"
|
||||
# password = "{}"
|
||||
oauth2_token = "{}"
|
||||
|
51
src/main.rs
51
src/main.rs
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user