use std::ffi::OsString;
use clap::CommandFactory;
use crate::app::{self, Session};
use crate::cli::{Cli, Command, SelfCommand};
use crate::error::{PmpxError, EXIT_OK};
mod config;
mod info;
mod plugin;
mod plugin_pin;
mod plugin_store;
use self::config::config_cmd;
use self::info::{show_detection, show_info};
use self::plugin::plugin_cmd;
pub fn dispatch(args: &Cli) -> crate::error::Result<u8> {
let Some(command) = &args.command else {
return show_detection(args);
};
match command {
Command::Install { packages } => {
run_verb(args, pmpx_plugin::Verb::Install, packages.clone(), false)
}
Command::Remove { packages } => {
run_verb(args, pmpx_plugin::Verb::Remove, packages.clone(), false)
}
Command::Update { packages } => {
run_verb(args, pmpx_plugin::Verb::Update, packages.clone(), false)
}
Command::Build { args: rest } => {
run_verb(args, pmpx_plugin::Verb::Build, rest.clone(), false)
}
Command::Test { args: rest } => {
run_verb(args, pmpx_plugin::Verb::Test, rest.clone(), false)
}
Command::Run { target, args: rest } => {
let argv: Vec<OsString> = target.iter().cloned().chain(rest.iter().cloned()).collect();
run_verb(args, pmpx_plugin::Verb::Run, argv, false)
}
Command::Exec { command } => run_verb(args, pmpx_plugin::Verb::Exec, command.clone(), true),
Command::Info => show_info(args),
Command::Plugin(sub) => plugin_cmd(args, sub),
Command::Config(sub) => config_cmd(sub),
Command::Completion { shell } => completion(*shell),
Command::SelfTool { command } => self_cmd(command),
}
}
fn run_verb(
args: &Cli,
verb: pmpx_plugin::Verb,
argv: Vec<OsString>,
allow_exec_fallback: bool,
) -> crate::error::Result<u8> {
let session = Session::open(args).map_err(PmpxError::Other)?;
app::run_verb(&session, verb, &argv, allow_exec_fallback)
}
fn self_cmd(cmd: &SelfCommand) -> crate::error::Result<u8> {
match cmd {
SelfCommand::Update {
check,
version,
force,
} => crate::selfupdate::run(&crate::selfupdate::Request {
check: *check,
version: version.clone(),
force: *force,
}),
}
}
fn completion(shell: clap_complete::Shell) -> crate::error::Result<u8> {
let mut cmd = Cli::command();
let name = cmd.get_name().to_string();
clap_complete::generate(shell, &mut cmd, name, &mut std::io::stdout());
Ok(EXIT_OK)
}