diff --git a/Cargo.lock b/Cargo.lock index ab7f1a0..763bc9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,91 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "rust-archlinux-update" version = "0.1.0" +dependencies = [ + "colored", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml index 9d85513..02badba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +colored = "2.1.0" diff --git a/README.md b/README.md index e028eb7..42ad468 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # Rust Arch Linux Updater -A program that automates the arch update process with cleanup and auto removal \ No newline at end of file +A program that automates the arch update process with cleanup and auto removal + +Currently, a WIP. Use at own risk. \ No newline at end of file diff --git a/src/apis/mod.rs b/src/apis/mod.rs new file mode 100644 index 0000000..f4da7e8 --- /dev/null +++ b/src/apis/mod.rs @@ -0,0 +1 @@ +pub mod pacman; diff --git a/src/apis/pacman.rs b/src/apis/pacman.rs new file mode 100644 index 0000000..0f7f4a0 --- /dev/null +++ b/src/apis/pacman.rs @@ -0,0 +1,98 @@ +/* + 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, result}; +use std::fmt::Formatter; +use std::path::Path; + +use crate::shell_commands; + +type Result = result::Result>; + +#[derive(Debug, Clone)] +enum PacmanError { + NotInstalled, + ExitCode(i32), + Other(&'static str), +} + +impl fmt::Display for PacmanError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let out = match self { + PacmanError::NotInstalled => "Pacman not installed.".to_string(), + PacmanError::ExitCode(code) => format!("Pacman exited with an exit code: {}", code), + PacmanError::Other(msg) => msg.to_string(), + }; + + write!(f, "PacmanError: {}", out) + } +} + +impl error::Error for PacmanError {} + +pub fn check() -> Result<()> { + let out = shell_commands::execute_quiet("which", ["pacman"])?; + + let path = if out.status.success() { + let mut stdout = out.stdout; + stdout.pop(); // Remove \n from stdout + + String::from_utf8(stdout)? + } else { + String::from("/usr/bin/pacman") + }; + + let path = Path::new(&path); + + if !path.exists() { + return Err(PacmanError::NotInstalled.into()); + } + + Ok(()) +} + +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(()) +} + +pub fn get_unused() -> Result> { + let out = shell_commands::execute_quiet("pacman", ["-Qtdq"])?; + + let result = String::from_utf8(out.stdout)?; + + let pkgs_to_remove: Vec = result.lines().map(|x| x.to_string()).collect(); + + Ok(pkgs_to_remove) +} + +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)?; + + dbg!(&out); + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 023539c..fe7dec9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,61 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -fn main() -> Result<(), Box> { +/* + main.rs + This file contains the primary logic of the application +*/ + +use colored::{ColoredString, Colorize}; + +use apis::pacman; + +mod apis; +mod shell_commands; + +fn main() { println!("{}", copyright_notice()); - Ok(()) + if let Err(e) = pacman::check() { + error_println(e.to_string()); + return; + } else { + notice_println("Pacman is installed\n") + } + + notice_println("Running pacman update"); + if let Err(e) = pacman::update_all() { + error_println(e.to_string()); + return; + } + + notice_println("Getting orphaned packages"); + let result = match pacman::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::remove_unused(result) { + error_println(e.to_string()); + return; + } + + notice_println("\nUpdate process complete!"); } -fn copyright_notice() -> &'static str { - "Rust Arch Linux Updater Copyright (C) 2024 Luke Harding \nThis program comes with ABSOLUTELY NO WARRANTY\nThis is free software, and you are welcome to redistribute it under certain conditions" +fn copyright_notice() -> ColoredString { + "Rust Arch Linux Updater Copyright (C) 2024 Luke Harding \nThis program comes with ABSOLUTELY NO WARRANTY\nThis is free software, and you are welcome to redistribute it under certain conditions\n".italic() +} + +pub fn error_println(msg: impl Into) { + eprintln!("{}", msg.into().red().bold()) +} + +pub fn notice_println(msg: impl Into) { + println!("{}", msg.into().green().bold()) } diff --git a/src/shell_commands.rs b/src/shell_commands.rs new file mode 100644 index 0000000..4346e93 --- /dev/null +++ b/src/shell_commands.rs @@ -0,0 +1,40 @@ +/* + 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 . +*/ + +/* + shell_commands.rs + This file contains utility functions to interact with the shell. +*/ + +use std::ffi::OsStr; +use std::io; +use std::process::{Command, ExitStatus, Output}; + +pub fn execute_and_display, I>(cmd: S, args: I) -> io::Result +where + I: IntoIterator, + I::Item: AsRef, +{ + let mut child = Command::new(cmd).args(args).spawn()?; + + let exit_status = child.wait()?; + + Ok(exit_status) +} + +pub fn execute_in_sh(cmd: impl Into) -> io::Result { + execute_and_display("sh", ["-c", &cmd.into()]) +} + +pub fn execute_quiet, I>(cmd: S, args: I) -> io::Result +where + I: IntoIterator, + I::Item: AsRef, +{ + Command::new(cmd).args(args).output() +}