feat: Use setup subcommand to set up config instead of passing args to run
This commit is contained in:
parent
cabc194158
commit
2fcbe535b9
35
src/main.rs
35
src/main.rs
@ -15,9 +15,7 @@ struct Cli {
|
||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Subcommand)]
|
||||
enum Action {
|
||||
/// Move config to ~/.config
|
||||
Setup,
|
||||
/// Run the bot
|
||||
Run {
|
||||
Setup {
|
||||
/// Specify the username of the bot
|
||||
#[arg(long, env = "MW_USERNAME")]
|
||||
username: String,
|
||||
@ -33,20 +31,20 @@ enum Action {
|
||||
/// Specify the REST URL of the bot
|
||||
#[arg(long, env = "MW_REST_URL")]
|
||||
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") {
|
||||
|
||||
let username = env::var("MW_USERNAME").expect("The .env file must have a \"MW_USERNAME\" attribute.");
|
||||
let oauth2_token = env::var("MW_OAUTH2").expect("The .env file must have a \"MW_OAUTH2\" attribute.");
|
||||
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();
|
||||
|
||||
match action {
|
||||
Action::Run => {panic!();}
|
||||
Action::Setup { username, botpassword, oauth2_token, api_url, rest_url } => {
|
||||
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(())
|
||||
} else {
|
||||
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();
|
||||
|
||||
match cli.action {
|
||||
Action::Setup => {
|
||||
setup()?;
|
||||
action @ Action::Setup {..} => {
|
||||
setup(action)?;
|
||||
eprintln!("Successfully set up ~/.config/mwbot.toml.")
|
||||
},
|
||||
Action::Run {username, botpassword, oauth2_token, api_url, rest_url} => {
|
||||
let bot = mwbot::Bot::builder(api_url, rest_url)
|
||||
.set_botpassword(username, botpassword)
|
||||
.build()
|
||||
.await.unwrap();
|
||||
Action::Run => {
|
||||
let bot = mwbot::Bot::from_default_config().await.unwrap();
|
||||
let page = bot.page("Bulgaria").unwrap();
|
||||
let html = page.html().await.unwrap().into_mutable();
|
||||
println!("{:?}", html);
|
||||
|
Loading…
Reference in New Issue
Block a user