use crate::cmd::{link, relink, unlink};
use clap::{
crate_authors, crate_description, crate_name, crate_version, AppSettings, Parser, Subcommand,
};
use tuck::{TuckDir, TuckErr};
use std::path::PathBuf;
pub trait Command {
fn exec(self, config: Config) -> Result<(), TuckErr>;
}
#[derive(Debug, Parser)]
pub struct Config {
#[clap(short = 'd', long, display_order = 0)]
pub dir: TuckDir,
#[clap(short = 't', long, display_order = 1)]
pub target: PathBuf,
#[clap(short = 'D', long)]
pub dotfiles: bool,
}
#[derive(Debug, Subcommand)]
pub enum SubCmd {
#[clap(display_order = 0)]
Link(link::Link),
#[clap(display_order = 1)]
Unlink(unlink::Unlink),
#[clap(display_order = 2)]
Relink(relink::Relink),
}
impl Command for SubCmd {
fn exec(self, config: Config) -> Result<(), TuckErr> {
match self {
Self::Link(x) => x.exec(config),
Self::Unlink(x) => x.exec(config),
Self::Relink(x) => x.exec(config),
}
}
}
#[derive(Debug, Parser)]
#[clap(
name = crate_name!(),
version = crate_version!(),
about = crate_description!(),
author = crate_authors!(),
setting = AppSettings::DisableHelpSubcommand,
setting = AppSettings::SubcommandRequired,
)]
pub struct Cli {
#[clap(subcommand)]
pub cmd: SubCmd,
#[clap(flatten)]
pub config: Config,
}
impl Cli {
pub fn new() -> Self {
Self::parse()
}
}