nedots 0.1.3

A tool to manage configuration files/(ne)dots.
//! # cmd module.

pub(crate) mod backup;
pub(crate) mod clean;
pub(crate) mod gather;
pub(crate) mod install;
pub(crate) mod nedots;
pub(crate) mod restore;
pub(crate) mod sync;

use crate::config::{read as read_cfg, Config};
use std::path::Path;

pub(crate) trait Command {
    /// Initialize the Command by using config args to find & read in the config
    /// file.
    fn init(&self, args: &nedots::RootCmd) -> anyhow::Result<Config> {
        let mut config: Config;
        if let Some(cfg_path) = &args.cfg_path {
            config = read_cfg(cfg_path)?;
        } else {
            let path = Path::new(&args.root).join(Path::new(&args.cfg_file));
            config = read_cfg(&path)?;
            config.root = Path::new(&args.root).to_path_buf();
        }

        if let Some(dots_dir) = &args.dots {
            config.dots_dir = Path::new(dots_dir).to_path_buf();
        }

        if let Some(backup_dir) = &args.backups {
            config.dots_dir = Path::new(backup_dir).to_path_buf();
        }

        log::debug!("{:#?}", config);
        Ok(config.resolve_paths())
    }

    /// Run the Command - this is the only function that implementors need to
    /// implement.
    fn run(&self, _: &nedots::RootCmd, _: &crate::config::Config) -> anyhow::Result<()>;

    /// Executes init & run.
    fn exec(&self, args: &nedots::RootCmd) -> anyhow::Result<()> {
        self.run(args, &self.init(args)?)
    }
}