use anyhow::Context;
use clap::{CommandFactory, Parser, Subcommand};
pub mod build;
pub mod new;
mod plugin;
#[derive(Parser, Debug)]
#[command(
name = "pint",
about = "Pint's package manager and CLI plugin host",
version
)]
pub struct Pint {
#[command(subcommand)]
cmd: Cmd,
}
#[derive(Debug, Subcommand)]
enum Cmd {
#[command(alias = "b")]
Build(build::Args),
New(new::Args),
Plugins,
#[clap(external_subcommand)]
Plugin(Vec<String>),
}
fn print_help_output() -> anyhow::Result<()> {
let mut cli = Pint::command();
cli.print_help()?;
Ok(())
}
pub fn run(pint: Pint) -> anyhow::Result<()> {
match pint.cmd {
Cmd::New(arg) => new::cmd(arg),
Cmd::Build(arg) => {
let (_plan, _built_pkgs) = build::cmd(arg)?;
Ok(())
}
Cmd::Plugins => {
plugin::print_all();
Ok(())
}
Cmd::Plugin(args) if !args.is_empty() => {
let Some(plugin_exe_path) = plugin::find_exe(&args[0]) else {
print_help_output()?;
anyhow::bail!("no known command or plugin {:?}", &args[0]);
};
let output = plugin::exec(&plugin_exe_path, &args[1..])?;
std::process::exit(output.status.code().context("unknown exit status")?);
}
_ => print_help_output(),
}
}