Pacman update_all() works and gives errors properly.

This commit is contained in:
Luke Harding 2024-04-28 00:47:20 -04:00
parent 8f46888688
commit 064081a0c3
3 changed files with 29 additions and 8 deletions

View File

@ -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");

View File

@ -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()));
}
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))
}
}

View File

@ -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<S: AsRef<OsStr>, I>(cmd: S, args: I) -> io::Result<()>
pub fn execute_and_display<S: AsRef<OsStr>, I>(cmd: S, args: I) -> io::Result<Child>
where
I: IntoIterator,
I::Item: AsRef<OsStr>,
{
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<Child> {
execute_and_display("sh", ["-c", cmd])
}