2023-07-17 17:47:42 +02:00
|
|
|
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};
|
2023-07-17 17:47:42 +02:00
|
|
|
use log::{debug, error, info, trace, warn};
|
|
|
|
use manifest::Manifest;
|
2023-07-17 20:40:01 +02:00
|
|
|
use crate::package::installer::{InstallType, PackageInstaller};
|
2023-07-17 21:15:30 +02:00
|
|
|
use crate::package::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)]
|
2023-07-17 17:47:42 +02:00
|
|
|
Debug,
|
|
|
|
#[command(hide = true)]
|
2023-07-17 11:50:19 +02:00
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Command {
|
|
|
|
pub fn execute(&self) {
|
|
|
|
match self {
|
|
|
|
Command::Install {
|
|
|
|
build,
|
2023-07-17 17:47:42 +02:00
|
|
|
package_identifier,
|
2023-07-17 11:50:19 +02:00
|
|
|
} => {
|
2023-07-17 17:47:42 +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();
|
|
|
|
|
2023-07-17 20:40:01 +02:00
|
|
|
info!("Parsing package...");
|
2023-07-17 17:47:42 +02:00
|
|
|
trace!("Fetching package: {}", package_identifier);
|
2023-07-17 21:15:30 +02:00
|
|
|
let mut pkg = Package::fetch(package_identifier.clone()).unwrap();
|
|
|
|
debug!("manifest size: {}kb", pkg.pkgfile.manifest.len() / 1024);
|
|
|
|
debug!("files size: {}kb", pkg.pkgfile.data.len() / 1024);
|
2023-07-17 17:47:42 +02:00
|
|
|
|
2023-07-17 21:15:30 +02:00
|
|
|
trace!("Checking if package is installed...");
|
|
|
|
if pkg.is_installed() {
|
|
|
|
error!("Package is already installed.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
trace!("Starting install...");
|
|
|
|
match pkg.install() {
|
|
|
|
Ok(_) => (),
|
2023-07-17 17:47:42 +02:00
|
|
|
Err(e) => {
|
2023-07-17 21:15:30 +02:00
|
|
|
error!("Install failed: {}", e.to_string());
|
2023-07-17 18:02:10 +02:00
|
|
|
exit(1);
|
2023-07-17 17:47:42 +02:00
|
|
|
}
|
2023-07-17 11:50:19 +02:00
|
|
|
}
|
2023-07-17 21:15:30 +02:00
|
|
|
|
2023-07-17 17:47:42 +02:00
|
|
|
let end = std::time::Instant::now();
|
2023-07-17 21:15:30 +02:00
|
|
|
let unix_end = std::time::SystemTime::now()
|
|
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let duration = end.duration_since(start);
|
|
|
|
|
|
|
|
info!("Install complete.");
|
|
|
|
info!("Install took {}ms.", duration.as_nanos() as f64 / 1000000.0);
|
|
|
|
|
2023-07-17 11:50:19 +02:00
|
|
|
}
|
2023-07-17 17:47:42 +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
|
|
|
}
|
2023-07-17 17:47:42 +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.");
|
|
|
|
}
|
2023-07-17 17:47:42 +02:00
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|