1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! This module contains the configuration of the application.
//!
//! All options are passed individually to each function and are not bundled together.
//!
//! # Examples
//!
//! ```no_run
//! # use dishub::options::Options;
//! let options = Options::parse();
//! println!("Config directory: {}", options.config_dir.0);
//! ```


use clap::{self, App, SubCommand, Arg, AppSettings};
use std::time::Duration;
use std::path::PathBuf;
use std::env::home_dir;
use std::str::FromStr;
use regex::Regex;
use std::fs;


lazy_static! {
    static ref SLEEP_RGX: Regex = Regex::new(r"(\d+)s").unwrap();
}


/// All possible subsystems, think `cargo`'s or `git`'s.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum Subsystem {
    /// Initialise global app data
    Init {
        /// Whether to override current app configuration. Default: `false`
        force: bool,
    },
    /// Add feeds to post to servers
    AddFeeds,
    /// Unsubscribe from selected followed feeds
    UnfollowFeeds,
    /// Run the activity-posting daemon
    StartDaemon {
        /// How long to sleep between each iteration. Default: 1 minute
        sleep: Duration,
    },
}


/// Representation of the application's all configurable values.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Options {
    /// Directory containing configuration. Default: `"$HOME/.dishub"`
    pub config_dir: (String, PathBuf),
    /// The specified subsystem.
    pub subsystem: Subsystem,
}

impl Options {
    /// Parse `env`-wide command-line arguments into an `Options` instance
    pub fn parse() -> Options {
        let matches = App::new("dishub")
            .version(crate_version!())
            .author(crate_authors!())
            .setting(AppSettings::ColoredHelp)
            .setting(AppSettings::VersionlessSubcommands)
            .setting(AppSettings::SubcommandRequiredElseHelp)
            .about("Rust app for posting GitHub activity on Discord")
            .arg(Arg::from_usage("-c --config-dir=[CONFIG_DIR] 'Directory containing configuration. Default: $HOME/.dishub'")
                .validator(Options::config_dir_validator))
            .subcommand(SubCommand::with_name("init")
                .about("Initialise global app data")
                .arg(Arg::from_usage("-f --force 'Override current app configuration'")))
            .subcommand(SubCommand::with_name("add-feeds").about("Add feeds to post to servers"))
            .subcommand(SubCommand::with_name("unfollow-feeds").about("Unsubscribe from selected followed feeds"))
            .subcommand(SubCommand::with_name("start-daemon")
                .about("Run the activity-posting daemon")
                .arg(Arg::from_usage("-s --sleep=[SLEEP_TIME] 'Time to sleep between each iteration'")
                    .default_value("60s")
                    .validator(Options::sleep_validator)))
            .get_matches();

        Options {
            config_dir: match matches.value_of("config-dir") {
                Some(dirs) => (dirs.to_string(), fs::canonicalize(dirs).unwrap()),
                None => {
                    match home_dir() {
                        Some(mut hd) => {
                            hd = hd.canonicalize().unwrap();
                            hd.push(".dishub");

                            fs::create_dir_all(&hd).unwrap();
                            ("$HOME/.dishub".to_string(), hd)
                        }
                        None => {
                            clap::Error {
                                    message: "Couldn't automatically get home directory, please specify configuration directory with the -c option".to_string(),
                                    kind: clap::ErrorKind::MissingRequiredArgument,
                                    info: None,
                                }
                                .exit()
                        }
                    }
                }
            },
            subsystem: match matches.subcommand() {
                ("init", Some(init_matches)) => Subsystem::Init { force: init_matches.is_present("force") },
                ("add-feeds", _) => Subsystem::AddFeeds,
                ("unfollow-feeds", _) => Subsystem::UnfollowFeeds,
                ("start-daemon", Some(start_daemon_matches)) => {
                    Subsystem::StartDaemon { sleep: Duration::from_secs(Options::parse_sleep(start_daemon_matches.value_of("sleep").unwrap()).unwrap()) }
                }
                _ => panic!("No subcommand passed"),
            },
        }
    }

    fn parse_sleep(s: &str) -> Option<u64> {
        SLEEP_RGX.captures(s).map(|c| u64::from_str(c.at(1).unwrap()).unwrap())
    }

    fn config_dir_validator(s: String) -> Result<(), String> {
        fs::canonicalize(&s).map(|_| ()).map_err(|_| format!("Configuration directory \"{}\" not found", s))
    }

    fn sleep_validator(s: String) -> Result<(), String> {
        match Options::parse_sleep(&s) {
            None => Err(format!("\"{}\" is not a valid sleep duration (in format \"NNNs\")", s)),
            Some(_) => Ok(()),
        }
    }
}