Compare commits

...

2 Commits

Author SHA1 Message Date
064081a0c3 Pacman update_all() works and gives errors properly. 2024-04-28 00:47:20 -04:00
8f46888688 Remove install_check 2024-04-28 00:46:37 -04:00
4 changed files with 29 additions and 46 deletions

View File

@ -1,38 +0,0 @@
/*
Rust Arch Linux Updater
Copyright (C) 2024 Luke Harding <luke@lukeh990.io>
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 <https://www.gnu.org/licenses/>.
*/
/*
install_check.rs
This module allows for checking if any file is present on the filesystem
*/
use std::fmt;
use std::fmt::Formatter;
use std::path::Path;
#[derive(Debug, Clone)]
pub struct NotInstalledError {
file_name: String,
}
impl fmt::Display for NotInstalledError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{} not found.", self.file_name)
}
}
pub fn run(path: impl Into<String>) -> Result<(), NotInstalledError> {
let path = path.into();
let exists = Path::new(&path).exists();
if exists {
Ok(())
} else {
Err(NotInstalledError { file_name: path })
}
}

View File

@ -13,7 +13,6 @@
use colored::{ColoredString, Colorize}; use colored::{ColoredString, Colorize};
mod install_check;
mod pacman_api; mod pacman_api;
mod shell_commands; mod shell_commands;
@ -30,6 +29,7 @@ fn main() {
notice_println("Running pacman update"); notice_println("Running pacman update");
if let Err(e) = pacman_api::update_all() { if let Err(e) = pacman_api::update_all() {
error_println(e.to_string()); error_println(e.to_string());
return;
} }
notice_println("Move onto something else"); notice_println("Move onto something else");

View File

@ -13,6 +13,7 @@
use std::{error, fmt}; use std::{error, fmt};
use std::fmt::Formatter; use std::fmt::Formatter;
use std::io::Read;
use std::path::Path; use std::path::Path;
use crate::shell_commands; use crate::shell_commands;
@ -66,9 +67,26 @@ pub fn check() -> Result<(), PacmanError> {
} }
pub fn update_all() -> 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())); 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))
}
} }

View File

@ -13,21 +13,24 @@
use std::ffi::OsStr; use std::ffi::OsStr;
use std::io; 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 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()?;
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]) execute_and_display("sh", ["-c", cmd])
} }