use anyhow::Result;
use clap::{Parser, Subcommand};
use crate::commands;
#[derive(Parser)]
#[command(name = "texforge", version, about)]
pub struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Clean,
Init,
New {
name: String,
#[arg(short, long)]
template: Option<String>,
},
Build {
#[arg(long)]
watch: bool,
#[arg(long, default_value = "2")]
delay: u64,
},
Fmt {
#[arg(long)]
check: bool,
},
Check,
Template {
#[command(subcommand)]
action: TemplateAction,
},
Config {
key: Option<String>,
value: Option<String>,
},
}
#[derive(Subcommand)]
enum TemplateAction {
List {
#[arg(long)]
all: bool,
},
Add { source: String },
Remove { name: String },
Validate { name: String },
}
impl Cli {
pub fn execute(self) -> Result<()> {
match self.command {
Commands::Clean => commands::clean::execute(),
Commands::Init => commands::init::execute(),
Commands::New { name, template } => commands::new::execute(&name, template.as_deref()),
Commands::Build { watch, delay } => {
if watch {
commands::build::watch(delay)
} else {
commands::build::execute()
}
}
Commands::Fmt { check } => commands::fmt::execute(check),
Commands::Check => commands::check::execute(),
Commands::Template { action } => match action {
TemplateAction::List { all } => commands::template::list(all),
TemplateAction::Add { source } => commands::template::add(&source),
TemplateAction::Remove { name } => commands::template::remove(&name),
TemplateAction::Validate { name } => commands::template::validate(&name),
},
Commands::Config { key, value } => match (key, value) {
(None, None) => commands::config::wizard(),
(Some(k), None) if k == "list" => commands::config::list(),
(Some(k), None) => commands::config::get(&k),
(Some(k), Some(v)) => commands::config::set(&k, &v),
(None, Some(_)) => anyhow::bail!("Cannot set value without a key"),
},
}
}
}