server_forge/
distro.rs

1//! # Distribution Module
2//!
3//! This module provides functionality for interacting with different Linux distributions
4//! and their package managers. It includes functions for detecting the package manager,
5//! updating the system, and installing or uninstalling packages.
6
7use std::error::Error;
8use std::path::Path;
9
10/// Represents the different package managers supported by the application.
11#[derive(Debug, PartialEq)]
12pub enum PackageManager {
13    Apt, // For Debian-based distributions (e.g., Ubuntu)
14    Yum, // For older Red Hat-based distributions
15    Dnf, // For newer Red Hat-based distributions (e.g., Fedora)
16}
17
18/// Detects the package manager used by the current system.
19///
20/// This function checks for the existence of specific package manager
21/// executables to determine which one is available on the system.
22///
23/// # Returns
24///
25/// Returns a `Result` containing the detected `PackageManager` or an error
26/// if no supported package manager is found.
27pub fn get_package_manager() -> Result<PackageManager, Box<dyn Error>> {
28    if Path::new("/usr/bin/apt").exists() {
29        Ok(PackageManager::Apt)
30    } else if Path::new("/usr/bin/yum").exists() {
31        Ok(PackageManager::Yum)
32    } else if Path::new("/usr/bin/dnf").exists() {
33        Ok(PackageManager::Dnf)
34    } else {
35        Err("Unsupported package manager".into())
36    }
37}
38
39/// Updates the system using the specified package manager.
40///
41/// This function runs the appropriate update commands for the given package manager.
42///
43/// # Arguments
44///
45/// * `package_manager` - A reference to the `PackageManager` enum representing the system's package manager.
46///
47/// # Returns
48///
49/// Returns a `Result` indicating success or an error if the update process fails.
50pub fn update_system(package_manager: &PackageManager) -> Result<(), Box<dyn Error>> {
51    match package_manager {
52        PackageManager::Apt => {
53            crate::utils::run_command("apt", &["update"])?;
54            crate::utils::run_command("apt", &["upgrade", "-y"])?;
55        }
56        PackageManager::Yum => {
57            crate::utils::run_command("yum", &["update", "-y"])?;
58        }
59        PackageManager::Dnf => {
60            crate::utils::run_command("dnf", &["upgrade", "-y"])?;
61        }
62    }
63    Ok(())
64}
65
66/// Installs a package using the specified package manager.
67///
68/// This function runs the appropriate install command for the given package manager.
69///
70/// # Arguments
71///
72/// * `package_manager` - A reference to the `PackageManager` enum representing the system's package manager.
73/// * `package` - A string slice containing the name of the package to install.
74///
75/// # Returns
76///
77/// Returns a `Result` indicating success or an error if the installation process fails.
78pub fn install_package(
79    package_manager: &PackageManager,
80    package: &str,
81) -> Result<(), Box<dyn Error>> {
82    match package_manager {
83        PackageManager::Apt => crate::utils::run_command("apt", &["install", "-y", package])?,
84        PackageManager::Yum => crate::utils::run_command("yum", &["install", "-y", package])?,
85        PackageManager::Dnf => crate::utils::run_command("dnf", &["install", "-y", package])?,
86    }
87    Ok(())
88}
89
90/// Uninstalls a package using the specified package manager.
91///
92/// This function runs the appropriate remove command for the given package manager.
93///
94/// # Arguments
95///
96/// * `package_manager` - A reference to the `PackageManager` enum representing the system's package manager.
97/// * `package` - A string slice containing the name of the package to uninstall.
98///
99/// # Returns
100///
101/// Returns a `Result` indicating success or an error if the uninstallation process fails.
102pub fn uninstall_package(
103    package_manager: &PackageManager,
104    package: &str,
105) -> Result<(), Box<dyn Error>> {
106    match package_manager {
107        PackageManager::Apt => crate::utils::run_command("apt", &["remove", "-y", package])?,
108        PackageManager::Yum => crate::utils::run_command("yum", &["remove", "-y", package])?,
109        PackageManager::Dnf => crate::utils::run_command("dnf", &["remove", "-y", package])?,
110    }
111    Ok(())
112}