feat: Use setup subcommand to set up config instead of passing args to run

This commit is contained in:
Kiril Kovachev 2025-05-08 00:20:11 +01:00
parent cabc194158
commit 2fcbe535b9

View File

@ -15,9 +15,7 @@ struct Cli {
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)]
enum Action { enum Action {
/// Move config to ~/.config /// Move config to ~/.config
Setup, Setup {
/// Run the bot
Run {
/// 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,
@ -33,20 +31,20 @@ enum Action {
/// Specify the REST URL of the bot /// Specify the REST URL of the bot
#[arg(long, env = "MW_REST_URL")] #[arg(long, env = "MW_REST_URL")]
rest_url: String, rest_url: String,
}, },
/// Run the bot
Run,
} }
fn setup() -> Result<(), std::io::Error> { fn setup(action: Action) -> Result<(), std::io::Error> {
if let Ok(config_template) = std::fs::read_to_string("mwbot.toml") { if let Ok(config_template) = std::fs::read_to_string("mwbot.toml") {
match action {
let username = env::var("MW_USERNAME").expect("The .env file must have a \"MW_USERNAME\" attribute."); Action::Run => {panic!();}
let oauth2_token = env::var("MW_OAUTH2").expect("The .env file must have a \"MW_OAUTH2\" attribute."); Action::Setup { username, botpassword, oauth2_token, api_url, rest_url } => {
let botpassword: String = env::var("MW_BOTPASSWORD").expect("The .env file must have a \"MW_BOTPASSWORD\" attribute."); let filled_in_config = formatx!(config_template, username, botpassword, oauth2_token).unwrap();
std::fs::write(shellexpand::tilde("~/.config/mwbot.toml").into_owned(), filled_in_config).unwrap();
let filled_in_config = formatx!(config_template, username, botpassword, oauth2_token).unwrap(); }
std::fs::write(shellexpand::tilde("~/.config/mwbot.toml").into_owned(), filled_in_config).unwrap(); }
Ok(()) Ok(())
} else { } else {
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "Unable to find mwbot.toml; did you execute the script from the same directory?")) Err(std::io::Error::new(std::io::ErrorKind::NotFound, "Unable to find mwbot.toml; did you execute the script from the same directory?"))
@ -60,15 +58,12 @@ async fn main() -> Result<(), std::io::Error> {
let cli = Cli::parse(); let cli = Cli::parse();
match cli.action { match cli.action {
Action::Setup => { action @ Action::Setup {..} => {
setup()?; setup(action)?;
eprintln!("Successfully set up ~/.config/mwbot.toml.") eprintln!("Successfully set up ~/.config/mwbot.toml.")
}, },
Action::Run {username, botpassword, oauth2_token, api_url, rest_url} => { Action::Run => {
let bot = mwbot::Bot::builder(api_url, rest_url) let bot = mwbot::Bot::from_default_config().await.unwrap();
.set_botpassword(username, botpassword)
.build()
.await.unwrap();
let page = bot.page("Bulgaria").unwrap(); let page = bot.page("Bulgaria").unwrap();
let html = page.html().await.unwrap().into_mutable(); let html = page.html().await.unwrap().into_mutable();
println!("{:?}", html); println!("{:?}", html);