New check_exit_code fn

The new check_exit_code fn in pacman.rs allows update_all and remove_unused to share the logic that checks exit codes and returns an error if not 0.
This commit is contained in:
Luke Harding 2024-04-30 17:22:56 -04:00
parent 4b96063e78
commit aa1057dbd8

View File

@ -7,13 +7,14 @@
*/ */
/* /*
pacman_api.rs pacman.rs
This module provides an api to make working with Pacman much easier. This module provides an api to make working with Pacman much easier.
*/ */
use std::{error, fmt, result}; use std::{error, fmt, result};
use std::fmt::Formatter; use std::fmt::Formatter;
use std::path::Path; use std::path::Path;
use std::process::ExitStatus;
use crate::shell_commands; use crate::shell_commands;
@ -64,16 +65,7 @@ pub fn check() -> Result<()> {
pub fn update_all() -> Result<()> { pub fn update_all() -> Result<()> {
let exit_status = shell_commands::execute_in_sh("sudo pacman -Syu")?; let exit_status = shell_commands::execute_in_sh("sudo pacman -Syu")?;
if !exit_status.success() { check_exit_code(exit_status)
let exit_code = match exit_status.code() {
Some(exit_code) => exit_code,
None => return Err(PacmanError::Other("No Exit Code Found").into()),
};
return Err(PacmanError::ExitCode(exit_code).into());
}
Ok(())
} }
pub fn get_unused() -> Result<Vec<String>> { pub fn get_unused() -> Result<Vec<String>> {
@ -90,9 +82,20 @@ pub fn remove_unused(packages: Vec<String>) -> Result<()> {
let package_string = packages.join(" "); let package_string = packages.join(" ");
let command = format!("sudo pacman -Rns {}", package_string); let command = format!("sudo pacman -Rns {}", package_string);
let out = shell_commands::execute_in_sh(command)?; let exit_status = shell_commands::execute_in_sh(command)?;
dbg!(&out); check_exit_code(exit_status)
}
fn check_exit_code(exit_status: ExitStatus) -> Result<()> {
if !exit_status.success() {
let exit_code = match exit_status.code() {
Some(exit_code) => exit_code,
None => return Err(PacmanError::Other("No Exit Code Found").into()),
};
return Err(PacmanError::ExitCode(exit_code).into());
}
Ok(()) Ok(())
} }