Add ability to remove unused packages

This commit is contained in:
Luke Harding 2024-04-28 12:48:57 -04:00
parent 064081a0c3
commit 160ad7f1e2
3 changed files with 56 additions and 8 deletions

View File

@ -32,7 +32,23 @@ fn main() {
return;
}
notice_println("Move onto something else");
notice_println("Getting orphaned packages");
let result = match pacman_api::get_unused() {
Ok(result) => result,
Err(e) => {
error_println(e.to_string());
return;
}
};
if result.is_empty() {
println!("No Orphaned Packages Found.")
} else if let Err(e) = pacman_api::remove_unused(result) {
error_println(e.to_string());
return;
}
notice_println("Do Something Else");
}
fn copyright_notice() -> ColoredString {

View File

@ -11,17 +11,18 @@
This module provides an api to make working with Pacman much easier.
*/
use std::{error, fmt};
use std::{error, fmt, io};
use std::fmt::Formatter;
use std::io::Read;
use std::path::Path;
use crate::shell_commands;
#[derive(Debug, Clone)]
#[derive(Debug)]
pub enum PacmanError {
InsufficientPrivilege,
NotInstalled,
IoError(io::Error),
Other(String),
}
@ -31,6 +32,7 @@ impl fmt::Display for PacmanError {
PacmanError::InsufficientPrivilege => "Failed to run pacman. Maybe try sudo?",
PacmanError::NotInstalled => "Unable to find pacman. Is this system arch based?",
PacmanError::Other(msg) => msg,
_ => "Unexpected Error Occurred",
};
write!(f, "{}", output)
@ -42,7 +44,7 @@ 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())),
Err(e) => return Err(PacmanError::IoError(e)),
};
let path = if out.status.success() {
@ -67,9 +69,9 @@ pub fn check() -> Result<(), PacmanError> {
}
pub fn update_all() -> Result<(), PacmanError> {
let out = match shell_commands::execute_in_sh("pacman -Syu") {
let out = match shell_commands::execute_in_sh("sudo pacman -Syu") {
Ok(out) => out,
Err(e) => return Err(PacmanError::Other(e.to_string())),
Err(e) => return Err(PacmanError::IoError(e)),
};
let mut child_stderr = match out.stderr {
@ -90,3 +92,33 @@ pub fn update_all() -> Result<(), PacmanError> {
Err(PacmanError::Other(stderr_out))
}
}
#[derive(Debug)]
struct GetUnusedStatus {}
pub fn get_unused() -> Result<String, PacmanError> {
let out = match shell_commands::execute_quiet("pacman", ["-Qtdq"]) {
Ok(out) => out,
Err(e) => return Err(PacmanError::IoError(e)),
};
let result = match String::from_utf8(out.stdout) {
Ok(out) => out,
Err(e) => return Err(PacmanError::Other(e.to_string())),
};
Ok(result)
}
pub fn remove_unused(packages: String) -> Result<(), PacmanError> {
let command = format!("sudo pacman -Rns {}", packages);
let out = match shell_commands::execute_in_sh(command) {
Ok(out) => out,
Err(e) => return Err(PacmanError::IoError(e)),
};
dbg!(&out);
Ok(())
}

View File

@ -30,8 +30,8 @@ where
Ok(child)
}
pub fn execute_in_sh(cmd: &'static str) -> io::Result<Child> {
execute_and_display("sh", ["-c", cmd])
pub fn execute_in_sh(cmd: impl Into<String>) -> io::Result<Child> {
execute_and_display("sh", ["-c", &cmd.into()])
}
pub fn execute_quiet<S: AsRef<OsStr>, I>(cmd: S, args: I) -> io::Result<Output>