use std::process::exit; use crate::package::identifier::PackageIdentifier; use clap::{Parser, Subcommand}; use log::{debug, error, info, trace, warn}; use manifest::Manifest; use crate::package::builder::{InstallType, PackageInstaller}; use crate::package::fetch::fetch_package; #[derive(Parser, Debug)] #[clap(name = "pkgr", version)] pub struct Cli { #[command(subcommand)] pub command: Command, } #[derive(Subcommand, Debug)] pub enum Command { /// Install a package Install { #[arg(short, long, default_value_t = false)] build: bool, package_identifier: PackageIdentifier, }, /// Remove a package from the system Remove { /// Remove the package from index too #[arg(short, long, default_value_t = false)] index: bool, package_identifier: PackageIdentifier, }, /// List packages List { /// List installed packages #[arg(short, long, default_value_t = true)] installed: bool, }, /// Update packages on the system Update, #[command(hide = true)] Debug, #[command(hide = true)] None, } impl Command { pub fn execute(&self) { match self { Command::Install { build, package_identifier, } => { warn!("Installer does not run in isolation."); let start = std::time::Instant::now(); let unix_start = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap(); trace!("Fetching package: {}", package_identifier); let pkgfile = fetch_package(package_identifier.clone()).unwrap(); debug!("size: manifest({}kb) data({}kb)", pkgfile.manifest.len() / 1024, pkgfile.data.len() / 1024); trace!("parsing manifest"); let manifest = Manifest::try_from(pkgfile.manifest.clone()).unwrap(); debug!("manifest pkg name: {}", manifest.package.name); trace!("creating installer"); let installer = PackageInstaller::new(manifest.clone(), pkgfile, if *build { InstallType::Build } else { InstallType::Bin }); trace!("starting install"); match installer.install() { Ok(_) => { info!("Sucessfully installed: {}", &manifest.package.name); () }, Err(e) => { error!("{}", e.to_string()) } } let unix_end = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap(); let end = std::time::Instant::now(); // float ms let duration = (end - start).as_nanos() as f64; info!("Install took: {}ms", (duration / 1_000_000.0)); } Command::Remove { package_identifier, index } => { if let PackageIdentifier::URI(_) = package_identifier { error!("URI is unsupported when removing applications."); exit(1); } info!("Index: {}", index); info!("Package identifier: {}", package_identifier); } Command::List { installed: _ } => { error!("List is not yet implemented."); } Command::Update => { error!("Update is not yet implemented."); } Command::Debug => { trace!("Trace message."); debug!("Debug message."); info!("Info message."); warn!("Warning message."); error!("Error message."); info!(""); info!("PKGR VERSION: {}", env!("CARGO_PKG_VERSION")); info!("PKGR AUTHORS: {}", env!("CARGO_PKG_AUTHORS")); info!("PKGR DESCRIPTION: {}", env!("CARGO_PKG_DESCRIPTION")); info!(""); info!("PKGR_LOG_LEVEL: {}", std::env::var("PKGR_LOG_LEVEL").unwrap_or_else(|_| "info".to_string())); } Command::None => { error!("No command was specified."); } } } }