mirror of
https://github.com/eRgo35/ah.git
synced 2025-12-16 14:56:10 +01:00
package upgrade, refacror
This commit is contained in:
@@ -5,4 +5,4 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.10", features = ["derive"] }
|
clap = { version = "4.5.10", features = ["derive"] }
|
||||||
colored = "2.1.0"
|
colored = "2.1.0"
|
||||||
60
src/cli.rs
60
src/cli.rs
@@ -1,27 +1,49 @@
|
|||||||
use clap::{Parser, Subcommand, Args};
|
use clap::{Args, Parser, Subcommand};
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(
|
||||||
|
name = "ah",
|
||||||
|
author = "Michał Czyż",
|
||||||
|
version = "0.1.0",
|
||||||
|
about = "A declarative package manager for Arch Linux",
|
||||||
|
long_about = "Arch Helper is a declarative package management tool for Arch Linux. It leverages paru or other package managers for seamless integration.")]
|
||||||
pub struct Cli {
|
pub struct Cli {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
pub command: Option<SubCommands>,
|
pub command: Option<Commands>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subcommand, Debug)]
|
#[derive(Subcommand)]
|
||||||
pub enum SubCommands {
|
pub enum Commands {
|
||||||
#[command(alias = "i")]
|
#[command(about = "Install packages")]
|
||||||
Install(PackageArg),
|
Install(PackageList),
|
||||||
#[command(alias = "u")]
|
|
||||||
Upgrade {},
|
#[command(about = "Upgrade packages")]
|
||||||
#[command(alias = "s")]
|
Upgrade {
|
||||||
Sync {},
|
#[arg(help = "Don't prompt for confirmation", default_value_t = false)]
|
||||||
#[command(alias = "r")]
|
noconfirm: bool
|
||||||
Remove(PackageArg),
|
},
|
||||||
#[command(alias = "f")]
|
|
||||||
Find(PackageArg),
|
#[command(about = "Synchronize packages")]
|
||||||
|
Sync {
|
||||||
|
#[arg(help = "Don't prompt for confirmation", default_value_t = false)]
|
||||||
|
noconfirm: bool
|
||||||
|
},
|
||||||
|
|
||||||
|
#[command(about = "Remove packages")]
|
||||||
|
Remove(PackageList),
|
||||||
|
|
||||||
|
#[command(about = "Find packages")]
|
||||||
|
Find(Query),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args)]
|
||||||
pub struct PackageArg {
|
pub struct PackageList {
|
||||||
pub package: String,
|
#[arg(help = "Name(s) of the package(s), separated by spaces")]
|
||||||
|
pub packages: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Args)]
|
||||||
|
pub struct Query {
|
||||||
|
#[arg(help = "Search term for finding packages")]
|
||||||
|
pub query: Vec<String>,
|
||||||
}
|
}
|
||||||
23
src/file.rs
Normal file
23
src/file.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
use std::{
|
||||||
|
fs::File,
|
||||||
|
io::{prelude::*, BufReader}, path::PathBuf,
|
||||||
|
};
|
||||||
|
|
||||||
|
// pub fn read_config(path: &str) -> Vec<String> {
|
||||||
|
// todo!();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn write_config(path: &str, content: &str) {
|
||||||
|
// todo!();
|
||||||
|
// }
|
||||||
|
|
||||||
|
pub fn read_packages(path: PathBuf) -> Vec<String> {
|
||||||
|
let file = File::open(path).expect("Failed to open file");
|
||||||
|
let buf = BufReader::new(file);
|
||||||
|
|
||||||
|
buf.lines().map(|l| l.expect("Failed to read line")).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub fn write_packages(path: &str, content: &str) {
|
||||||
|
// todo!();
|
||||||
|
// }
|
||||||
22
src/main.rs
22
src/main.rs
@@ -1,17 +1,25 @@
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use cli::{PackageList, Query};
|
||||||
|
use colored::Colorize;
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
mod packages;
|
mod packages;
|
||||||
|
mod file;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cli = cli::Cli::parse();
|
let cli = cli::Cli::parse();
|
||||||
|
|
||||||
match cli.command {
|
let result = match cli.command {
|
||||||
Some(cli::SubCommands::Install(arg)) => packages::install(&arg.package),
|
Some(cli::Commands::Install(PackageList { packages })) => packages::install(packages),
|
||||||
Some(cli::SubCommands::Upgrade {}) => packages::upgrade(),
|
Some(cli::Commands::Upgrade { noconfirm }) => packages::upgrade(noconfirm),
|
||||||
Some(cli::SubCommands::Sync {}) => packages::sync(),
|
Some(cli::Commands::Sync { noconfirm }) => packages::sync(noconfirm),
|
||||||
Some(cli::SubCommands::Remove(arg)) => packages::remove(&arg.package),
|
Some(cli::Commands::Remove(PackageList { packages })) => packages::remove(packages),
|
||||||
Some(cli::SubCommands::Find(arg)) => packages::find(&arg.package),
|
Some(cli::Commands::Find(Query { query })) => packages::find(query),
|
||||||
None => packages::sync(),
|
None => packages::rebuild(true),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(err) = result {
|
||||||
|
eprintln!("{} {}", "::".bold().red(), err.to_string().bold());
|
||||||
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
use colored::Colorize;
|
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
const PACKAGE_MANAGER: &str = "paru";
|
|
||||||
|
|
||||||
pub fn sync() {
|
|
||||||
println!("{} {}", "::".bold().green(), "Syncing packages...".bold());
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn upgrade() {
|
|
||||||
println!("{} {}", "::".bold().green(), "Upgrading packages...".bold());
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn install(package: &str) {
|
|
||||||
println!("{} {}", "::".bold().green(), "Installing packages...".bold());
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove(package: &str) {
|
|
||||||
println!("{} {}", "::".bold().green(), "Removing packages...".bold());
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn find(package: &str) {
|
|
||||||
println!("{} {}", "::".bold().green(), "Looking for package...".bold());
|
|
||||||
|
|
||||||
let output = Command::new(PACKAGE_MANAGER)
|
|
||||||
.arg("--color")
|
|
||||||
.arg("always")
|
|
||||||
.arg("-Ss")
|
|
||||||
.arg(package)
|
|
||||||
.output()
|
|
||||||
.expect("Failed to execute command");
|
|
||||||
|
|
||||||
print!("{}", String::from_utf8_lossy(&output.stdout));
|
|
||||||
}
|
|
||||||
24
src/packages/find.rs
Normal file
24
src/packages/find.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
use colored::Colorize;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
const PACKAGE_MANAGER: &str = "paru";
|
||||||
|
|
||||||
|
pub fn find(query: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("{} {}", "::".bold().green(), "Looking for package...".bold());
|
||||||
|
|
||||||
|
if query.is_empty() {
|
||||||
|
return Err("No query provided".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = Command::new(PACKAGE_MANAGER)
|
||||||
|
.arg("--color")
|
||||||
|
.arg("always")
|
||||||
|
.arg("-Ss")
|
||||||
|
.args(query)
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
print!("{}", String::from_utf8_lossy(&output.stdout));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
6
src/packages/install.rs
Normal file
6
src/packages/install.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
use colored::Colorize;
|
||||||
|
|
||||||
|
pub fn install(_package: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("{} {}", "::".bold().green(), "Installing packages...".bold());
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
34
src/packages/mod.rs
Normal file
34
src/packages/mod.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
use colored::Colorize;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
pub mod rebuild;
|
||||||
|
pub mod sync;
|
||||||
|
pub mod upgrade;
|
||||||
|
pub mod install;
|
||||||
|
pub mod remove;
|
||||||
|
pub mod find;
|
||||||
|
|
||||||
|
pub use rebuild::rebuild;
|
||||||
|
pub use sync::sync;
|
||||||
|
pub use upgrade::upgrade;
|
||||||
|
pub use install::install;
|
||||||
|
pub use remove::remove;
|
||||||
|
pub use find::find;
|
||||||
|
|
||||||
|
fn get_package_path() -> PathBuf {
|
||||||
|
let home_dir = std::env::var("HOME").unwrap();
|
||||||
|
|
||||||
|
PathBuf::from(home_dir).join("packages")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ask_confirmation() -> Result<bool, io::Error> {
|
||||||
|
print!("{} {}", "::".bold().blue(), "Do you want to continue? [Y/n] ");
|
||||||
|
io::stdout().flush()?;
|
||||||
|
|
||||||
|
let mut input = String::new();
|
||||||
|
io::stdin().read_line(&mut input)?;
|
||||||
|
|
||||||
|
let input = input.trim().to_lowercase();
|
||||||
|
Ok(input.is_empty() || input == "y")
|
||||||
|
}
|
||||||
52
src/packages/rebuild.rs
Normal file
52
src/packages/rebuild.rs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
use colored::Colorize;
|
||||||
|
use std::process::{Command, Stdio};
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use crate::file;
|
||||||
|
use crate::packages::{ask_confirmation, get_package_path};
|
||||||
|
|
||||||
|
const PACKAGE_MANAGER: &str = "paru";
|
||||||
|
|
||||||
|
pub fn rebuild(noconfirm: bool) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("{} {}", "::".bold().green(), "Upgrading & syncing packages...".bold());
|
||||||
|
|
||||||
|
if !ask_confirmation()? {
|
||||||
|
return Err("Operation aborted".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let packages = file::read_packages(get_package_path());
|
||||||
|
|
||||||
|
let packages = packages.into_iter()
|
||||||
|
.filter(|p| !p.contains("#") && !p.is_empty())
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
|
let noconfirm = if noconfirm { "--noconfirm" } else { "--confirm" };
|
||||||
|
|
||||||
|
let mut child = Command::new(PACKAGE_MANAGER)
|
||||||
|
.arg("--color")
|
||||||
|
.arg("always")
|
||||||
|
.arg("-Syu")
|
||||||
|
.arg("--needed")
|
||||||
|
.arg(noconfirm)
|
||||||
|
.arg("-")
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.stdout(Stdio::inherit())
|
||||||
|
.stderr(Stdio::inherit())
|
||||||
|
.spawn()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
if let Some(mut stdin) = child.stdin.take() {
|
||||||
|
for package in packages {
|
||||||
|
writeln!(stdin, "{}", package).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let status = child.wait().expect("Failed to wait on child");
|
||||||
|
|
||||||
|
if !status.success() {
|
||||||
|
return Err("Failed to upgrade & sync packages".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{} {}", "::".bold().green(), "Packages upgraded & synced".bold());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
6
src/packages/remove.rs
Normal file
6
src/packages/remove.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
use colored::Colorize;
|
||||||
|
|
||||||
|
pub fn remove(_package: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("{} {}", "::".bold().green(), "Removing packages...".bold());
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
48
src/packages/sync.rs
Normal file
48
src/packages/sync.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
use colored::Colorize;
|
||||||
|
use std::process::{Command, Stdio};
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use crate::file;
|
||||||
|
use crate::packages::get_package_path;
|
||||||
|
|
||||||
|
const PACKAGE_MANAGER: &str = "paru";
|
||||||
|
|
||||||
|
pub fn sync(noconfirm: bool) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("{} {}", "::".bold().green(), "Syncing packages...".bold());
|
||||||
|
|
||||||
|
let packages = file::read_packages(get_package_path());
|
||||||
|
|
||||||
|
let packages = packages.into_iter()
|
||||||
|
.filter(|p| !p.contains("#") && !p.is_empty())
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
|
let noconfirm = if noconfirm { "--noconfirm" } else { "--confirm" };
|
||||||
|
|
||||||
|
let mut child = Command::new(PACKAGE_MANAGER)
|
||||||
|
.arg("--color")
|
||||||
|
.arg("always")
|
||||||
|
.arg("-S")
|
||||||
|
.arg("--needed")
|
||||||
|
.arg(noconfirm)
|
||||||
|
.arg("-")
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.stdout(Stdio::inherit())
|
||||||
|
.stderr(Stdio::inherit())
|
||||||
|
.spawn()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
if let Some(mut stdin) = child.stdin.take() {
|
||||||
|
for package in packages {
|
||||||
|
writeln!(stdin, "{}", package).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let status = child.wait().expect("Failed to wait on child");
|
||||||
|
|
||||||
|
if !status.success() {
|
||||||
|
return Err("Failed to sync packages".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{} {}", "::".bold().green(), "Packages synced".bold());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
27
src/packages/upgrade.rs
Normal file
27
src/packages/upgrade.rs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
use colored::Colorize;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
const PACKAGE_MANAGER: &str = "paru";
|
||||||
|
|
||||||
|
pub fn upgrade(noconfirm: bool) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("{} {}", "::".bold().green(), "Upgrading packages...".bold());
|
||||||
|
|
||||||
|
let noconfirm = if noconfirm { "--noconfirm" } else { "--confirm" };
|
||||||
|
|
||||||
|
let mut child = Command::new(PACKAGE_MANAGER)
|
||||||
|
.arg("--color")
|
||||||
|
.arg("always")
|
||||||
|
.arg("-Syu")
|
||||||
|
.arg(noconfirm)
|
||||||
|
.spawn()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
let status = child.wait().expect("Failed to wait on child");
|
||||||
|
|
||||||
|
if !status.success() {
|
||||||
|
return Err("Failed to upgrade packages".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{} {}", "::".bold().green(), "Packages upgraded".bold());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user