Compare commits

...

14 Commits

Author SHA1 Message Date
4b96063e78 Merge branch 'main' of git.lukeh990.io:luke/rust-archlinux-update
All checks were successful
Build on Release / cargo-build (push) Successful in 8s
2024-04-30 16:33:21 -04:00
426ef923e0 Update README and add gitea workflow 2024-04-30 16:32:15 -04:00
8b79dd83bb Work on #1 and #4 2024-04-30 16:32:15 -04:00
2466c99e76 Update README.md 2024-04-30 16:06:12 -04:00
d7f62ef690 Move pacman_api; Fix #11; Changes to orphan check 2024-04-30 12:49:34 -04:00
160ad7f1e2 Add ability to remove unused packages 2024-04-28 12:48:57 -04:00
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
00542f11d3 Work on #10 & New implementation for #9 2024-04-27 16:01:47 -04:00
61b5343d43 Add error handling for pacman update 2024-04-24 15:35:53 -04:00
3c2b2479b5 Add shell command framework #6
Added the shell_commands module that will either let you run a command or automatically use `sh - c` to run a command.
2024-04-24 14:16:27 -04:00
12bf969d41 Update README 2024-04-24 12:43:47 -04:00
0d5d2df810 Add file summaries 2024-04-24 12:42:00 -04:00
e890e89d98 Add pacman install check 2024-04-24 01:57:45 -04:00
6 changed files with 278 additions and 4 deletions

85
Cargo.lock generated
View File

@ -2,6 +2,91 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "colored"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
dependencies = [
"lazy_static",
"windows-sys",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "rust-archlinux-update"
version = "0.1.0"
dependencies = [
"colored",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

View File

@ -7,3 +7,4 @@ license = "GPL-3"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
colored = "2.1.0"

1
src/apis/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod pacman;

98
src/apis/pacman.rs Normal file
View File

@ -0,0 +1,98 @@
/*
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/>.
*/
/*
pacman_api.rs
This module provides an api to make working with Pacman much easier.
*/
use std::{error, fmt, result};
use std::fmt::Formatter;
use std::path::Path;
use crate::shell_commands;
type Result<T> = result::Result<T, Box<dyn error::Error>>;
#[derive(Debug, Clone)]
enum PacmanError {
NotInstalled,
ExitCode(i32),
Other(&'static str),
}
impl fmt::Display for PacmanError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let out = match self {
PacmanError::NotInstalled => "Pacman not installed.".to_string(),
PacmanError::ExitCode(code) => format!("Pacman exited with an exit code: {}", code),
PacmanError::Other(msg) => msg.to_string(),
};
write!(f, "PacmanError: {}", out)
}
}
impl error::Error for PacmanError {}
pub fn check() -> Result<()> {
let out = shell_commands::execute_quiet("which", ["pacman"])?;
let path = if out.status.success() {
let mut stdout = out.stdout;
stdout.pop(); // Remove \n from stdout
String::from_utf8(stdout)?
} else {
String::from("/usr/bin/pacman")
};
let path = Path::new(&path);
if !path.exists() {
return Err(PacmanError::NotInstalled.into());
}
Ok(())
}
pub fn update_all() -> Result<()> {
let exit_status = shell_commands::execute_in_sh("sudo pacman -Syu")?;
if !exit_status.success() {
let exit_code = match exit_status.code() {
Some(exit_code) => exit_code,
None => return Err(PacmanError::Other("No Exit Code Found").into()),
};
return Err(PacmanError::ExitCode(exit_code).into());
}
Ok(())
}
pub fn get_unused() -> Result<Vec<String>> {
let out = shell_commands::execute_quiet("pacman", ["-Qtdq"])?;
let result = String::from_utf8(out.stdout)?;
let pkgs_to_remove: Vec<String> = result.lines().map(|x| x.to_string()).collect();
Ok(pkgs_to_remove)
}
pub fn remove_unused(packages: Vec<String>) -> Result<()> {
let package_string = packages.join(" ");
let command = format!("sudo pacman -Rns {}", package_string);
let out = shell_commands::execute_in_sh(command)?;
dbg!(&out);
Ok(())
}

View File

@ -6,12 +6,61 @@
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
fn main() -> Result<(), Box<dyn std::error::Error>> {
/*
main.rs
This file contains the primary logic of the application
*/
use colored::{ColoredString, Colorize};
use apis::pacman;
mod apis;
mod shell_commands;
fn main() {
println!("{}", copyright_notice());
Ok(())
if let Err(e) = pacman::check() {
error_println(e.to_string());
return;
} else {
notice_println("Pacman is installed\n")
}
notice_println("Running pacman update");
if let Err(e) = pacman::update_all() {
error_println(e.to_string());
return;
}
notice_println("Getting orphaned packages");
let result = match pacman::get_unused() {
Ok(result) => result,
Err(e) => {
error_println(e.to_string());
return;
}
};
if result.is_empty() {
println!("No Orphaned Packages Found.")
} else if let Err(e) = pacman::remove_unused(result) {
error_println(e.to_string());
return;
}
notice_println("\nUpdate process complete!");
}
fn copyright_notice() -> &'static str {
"Rust Arch Linux Updater Copyright (C) 2024 Luke Harding <luke@lukeh990.io>\nThis program comes with ABSOLUTELY NO WARRANTY\nThis is free software, and you are welcome to redistribute it under certain conditions"
fn copyright_notice() -> ColoredString {
"Rust Arch Linux Updater Copyright (C) 2024 Luke Harding <luke@lukeh990.io>\nThis program comes with ABSOLUTELY NO WARRANTY\nThis is free software, and you are welcome to redistribute it under certain conditions\n".italic()
}
pub fn error_println(msg: impl Into<String>) {
eprintln!("{}", msg.into().red().bold())
}
pub fn notice_println(msg: impl Into<String>) {
println!("{}", msg.into().green().bold())
}

40
src/shell_commands.rs Normal file
View File

@ -0,0 +1,40 @@
/*
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/>.
*/
/*
shell_commands.rs
This file contains utility functions to interact with the shell.
*/
use std::ffi::OsStr;
use std::io;
use std::process::{Command, ExitStatus, Output};
pub fn execute_and_display<S: AsRef<OsStr>, I>(cmd: S, args: I) -> io::Result<ExitStatus>
where
I: IntoIterator,
I::Item: AsRef<OsStr>,
{
let mut child = Command::new(cmd).args(args).spawn()?;
let exit_status = child.wait()?;
Ok(exit_status)
}
pub fn execute_in_sh(cmd: impl Into<String>) -> io::Result<ExitStatus> {
execute_and_display("sh", ["-c", &cmd.into()])
}
pub fn execute_quiet<S: AsRef<OsStr>, I>(cmd: S, args: I) -> io::Result<Output>
where
I: IntoIterator,
I::Item: AsRef<OsStr>,
{
Command::new(cmd).args(args).output()
}