packager/pkgr/src/commands.rs

161 lines
5.3 KiB
Rust
Raw Normal View History

2023-08-07 04:32:27 +02:00
use std::process::exit;
2023-08-01 21:33:32 +02:00
2023-07-17 11:50:19 +02:00
use clap::{Parser, Subcommand};
use log::{debug, error, info, trace, warn};
2023-08-01 21:33:32 +02:00
2023-07-18 17:51:55 +02:00
use crate::CONFIG;
2023-08-07 04:32:27 +02:00
use crate::package::identifier::PackageIdentifier;
use crate::package::Package;
2023-07-18 17:51:55 +02:00
use crate::process::Process;
2023-08-06 00:19:40 +02:00
use crate::types::fetch::TryFetch;
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-08-07 04:27:14 +02:00
/// Get info about pkgr
Info {
#[arg(short, long, default_value_t = false)]
detailed: bool
},
#[cfg(debug_assertions)]
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();
2023-08-01 21:33:32 +02:00
let _unix_start = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap();
info!("Parsing package...");
2023-08-07 04:27:14 +02:00
trace!("Fetching package: {:?}", package_identifier);
2023-08-06 00:19:40 +02:00
let mut pkg = Package::try_fetch(package_identifier.clone()).unwrap();
2023-07-17 21:15:30 +02:00
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-08-01 21:33:32 +02:00
let _unix_end = std::time::SystemTime::now()
2023-07-17 21:15:30 +02:00
.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.");
}
2023-08-07 04:27:14 +02:00
Command::Info {
detailed
} => {
info!("Welcome to pkgr!\n\
{}\n\
To get help please run \"{} -h\"", env!("CARGO_PKG_DESCRIPTION"), std::env::args().nth(0).unwrap());
if *detailed {
info!("");
info!("version: {}", env!("CARGO_PKG_VERSION"));
info!("authors: {}", env!("CARGO_PKG_AUTHORS"));
info!("");
info!("If you can't seem to figure something out, use debug!");
trace!("Trace should really only be used by PKGR devs.");
}
}
#[cfg(debug_assertions)]
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-08-01 21:33:32 +02:00
if let Ok(_user) = std::env::var("SUDO_USER") {
2023-07-19 15:35:51 +02:00
Process::command(vec!["sh", "-c", "echo sudo_user: $SUDO_USER"])
.spawn()
.unwrap();
}
2023-07-18 17:51:55 +02:00
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.");
}
}
}
}