Add error handling for pacman update

This commit is contained in:
Luke Harding 2024-04-24 15:35:53 -04:00
parent 3c2b2479b5
commit 61b5343d43
2 changed files with 37 additions and 5 deletions

View File

@ -11,6 +11,8 @@
This file contains the primary logic of the application
*/
use std::io;
mod pacman_install_check;
mod shell_commands;
@ -22,9 +24,21 @@ fn main() {
return;
}
println!("Here we do things");
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;
}
}
}
shell_commands::execute_in_sh("sudo pacman -Syu").unwrap();
println!("Move onto something else");
}
fn copyright_notice() -> &'static str {

View File

@ -13,18 +13,36 @@
use std::ffi::OsStr;
use std::io;
use std::process::Command;
use std::io::{ErrorKind, Read};
use std::process::{Command, Stdio};
pub fn execute_and_display<S: AsRef<OsStr>, I>(cmd: S, args: I) -> io::Result<()>
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(())
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(())
}
}
pub fn execute_in_sh(cmd: &'static str) -> io::Result<()> {