Compare commits
3 Commits
b4b605861a
...
1400334ce0
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1400334ce0 | ||
![]() |
357e012525 | ||
![]() |
9c7bcc8f99 |
@ -4,5 +4,3 @@ rest_url = "{}"
|
|||||||
|
|
||||||
[auth]
|
[auth]
|
||||||
username = "{}"
|
username = "{}"
|
||||||
# password = "{}"
|
|
||||||
oauth2_token = "{}"
|
|
||||||
|
51
src/main.rs
51
src/main.rs
@ -19,17 +19,41 @@ 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
|
|
||||||
#[arg(long, env = "MW_BOTPASSWORD")]
|
/// Specify the password or OAuth2 key for the bot
|
||||||
botpassword: Option<String>,
|
#[clap(flatten)]
|
||||||
/// Specify the OAuth2 token of the bot
|
auth_phrase: AuthMethodParse,
|
||||||
#[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,
|
||||||
@ -41,7 +65,7 @@ struct SetupArgs {
|
|||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)]
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)]
|
||||||
enum Action {
|
enum Action {
|
||||||
/// Move config to ~/.config
|
/// Output a bot config to system's config directory
|
||||||
Setup(SetupArgs),
|
Setup(SetupArgs),
|
||||||
/// Run the bot
|
/// Run the bot
|
||||||
Run,
|
Run,
|
||||||
@ -52,7 +76,18 @@ 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 {
|
||||||
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.
|
// 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