packager/pkgr/src/commands/mod.rs

117 lines
4.2 KiB
Rust
Raw Normal View History

use std::process::exit;
2023-07-16 19:00:09 +02:00
use crate::package::identifier::PackageIdentifier;
2023-07-17 11:50:19 +02:00
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;
2023-07-16 19:00:09 +02:00
2023-07-17 11:50:19 +02:00
#[derive(Parser, Debug)]
2023-07-17 12:13:14 +02:00
#[clap(name = "pkgr", version)]
2023-07-17 11:50:19 +02:00
pub struct Cli {
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand, Debug)]
2023-07-16 19:00:09 +02:00
pub enum Command {
2023-07-17 11:50:19 +02:00
/// Install a package
Install {
#[arg(short, long, default_value_t = false)]
build: bool,
package_identifier: PackageIdentifier,
},
/// Remove a package from the system
Remove {
2023-07-17 12:13:14 +02:00
/// Remove the package from index too
#[arg(short, long, default_value_t = false)]
index: bool,
2023-07-17 11:50:19 +02:00
package_identifier: PackageIdentifier,
},
/// List packages
List {
/// List installed packages
#[arg(short, long, default_value_t = true)]
installed: bool,
},
/// Update packages on the system
2023-07-16 19:00:09 +02:00
Update,
2023-07-17 11:50:19 +02:00
#[command(hide = true)]
Debug,
#[command(hide = true)]
2023-07-17 11:50:19 +02:00
None,
}
impl Command {
pub fn execute(&self) {
match self {
Command::Install {
build,
package_identifier,
2023-07-17 11:50:19 +02:00
} => {
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())
}
2023-07-17 11:50:19 +02:00
}
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));
2023-07-17 11:50:19 +02:00
}
Command::Remove { package_identifier, index } => {
if let PackageIdentifier::URI(_) = package_identifier {
error!("URI is unsupported when removing applications.");
exit(1);
2023-07-17 12:13:14 +02:00
}
info!("Index: {}", index);
info!("Package identifier: {}", package_identifier);
2023-07-17 11:50:19 +02:00
}
2023-07-17 11:50:51 +02:00
Command::List { installed: _ } => {
2023-07-17 11:50:19 +02:00
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()));
}
2023-07-17 11:50:19 +02:00
Command::None => {
error!("No command was specified.");
}
}
}
}