use anyhow::Result;
use camino::Utf8PathBuf;
use clap::builder::styling::{AnsiColor, Effects, Styles};
use clap::{Parser, Subcommand};
use crate::cmd;
use crate::config::IconsMode;
const HELP_STYLES: Styles = Styles::styled()
.header(AnsiColor::BrightCyan.on_default().effects(Effects::BOLD))
.usage(AnsiColor::BrightCyan.on_default().effects(Effects::BOLD))
.literal(AnsiColor::Magenta.on_default().effects(Effects::BOLD))
.placeholder(AnsiColor::Cyan.on_default())
.error(AnsiColor::Red.on_default().effects(Effects::BOLD))
.valid(AnsiColor::Green.on_default())
.invalid(AnsiColor::Yellow.on_default().effects(Effects::BOLD));
#[derive(Parser, Debug)]
#[command(version, about, long_about = None, styles = HELP_STYLES)]
pub struct Cli {
#[arg(short, long, env = "YUI_SOURCE", global = true)]
pub source: Option<Utf8PathBuf>,
#[arg(short, long, action = clap::ArgAction::Count, global = true)]
pub verbose: u8,
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand, Debug)]
pub enum Command {
Init {
#[arg(long)]
git_hooks: bool,
},
Apply {
#[arg(long)]
dry_run: bool,
},
Render {
#[arg(long)]
check: bool,
#[arg(long)]
dry_run: bool,
},
Link {
#[arg(long)]
dry_run: bool,
},
Unlink { paths: Vec<Utf8PathBuf> },
Status {
#[arg(long, value_name = "MODE")]
icons: Option<IconsMode>,
#[arg(long)]
no_color: bool,
},
List {
#[arg(long)]
all: bool,
#[arg(long, value_name = "MODE")]
icons: Option<IconsMode>,
#[arg(long)]
no_color: bool,
},
Absorb {
target: Utf8PathBuf,
#[arg(long)]
dry_run: bool,
},
Doctor,
GcBackup {
#[arg(long)]
older_than: Option<String>,
},
Hooks {
#[command(subcommand)]
action: HookAction,
},
}
#[derive(Subcommand, Debug)]
pub enum HookAction {
List {
#[arg(long, value_name = "MODE")]
icons: Option<IconsMode>,
#[arg(long)]
no_color: bool,
},
Run {
name: Option<String>,
#[arg(long)]
force: bool,
},
}
impl Cli {
pub fn run(self) -> Result<()> {
let source = self.source;
match self.command {
Command::Init { git_hooks } => cmd::init(source, git_hooks),
Command::Apply { dry_run } => cmd::apply(source, dry_run),
Command::Render { check, dry_run } => cmd::render(source, check, dry_run),
Command::Link { dry_run } => cmd::link(source, dry_run),
Command::Unlink { paths } => cmd::unlink(source, paths),
Command::Status { icons, no_color } => cmd::status(source, icons, no_color),
Command::List {
all,
icons,
no_color,
} => cmd::list(source, all, icons, no_color),
Command::Absorb { target, dry_run } => cmd::absorb(source, target, dry_run),
Command::Doctor => cmd::doctor(source),
Command::GcBackup { older_than } => cmd::gc_backup(source, older_than),
Command::Hooks { action } => match action {
HookAction::List { icons, no_color } => cmd::hooks_list(source, icons, no_color),
HookAction::Run { name, force } => cmd::hooks_run(source, name, force),
},
}
}
}