nedots 0.1.2

A tool to manage configuration files/(ne)dots.
pub(crate) mod backup;
pub(crate) mod clean;
pub(crate) mod gather;
pub(crate) mod install;
pub(crate) mod nedots;
pub(crate) mod restore;

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

#[derive(Debug, thiserror::Error)]
pub(crate) enum CommandError {
    #[error(transparent)]
    ConfigError(#[from] crate::config::ConfigError),

    #[error(transparent)]
    IoError(#[from] std::io::Error),

    #[error(transparent)]
    StripPrefixError(#[from] std::path::StripPrefixError),

    #[error(transparent)]
    SystemTimeError(#[from] std::time::SystemTimeError),

    #[error(transparent)]
    TrashError(#[from] trash::Error),

    #[error(transparent)]
    ZipError(#[from] zip::result::ZipError),
}

pub(crate) trait Command {
    fn init(&self, args: &nedots::RootCmd) -> Result<Config, CommandError> {
        let mut config: Config;
        if let Some(cfg_path) = &args.cfg_path {
            config = read_cfg(&cfg_path)?;
        } else {
            config = read_cfg(&Path::new(&args.root).join(Path::new(&args.cfg_file)))?;
            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();
        }

        Ok(config.resolve_paths())
    }

    fn run(&self, _: &nedots::RootCmd, _: &crate::config::Config) -> Result<(), CommandError> {
        Ok(())
    }

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