Add ability to remove unused packages
This commit is contained in:
parent
064081a0c3
commit
160ad7f1e2
18
src/main.rs
18
src/main.rs
@ -32,7 +32,23 @@ fn main() {
|
|||||||
return;
|
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 {
|
fn copyright_notice() -> ColoredString {
|
||||||
|
@ -11,17 +11,18 @@
|
|||||||
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};
|
use std::{error, fmt, io};
|
||||||
use std::fmt::Formatter;
|
use std::fmt::Formatter;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::shell_commands;
|
use crate::shell_commands;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug)]
|
||||||
pub enum PacmanError {
|
pub enum PacmanError {
|
||||||
InsufficientPrivilege,
|
InsufficientPrivilege,
|
||||||
NotInstalled,
|
NotInstalled,
|
||||||
|
IoError(io::Error),
|
||||||
Other(String),
|
Other(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,6 +32,7 @@ impl fmt::Display for PacmanError {
|
|||||||
PacmanError::InsufficientPrivilege => "Failed to run pacman. Maybe try sudo?",
|
PacmanError::InsufficientPrivilege => "Failed to run pacman. Maybe try sudo?",
|
||||||
PacmanError::NotInstalled => "Unable to find pacman. Is this system arch based?",
|
PacmanError::NotInstalled => "Unable to find pacman. Is this system arch based?",
|
||||||
PacmanError::Other(msg) => msg,
|
PacmanError::Other(msg) => msg,
|
||||||
|
_ => "Unexpected Error Occurred",
|
||||||
};
|
};
|
||||||
|
|
||||||
write!(f, "{}", output)
|
write!(f, "{}", output)
|
||||||
@ -42,7 +44,7 @@ impl error::Error for PacmanError {}
|
|||||||
pub fn check() -> Result<(), PacmanError> {
|
pub fn check() -> Result<(), PacmanError> {
|
||||||
let out = match shell_commands::execute_quiet("which", ["pacman"]) {
|
let out = match shell_commands::execute_quiet("which", ["pacman"]) {
|
||||||
Ok(out) => out,
|
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() {
|
let path = if out.status.success() {
|
||||||
@ -67,9 +69,9 @@ pub fn check() -> Result<(), PacmanError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_all() -> 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,
|
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 {
|
let mut child_stderr = match out.stderr {
|
||||||
@ -90,3 +92,33 @@ pub fn update_all() -> Result<(), PacmanError> {
|
|||||||
Err(PacmanError::Other(stderr_out))
|
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(())
|
||||||
|
}
|
||||||
|
@ -30,8 +30,8 @@ where
|
|||||||
Ok(child)
|
Ok(child)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execute_in_sh(cmd: &'static str) -> io::Result<Child> {
|
pub fn execute_in_sh(cmd: impl Into<String>) -> io::Result<Child> {
|
||||||
execute_and_display("sh", ["-c", cmd])
|
execute_and_display("sh", ["-c", &cmd.into()])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execute_quiet<S: AsRef<OsStr>, I>(cmd: S, args: I) -> io::Result<Output>
|
pub fn execute_quiet<S: AsRef<OsStr>, I>(cmd: S, args: I) -> io::Result<Output>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user