package upgrade, refacror

This commit is contained in:
2024-07-24 18:58:11 +02:00
parent 0d0c544088
commit b11271cf82
12 changed files with 277 additions and 65 deletions

24
src/packages/find.rs Normal file
View 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
View 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
View 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
View 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
View 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
View 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
View 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(())
}