Add error handling for pacman update
This commit is contained in:
parent
3c2b2479b5
commit
61b5343d43
18
src/main.rs
18
src/main.rs
@ -11,6 +11,8 @@
|
|||||||
This file contains the primary logic of the application
|
This file contains the primary logic of the application
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use std::io;
|
||||||
|
|
||||||
mod pacman_install_check;
|
mod pacman_install_check;
|
||||||
mod shell_commands;
|
mod shell_commands;
|
||||||
|
|
||||||
@ -22,9 +24,21 @@ fn main() {
|
|||||||
return;
|
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 {
|
fn copyright_notice() -> &'static str {
|
||||||
|
@ -13,18 +13,36 @@
|
|||||||
|
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::io;
|
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<()>
|
pub fn execute_and_display<S: AsRef<OsStr>, I>(cmd: S, args: I) -> io::Result<()>
|
||||||
where
|
where
|
||||||
I: IntoIterator,
|
I: IntoIterator,
|
||||||
I::Item: AsRef<OsStr>,
|
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()?;
|
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(())
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(io::Error::new(ErrorKind::Other, output))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execute_in_sh(cmd: &'static str) -> io::Result<()> {
|
pub fn execute_in_sh(cmd: &'static str) -> io::Result<()> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user