/* Rust Arch Linux Updater Copyright (C) 2024 Luke Harding This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /* pacman_api.rs This module provides an api to make working with Pacman much easier. */ use std::{error, fmt}; use std::fmt::Formatter; use std::path::Path; use crate::shell_commands; #[derive(Debug, Clone)] pub enum PacmanError { InsufficientPrivilege, NotInstalled, Other(String), } impl fmt::Display for PacmanError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { let output = match self { PacmanError::InsufficientPrivilege => "Failed to run pacman. Maybe try sudo?", PacmanError::NotInstalled => "Unable to find pacman. Is this system arch based?", PacmanError::Other(msg) => msg, }; write!(f, "{}", output) } } impl error::Error for PacmanError {} pub fn check() -> Result<(), PacmanError> { let out = match shell_commands::execute_quiet("which", ["pacman"]) { Ok(out) => out, Err(e) => return Err(PacmanError::Other(e.to_string())), }; let path = if out.status.success() { let mut stdout = out.stdout; stdout.pop(); // Remove \n from stdout match String::from_utf8(stdout) { Ok(result) => result, Err(e) => return Err(PacmanError::Other(e.to_string())), } } else { String::from("/usr/bin/pacman") }; let path = Path::new(&path); if !path.exists() { return Err(PacmanError::NotInstalled); } Ok(()) } pub fn update_all() -> Result<(), PacmanError> { if let Err(e) = shell_commands::execute_in_sh("sudo pacman -Syu") { return Err(PacmanError::Other(e.to_string())); } Ok(()) }