Compare commits

...

5 Commits

2 changed files with 21 additions and 11 deletions

View File

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

View File

@ -26,7 +26,7 @@ struct SetupArgs {
username: String,
/// Specify the botpassword of the bot
#[arg(long, env = "MW_BOTPASSWORD")]
botpassword: String,
botpassword: Option<String>,
/// Specify the OAuth2 token of the bot
#[arg(long, env = "MW_OAUTH2")]
oauth2_token: String,
@ -47,16 +47,26 @@ enum Action {
Run,
}
fn read_config_template() -> Result<String, std::io::Error> {
std::fs::read_to_string(CONFIG_TEMPLATE_PATH).or(Err(std::io::Error::new(std::io::ErrorKind::NotFound, format!("Unable to find {}; did you execute the script from the same directory?", CONFIG_TEMPLATE_PATH))))
}
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()
}
// Fix permissions on UNIX-like systems, since mwbot-rs doesn't like to read configs with loose permissions.
fn constrain_unix_permissions(path: &PathBuf) -> Result<(), std::io::Error> {
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))
}
fn setup(args: SetupArgs) -> Result<(), std::io::Error> {
let config_template = std::fs::read_to_string(CONFIG_TEMPLATE_PATH).or(Err(std::io::Error::new(std::io::ErrorKind::NotFound, format!("Unable to find {}; did you execute the script from the same directory?", CONFIG_TEMPLATE_PATH))))?;
let filled_in_config = formatx!(config_template, args.api_url, args.rest_url, args.username, args.botpassword, args.oauth2_token).unwrap();
let config_template = read_config_template()?;
let filled_in_config = fill_config_template(config_template, args);
let path = get_bot_config_path();
std::fs::write(&path, filled_in_config).unwrap();
// Fix permissions on UNIX-like systems, since mwbot-rs doesn't like to read configs with loose permissions.
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?;
}
std::fs::write(&path, filled_in_config)?;
constrain_unix_permissions(&path)?;
Ok(())
}