From 00542f11d395a906302d6ddf7e79a5e8d53b19b1 Mon Sep 17 00:00:00 2001 From: Luke Harding Date: Sat, 27 Apr 2024 16:01:47 -0400 Subject: [PATCH] Work on #10 & New implementation for #9 --- Cargo.lock | 85 +++++++++++++++++++ Cargo.toml | 1 + ...cman_install_check.rs => install_check.rs} | 19 +++-- src/main.rs | 40 ++++----- src/pacman_api.rs | 74 ++++++++++++++++ src/shell_commands.rs | 32 +++---- 6 files changed, 203 insertions(+), 48 deletions(-) rename src/{pacman_install_check.rs => install_check.rs} (67%) create mode 100644 src/pacman_api.rs 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/src/pacman_install_check.rs b/src/install_check.rs similarity index 67% rename from src/pacman_install_check.rs rename to src/install_check.rs index 78eeebe..6741cc8 100644 --- a/src/pacman_install_check.rs +++ b/src/install_check.rs @@ -7,8 +7,8 @@ */ /* - pacman_install_check.rs - This file contains a method to check if the pacman package manager is installed + install_check.rs + This module allows for checking if any file is present on the filesystem */ use std::fmt; @@ -16,20 +16,23 @@ use std::fmt::Formatter; use std::path::Path; #[derive(Debug, Clone)] -pub struct PacmanInstalledError; +pub struct NotInstalledError { + file_name: String, +} -impl fmt::Display for PacmanInstalledError { +impl fmt::Display for NotInstalledError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "pacman pkg manager not installed.") + write!(f, "{} not found.", self.file_name) } } -pub fn run() -> Result<(), PacmanInstalledError> { - let exists = Path::new("/usr/bin/pacman").exists(); +pub fn run(path: impl Into) -> Result<(), NotInstalledError> { + let path = path.into(); + let exists = Path::new(&path).exists(); if exists { Ok(()) } else { - Err(PacmanInstalledError) + Err(NotInstalledError { file_name: path }) } } diff --git a/src/main.rs b/src/main.rs index 638d433..f66251e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,36 +11,38 @@ This file contains the primary logic of the application */ -use std::io; +use colored::{ColoredString, Colorize}; -mod pacman_install_check; +mod install_check; +mod pacman_api; mod shell_commands; fn main() { println!("{}", copyright_notice()); - if let Err(e) = pacman_install_check::run() { - eprintln!("{}", e); + if let Err(e) = pacman_api::check() { + error_println(e.to_string()); return; + } else { + notice_println("Pacman is installed\n") } - println!("Running pacman update"); - if let Err(e) = shell_commands::execute_in_sh("pacman -Syu") { - match e.kind() { - io::ErrorKind::PermissionDenied => { - eprintln!("Permission to use pacman has been denied. Please try again with sudo."); - return; - } - _ => { - eprintln!("{}", e); - return; - } - } + notice_println("Running pacman update"); + if let Err(e) = pacman_api::update_all() { + error_println(e.to_string()); } - println!("Move onto something else"); + notice_println("Move onto something else"); } -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\n" +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/pacman_api.rs b/src/pacman_api.rs new file mode 100644 index 0000000..0d72f03 --- /dev/null +++ b/src/pacman_api.rs @@ -0,0 +1,74 @@ +/* + 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}; +use std::fmt::Formatter; +use std::path::Path; + +use crate::shell_commands; + +#[derive(Debug, Clone)] +pub enum PacmanError { + InsufficientPrivilege, + NotInstalled, + Other(String), +} + +impl fmt::Display for PacmanError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let output = match self { + PacmanError::InsufficientPrivilege => "Failed to run pacman. Maybe try sudo?", + PacmanError::NotInstalled => "Unable to find pacman. Is this system arch based?", + PacmanError::Other(msg) => msg, + }; + + write!(f, "{}", output) + } +} + +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())), + }; + + let path = if out.status.success() { + let mut stdout = out.stdout; + stdout.pop(); // Remove \n from stdout + + match String::from_utf8(stdout) { + Ok(result) => result, + Err(e) => return Err(PacmanError::Other(e.to_string())), + } + } else { + String::from("/usr/bin/pacman") + }; + + let path = Path::new(&path); + + if !path.exists() { + return Err(PacmanError::NotInstalled); + } + + Ok(()) +} + +pub fn update_all() -> Result<(), PacmanError> { + if let Err(e) = shell_commands::execute_in_sh("sudo pacman -Syu") { + return Err(PacmanError::Other(e.to_string())); + } + + Ok(()) +} diff --git a/src/shell_commands.rs b/src/shell_commands.rs index a42d020..fe4d2eb 100644 --- a/src/shell_commands.rs +++ b/src/shell_commands.rs @@ -13,38 +13,28 @@ use std::ffi::OsStr; use std::io; -use std::io::{ErrorKind, Read}; -use std::process::{Command, Stdio}; +use std::process::{Command, 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) - .stderr(Stdio::piped()) - .spawn()?; + let mut child = Command::new(cmd).args(args).spawn()?; child.wait()?; - if let Some(mut child_stderr) = child.stderr { - let mut output = String::new(); - child_stderr.read_to_string(&mut output)?; - - // TODO: Relocate to future pacman api - if output == "error: you cannot perform this operation unless you are root.\n" { - Err(io::Error::from(ErrorKind::PermissionDenied)) - } else if output.is_empty() { - Ok(()) - } else { - Err(io::Error::new(ErrorKind::Other, output)) - } - } else { - Ok(()) - } + Ok(()) } pub fn execute_in_sh(cmd: &'static str) -> io::Result<()> { execute_and_display("sh", ["-c", cmd]) } + +pub fn execute_quiet, I>(cmd: S, args: I) -> io::Result +where + I: IntoIterator, + I::Item: AsRef, +{ + Command::new(cmd).args(args).output() +}