use anyhow::Result;
use clap::{Parser, Subcommand};
use crate::commands::{
add::AddCommand, analytics::AnalyticsCommand, bench::BenchCommand, build::BuildCommand,
check::CheckCommand, clean::CleanCommand, collection::CollectionCommand, doc::DocCommand,
doctor::DoctorCommand, init::InitCommand, install::InstallCommand, new::NewCommand,
package::PackageCommand, publish::PublishCommand, registry::RegistryCommand,
release::ReleaseCommand, remove::RemoveCommand, run::RunCommand, search::SearchCommand,
tag::TagCommand, test::TestCommand, tree::TreeCommand, update::UpdateCommand,
vendor::VendorCommand,
};
#[derive(Parser, Debug)]
#[command(name = "ccgo")]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct Cli {
#[arg(short, long, global = true)]
pub verbose: bool,
#[arg(long, global = true)]
pub no_color: bool,
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
Build(BuildCommand),
New(NewCommand),
Init(InitCommand),
Run(RunCommand),
Test(TestCommand),
Bench(BenchCommand),
Doc(DocCommand),
Publish(PublishCommand),
Check(CheckCommand),
Doctor(DoctorCommand),
Clean(CleanCommand),
Tag(TagCommand),
Release(ReleaseCommand),
Package(PackageCommand),
Install(InstallCommand),
Add(AddCommand),
Remove(RemoveCommand),
Update(UpdateCommand),
Tree(TreeCommand),
Collection(CollectionCommand),
Registry(RegistryCommand),
Search(SearchCommand),
Vendor(VendorCommand),
Analytics(AnalyticsCommand),
}
impl Cli {
pub fn execute(self) -> Result<()> {
if self.no_color {
console::set_colors_enabled(false);
console::set_colors_enabled_stderr(false);
}
dispatch_command(self.command, self.verbose)
}
}
fn dispatch_command(command: Commands, verbose: bool) -> Result<()> {
match command {
Commands::Build(cmd) => cmd.execute(verbose),
Commands::New(cmd) => cmd.execute(verbose),
Commands::Init(cmd) => cmd.execute(verbose),
Commands::Run(cmd) => cmd.execute(verbose),
Commands::Test(cmd) => cmd.execute(verbose),
Commands::Bench(cmd) => cmd.execute(verbose),
Commands::Doc(cmd) => cmd.execute(verbose),
Commands::Publish(cmd) => cmd.execute(verbose),
Commands::Check(cmd) => cmd.execute(verbose),
Commands::Doctor(cmd) => cmd.execute(verbose),
Commands::Clean(cmd) => cmd.execute(verbose),
Commands::Tag(cmd) => cmd.execute(verbose),
Commands::Release(cmd) => cmd.execute(verbose),
Commands::Package(cmd) => cmd.execute(verbose),
Commands::Analytics(cmd) => cmd.execute(verbose),
other => dispatch_dependency_command(other, verbose),
}
}
fn dispatch_dependency_command(command: Commands, verbose: bool) -> Result<()> {
match command {
Commands::Install(cmd) => cmd.execute(verbose),
Commands::Add(cmd) => cmd.execute(verbose),
Commands::Remove(cmd) => cmd.execute(verbose),
Commands::Update(cmd) => cmd.execute(verbose),
Commands::Tree(cmd) => cmd.execute(verbose),
Commands::Collection(cmd) => cmd.execute(verbose),
Commands::Registry(cmd) => cmd.execute(verbose),
Commands::Search(cmd) => cmd.execute(verbose),
Commands::Vendor(cmd) => cmd.execute(verbose),
_ => unreachable!("dispatch_dependency_command called with non-dependency command"),
}
}