nedots 0.1.2

A tool to manage configuration files/(ne)dots.
use std::path::PathBuf;

#[derive(Debug, clap::Parser)]
#[command(author, about, version, arg_required_else_help(true))]
pub(crate) struct RootCmd {
    #[command(flatten)]
    pub(crate) verbose: clap_verbosity_flag::Verbosity,

    #[arg(short, long, default_value = ".nedots")]
    /// Root directory.
    pub(crate) root: String,

    #[arg(short, long = "config", default_value = ".nedots.yml")]
    /// Custom config file.
    pub(crate) cfg_file: String,

    #[arg(short = 'C', long = "configpath")]
    /// Path to config file, if provided root & config are discarded.
    pub(crate) cfg_path: Option<PathBuf>,

    #[arg(short, long)]
    /// Where to store config files/(ne)dots.
    pub(crate) dots: Option<PathBuf>,

    #[arg(short, long)]
    /// Where to store backups of config files/(ne)dots.
    pub(crate) backups: Option<PathBuf>,

    #[command(subcommand)]
    cmd: Option<Commands>,
}

#[derive(Debug, clap::Subcommand)]
pub(crate) enum Commands {
    Gather(super::gather::GatherCmd),
    Backup(super::backup::BackupCmd),
    Clean(super::clean::CleanCmd),
    Install(super::install::InstallCmd),
    Restore(super::restore::RestoreCmd),
}

impl super::Command for RootCmd {
    fn init(&self, _: &self::RootCmd) -> Result<crate::config::Config, super::CommandError> {
        Ok(crate::config::Config::default())
    }

    fn run(
        &self,
        args: &RootCmd,
        _config: &crate::config::Config,
    ) -> Result<(), super::CommandError> {
        if let Some(cmd) = &self.cmd {
            match cmd {
                Commands::Backup(backup_cmd) => {
                    return backup_cmd.exec(args);
                }
                Commands::Clean(clean_cmd) => {
                    return clean_cmd.exec(args);
                }
                Commands::Gather(gather_cmd) => {
                    return gather_cmd.exec(args);
                }
                Commands::Install(install_cmd) => {
                    return install_cmd.exec(args);
                }
                Commands::Restore(restore_cmd) => {
                    return restore_cmd.exec(args);
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod test {
    use clap::CommandFactory;

    #[test]
    fn verify() {
        super::RootCmd::command().debug_assert()
    }
}