workmn 0.0.5

Manages the lifecycle of projects on the local machine
Documentation
use clap::ArgMatches;
use failure::Error;
use log::warn;
use workmn::conf::{Conf, ConfFolderLocation};

pub type ActionResult = Result<(), Error>;

pub struct ActionContext<'a> {
    pub matches: &'a ArgMatches<'a>,

    // plan to allow arbitrary config dirs later
    #[allow(dead_code)]
    parent_matches: &'a ArgMatches<'a>,
}

impl<'a> ActionContext<'a> {
    pub fn new(parent_matches: &'a ArgMatches, matches: Option<&'a ArgMatches>) -> Self {
        Self {
            parent_matches,
            matches: matches.expect("Creating an ActionContext without arg matches"),
        }
    }

    pub fn unwrap_conf(&self) -> Conf {
        // TODO: allow overrides via `parent_matches`
        let config_location = ConfFolderLocation::default();

        match Conf::load(&config_location) {
            Ok(conf) => {
                if !conf.load_errors().is_empty() {
                    warn!("some errors occurred loading the configuration:");
                    for err in conf.load_errors() {
                        warn!("{}", err);
                    }
                }
                conf
            }
            Err(e) => {
                panic!("Unable to load config file: {}", e);
            }
        }
    }
}

pub trait CliAction {
    fn dispatch(context: ActionContext) -> ActionResult;

    fn run(&self, context: ActionContext) -> ActionResult {
        Self::dispatch(context)
    }
}