diff --git a/src/main.rs b/src/main.rs index f66251e..b0b1b9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,6 @@ use colored::{ColoredString, Colorize}; -mod install_check; mod pacman_api; mod shell_commands; @@ -30,6 +29,7 @@ fn main() { notice_println("Running pacman update"); if let Err(e) = pacman_api::update_all() { error_println(e.to_string()); + return; } notice_println("Move onto something else"); diff --git a/src/pacman_api.rs b/src/pacman_api.rs index 0d72f03..aa400f7 100644 --- a/src/pacman_api.rs +++ b/src/pacman_api.rs @@ -13,6 +13,7 @@ use std::{error, fmt}; use std::fmt::Formatter; +use std::io::Read; use std::path::Path; use crate::shell_commands; @@ -66,9 +67,26 @@ pub fn check() -> Result<(), PacmanError> { } pub fn update_all() -> Result<(), PacmanError> { - if let Err(e) = shell_commands::execute_in_sh("sudo pacman -Syu") { + let out = match shell_commands::execute_in_sh("pacman -Syu") { + Ok(out) => out, + Err(e) => return Err(PacmanError::Other(e.to_string())), + }; + + let mut child_stderr = match out.stderr { + Some(stderr) => stderr, + None => return Ok(()), + }; + + let mut stderr_out = String::new(); + if let Err(e) = child_stderr.read_to_string(&mut stderr_out) { return Err(PacmanError::Other(e.to_string())); } - Ok(()) + if stderr_out.is_empty() { + Ok(()) + } else if stderr_out == "error: you cannot perform this operation unless you are root.\n" { + Err(PacmanError::InsufficientPrivilege) + } else { + Err(PacmanError::Other(stderr_out)) + } } diff --git a/src/shell_commands.rs b/src/shell_commands.rs index fe4d2eb..c414fef 100644 --- a/src/shell_commands.rs +++ b/src/shell_commands.rs @@ -13,21 +13,24 @@ use std::ffi::OsStr; use std::io; -use std::process::{Command, Output}; +use std::process::{Child, Command, Output, Stdio}; -pub fn execute_and_display, I>(cmd: S, args: I) -> io::Result<()> +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 mut child = Command::new(cmd) + .args(args) + .stderr(Stdio::piped()) + .spawn()?; child.wait()?; - Ok(()) + Ok(child) } -pub fn execute_in_sh(cmd: &'static str) -> io::Result<()> { +pub fn execute_in_sh(cmd: &'static str) -> io::Result { execute_and_display("sh", ["-c", cmd]) }