Compare commits
2 Commits
00542f11d3
...
064081a0c3
Author | SHA1 | Date | |
---|---|---|---|
064081a0c3 | |||
8f46888688 |
@ -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 })
|
||||
}
|
||||
}
|
@ -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");
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
@ -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])
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user