Skip to content

Commit

Permalink
feat: get the package manager (#8)
Browse files Browse the repository at this point in the history
feat: get the default package manager from operating system

feat: get the package manager from all 3 possible input
  • Loading branch information
Rignchen authored Jul 12, 2024
1 parent 9d334fb commit 6e712a7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
66 changes: 66 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::env;
use std::io::Read;

/// List of errors that the program can return.
#[derive(Debug)]
pub enum DepotError {
UnknownOperatingSystem,
UnknownPackageManager,
}

/// Result type wich wither take a type T or a DepotError.
Expand All @@ -17,6 +19,8 @@ pub fn unwrap_depot_error<T>(result: DepotResult<T>) -> T {
match error {
DepotError::UnknownOperatingSystem =>
"Unable to determine your current operating system.",
DepotError::UnknownPackageManager =>
"The package manager is unknown or not supported.",
}
);
std::process::exit(1);
Expand Down Expand Up @@ -60,3 +64,65 @@ impl OperatingSystem {
}
}
}

/// List of all supported operating systems.
#[derive(clap::ValueEnum, Debug, Clone, PartialEq)]
pub enum PackageManager {
Pacman,
Yay,
Apk,
AptGet,
Apt,
Pkg,
Dnf,
}
impl From<&OperatingSystem> for PackageManager {
/// Get the default package manager for the operating system.
fn from(os: &OperatingSystem) -> PackageManager {
match os {
OperatingSystem::Arch => PackageManager::Pacman,
OperatingSystem::Alpine => PackageManager::Apk,
OperatingSystem::Debian => PackageManager::AptGet,
OperatingSystem::Ubuntu => PackageManager::AptGet,
OperatingSystem::Fedora => PackageManager::Dnf,
}
}
}

/// Get the package manager to use.
/// If the expected one is None, look for it in the environment variables.
/// If it is not in the environment variables, guess it from the current operating system.
///
/// ```
/// use depot::{get_package_manager, PackageManager, OperatingSystem};
/// assert_eq!(get_package_manager(Some(PackageManager::Pacman)).unwrap(), PackageManager::Pacman);
/// std::env::set_var("DEPOT_PM", "yay");
/// assert_eq!(get_package_manager(None).unwrap(), PackageManager::Yay);
/// std::env::remove_var("DEPOT_PM");
/// assert_eq!(get_package_manager(None).unwrap(),PackageManager::from(&OperatingSystem::current().unwrap()));
/// ```
/// ```should_panic
/// std::env::set_var("DEPOT_PM", "unknown");
/// depot::get_package_manager(None).unwrap();
/// ```
pub fn get_package_manager(expected: Option<PackageManager>) -> DepotResult<PackageManager> {
match expected {
Some(manager) => Ok(manager),
None => match env::var("DEPOT_PM") {
Ok(manager) => match manager.as_str() {
"pacman" => Ok(PackageManager::Pacman),
"yay" => Ok(PackageManager::Yay),
"apk" => Ok(PackageManager::Apk),
"apt-get" => Ok(PackageManager::AptGet),
"apt" => Ok(PackageManager::Apt),
"pkg" => Ok(PackageManager::Pkg),
"dnf" => Ok(PackageManager::Dnf),
_ => Err(DepotError::UnknownPackageManager),
},
Err(_) => {
let os = OperatingSystem::current()?;
Ok(PackageManager::from(&os))
}
},
}
}
11 changes: 5 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use depot::{unwrap_depot_error, OperatingSystem};
use depot::{get_package_manager, unwrap_depot_error, PackageManager};

structstruck::strike! {
/// Structure of the command line arguments
Expand All @@ -16,8 +16,8 @@ structstruck::strike! {
/// update Update the package
#[strikethrough[derive(Parser, Debug)]]
struct Args {
#[clap(short, long, alias = "pm")]
package_manager: Option<String>,
#[clap(short, long, alias = "pm", value_enum)]
package_manager: Option<PackageManager>,
#[clap(subcommand)]
cmd: enum Command {
Install(
Expand Down Expand Up @@ -52,7 +52,8 @@ structstruck::strike! {
/// - get the os name and deduce it from there
fn main() {
let args = Args::parse();
println!("Args: {:?}", args);
let package_manager = unwrap_depot_error(get_package_manager(args.package_manager));
println!("Package manager: {:?}", package_manager);
println!(
"{}",
match args.cmd {
Expand All @@ -65,6 +66,4 @@ fn main() {
),
}
);
let os = unwrap_depot_error(OperatingSystem::current());
println!("OS: {:?}", os);
}

0 comments on commit 6e712a7

Please sign in to comment.