From aa1057dbd82883f48f8a1999ccc6fc7ef0fb5203 Mon Sep 17 00:00:00 2001 From: Luke Harding Date: Tue, 30 Apr 2024 17:22:56 -0400 Subject: [PATCH] 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. --- src/apis/pacman.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/apis/pacman.rs b/src/apis/pacman.rs index 0f7f4a0..8b562fb 100644 --- a/src/apis/pacman.rs +++ b/src/apis/pacman.rs @@ -7,13 +7,14 @@ */ /* - pacman_api.rs + pacman.rs This module provides an api to make working with Pacman much easier. */ use std::{error, fmt, result}; use std::fmt::Formatter; use std::path::Path; +use std::process::ExitStatus; use crate::shell_commands; @@ -64,16 +65,7 @@ pub fn check() -> Result<()> { pub fn update_all() -> Result<()> { let exit_status = shell_commands::execute_in_sh("sudo pacman -Syu")?; - 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(()) + check_exit_code(exit_status) } pub fn get_unused() -> Result> { @@ -90,9 +82,20 @@ pub fn remove_unused(packages: Vec) -> Result<()> { let package_string = packages.join(" "); 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(()) }