mwkeep 0.0.1

A tool for house keeping MediaWiki sites.
//! Command line parameter type

use std::path::{Path, PathBuf};

/// Command line parameter type
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Param {
    data_dir: PathBuf,
    verbose: bool,
    dry_run: bool,
}

impl Param {
    pub fn new() -> Self {
        let args: Vec<String> = std::env::args().collect();
        let mut opts = getopts::Options::new();
        opts.optflag("h", "help", "show help message");
        opts.optflag("v", "verbose", "show verbose messages");
        opts.optflag("t", "dry-run", "dry run");
        opts.optopt("d", "data-dir", "set data directory path", "DATA_DIR");
        let matches = match opts.parse(&args[1..]) {
            Ok(m) => m,
            Err(f) => panic!("{}", f.to_string()),
        };

        if matches.opt_present("h") {
            println!("MwKeep - A bot for housekeeping MediaWiki instances");
            println!();
            println!("Usage: {} [OPTIONS]", args[0]);
            println!();
            println!("Options:");
            println!("  -h, --help : Show help message (this message)");
            println!("  -v, --verbose : Show verbose messages during execution");
            println!("  -t, --dry-run : Dry run");
            println!("  -d=DATA_DIR, --data-dir=DATA_DIR : Set data directory path to DIRECTORY");
            println!("Default DATA_DIR is current directory.");
            println!();
            println!("Configuration:");
            println!("  Configuration file is located at DATA_DIR/config.toml .");
            println!("  Example config.toml is as follows:");
            println!();
            println!("username = \"UserBot@HouseKeeping\"");
            println!("password = \"(secret)\"");
            println!("domains = [ \"example1.miraheze.org\", \"example2.miraheze.org\" ]");
            println!("keep_alive_title = \"KeepAlive\"");
            println!("keep_alive_text = \"keep alive --~~~~\"");
            println!("keep_alive_summary = \"keep alive\"");
            println!("keep_alive_days = 30");
            println!("delete_title = \"DeletedPage\"");
            println!("keep_days = 14");
            println!();
            println!("  By this configuration:");
            println!("    * This bot write to page KeepAlive if 30 days passed since last edit on the wiki instance.");
            println!("    * This bot delete pages which content is exactly same as [[DeletedPage]] if the page is not edited for 14 days.");
            println!();
            println!("  Execute this bot dayly by cron, for example setting:");
            println!("0 3 * * * cd /home/user/mwkeep && /home/user/mwkeep/target/release/mwkeep");
            println!();
            println!("  Note that application state is saved to DATA_DIR/state.toml .");

            std::process::exit(0)
        }

        let verbose = matches.opt_present("v");

        let dry_run = matches.opt_present("t");

        let data_dir = if let Some(data_dir) = matches.opt_str("d") {
            Path::new(&data_dir).to_path_buf()
        } else {
            match std::env::current_dir() {
                Ok(dir) => dir,
                Err(err) => panic!("{}", err),
            }
        };

        Self {
            data_dir,
            verbose,
            dry_run,
        }
    }

    pub fn data_dir(&self) -> &Path {
        self.data_dir.as_path()
    }

    pub fn verbose(&self) -> bool {
        self.verbose
    }

    pub fn dry_run(&self) -> bool {
        self.dry_run
    }
}