use crate::config::DcspkgConfig;
use crate::util::*;
use crate::{install_package, list_all_packages, run_package};
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[clap(version, about, long_about = None)]
#[clap(propagate_version = true)]
pub struct Cli {
#[clap(short, long, action = clap::ArgAction::Count)]
pub verbose: u8,
#[clap(subcommand)]
pub command: Command,
}
#[derive(Subcommand)]
pub enum Command {
List {
#[clap(long, short, action)]
json: bool,
},
Install { package: String },
Installed {
#[clap(long, short, action)]
json: bool,
},
Run { package: String },
}
impl Command {
pub fn run(&self, config: DcspkgConfig) -> anyhow::Result<()> {
use Command::*;
match &self {
List { json } => {
let packages = list_all_packages(config.server.url)?;
print_package_list(&packages, *json);
Ok(())
}
Install { package } => install_package(
package,
config.server.url,
config.registry.install_dir,
config.registry.bin_dir,
config.registry.registry_file,
),
Installed { json } => {
let packages = list_installed_packages(&config.registry.registry_file)?;
print_package_list(&packages, *json);
Ok(())
}
Run { package } => run_package(
&config.registry.registry_file,
config.registry.install_dir,
package,
),
}
}
}