packager/pkgr/src/commands.rs

142 lines
4.8 KiB
Rust
Raw Normal View History

2023-07-16 19:00:09 +02:00
use crate::package::identifier::PackageIdentifier;
2023-07-17 21:23:59 +02:00
use crate::package::installer::{InstallType, PackageInstaller};
use crate::package::Package;
2023-07-17 11:50:19 +02:00
use clap::{Parser, Subcommand};
use log::{debug, error, info, trace, warn};
use manifest::Manifest;
2023-07-17 21:23:59 +02:00
use std::process::exit;
2023-07-18 17:51:55 +02:00
use crate::CONFIG;
use crate::process::Process;
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();
info!("Parsing package...");
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 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...");
2023-07-18 17:51:55 +02:00
match pkg.install(CONFIG.with(|c| if !*build { c.build_by_default } else { *build })) {
2023-07-17 21:15:30 +02:00
Ok(_) => (),
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 11:50:19 +02:00
}
2023-07-17 21:15:30 +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 21:23:59 +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 => {
2023-07-19 16:15:26 +02:00
trace!("Trace message.\nWith newline.");
debug!("Debug message.\nWith newline.");
info!("Info message.\nWith newline.");
warn!("Warning message.\nWith newline.");
error!("Error message.\nWith newline.");
2023-07-18 17:51:55 +02:00
info!("");
2023-07-19 15:35:51 +02:00
Process::command(vec!["sh", "-c", "echo whoami: $(whoami)"])
2023-07-18 17:51:55 +02:00
.spawn()
.unwrap();
2023-07-19 15:35:51 +02:00
if let Ok(user) = std::env::var("SUDO_USER") {
Process::command(vec!["sh", "-c", "echo sudo_user: $SUDO_USER"])
.spawn()
.unwrap();
}
2023-07-18 17:51:55 +02:00
info!("");
info!("PKGR VERSION: {}", env!("CARGO_PKG_VERSION"));
info!("PKGR AUTHORS: {}", env!("CARGO_PKG_AUTHORS"));
info!("PKGR DESCRIPTION: {}", env!("CARGO_PKG_DESCRIPTION"));
info!("");
2023-07-17 21:23:59 +02:00
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.");
}
}
}
}