refactor and choose install

This commit is contained in:
2024-08-04 22:53:15 +02:00
parent f2f3997469
commit 085debe43c
10 changed files with 65 additions and 16 deletions

View File

@@ -35,6 +35,9 @@ pub enum Commands {
#[command(alias = "f", about = "Find packages")] #[command(alias = "f", about = "Find packages")]
Find(Query), Find(Query),
#[command(alias = "fi", about = "Find and install packages")]
ChooseInstall(Query),
} }
#[derive(Args)] #[derive(Args)]

View File

@@ -15,7 +15,8 @@ fn main() {
Some(cli::Commands::Sync { noconfirm }) => packages::sync(noconfirm), Some(cli::Commands::Sync { noconfirm }) => packages::sync(noconfirm),
Some(cli::Commands::Remove(PackageList { packages })) => packages::remove(packages), Some(cli::Commands::Remove(PackageList { packages })) => packages::remove(packages),
Some(cli::Commands::Find(Query { query })) => packages::find(query), Some(cli::Commands::Find(Query { query })) => packages::find(query),
None => packages::rebuild(true), Some(cli::Commands::ChooseInstall(Query { query })) => packages::choose_install(query),
None => packages::upgrade(true),
}; };
if let Err(err) = result { if let Err(err) = result {

View File

@@ -0,0 +1,50 @@
use colored::Colorize;
use std::io::Write;
use std::process::{Command, Stdio};
use crate::packages::PACKAGE_MANAGER;
pub fn choose_install(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 mut child = Command::new(PACKAGE_MANAGER)
.arg("--color")
.arg("always")
.arg("s")
.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 word in query.clone() {
writeln!(stdin, "{}", word).unwrap();
}
}
let status = child.wait().expect("Failed to wait on child");
if !status.success() {
return Err("Failed to install packages".into());
}
println!("{} {}", "::".bold().green(), "Packages installed".bold());
println!(
"{} {}",
"::".bold().red(),
"Package index has not been updated!".bold()
);
Ok(())
}

View File

@@ -1,7 +1,7 @@
use colored::Colorize; use colored::Colorize;
use std::process::Command; use std::process::Command;
const PACKAGE_MANAGER: &str = "paru"; use crate::packages::PACKAGE_MANAGER;
pub fn find(query: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { pub fn find(query: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
println!( println!(

View File

@@ -1,10 +1,9 @@
use crate::packages::PACKAGE_MANAGER;
use crate::{file, packages::get_package_path}; use crate::{file, packages::get_package_path};
use colored::Colorize; use colored::Colorize;
use std::io::Write; use std::io::Write;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
const PACKAGE_MANAGER: &str = "paru";
pub fn install(new_packages: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { pub fn install(new_packages: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
println!( println!(
"{} {}", "{} {}",
@@ -24,7 +23,6 @@ pub fn install(new_packages: Vec<String>) -> Result<(), Box<dyn std::error::Erro
.arg("always") .arg("always")
.arg("-S") .arg("-S")
.arg("--needed") .arg("--needed")
// .arg(noconfirm)
.arg("-") .arg("-")
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::inherit()) .stdout(Stdio::inherit())

View File

@@ -2,6 +2,7 @@ use colored::Colorize;
use std::io::{self, Write}; use std::io::{self, Write};
use std::path::PathBuf; use std::path::PathBuf;
pub mod choose_install;
pub mod find; pub mod find;
pub mod install; pub mod install;
pub mod rebuild; pub mod rebuild;
@@ -9,6 +10,7 @@ pub mod remove;
pub mod sync; pub mod sync;
pub mod upgrade; pub mod upgrade;
pub use choose_install::choose_install;
pub use find::find; pub use find::find;
pub use install::install; pub use install::install;
pub use rebuild::rebuild; pub use rebuild::rebuild;
@@ -16,6 +18,8 @@ pub use remove::remove;
pub use sync::sync; pub use sync::sync;
pub use upgrade::upgrade; pub use upgrade::upgrade;
const PACKAGE_MANAGER: &str = "paru";
fn get_package_path() -> PathBuf { fn get_package_path() -> PathBuf {
let home_dir = std::env::var("HOME").unwrap(); let home_dir = std::env::var("HOME").unwrap();

View File

@@ -3,9 +3,7 @@ use std::io::Write;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use crate::file; use crate::file;
use crate::packages::{ask_confirmation, get_package_path}; use crate::packages::{ask_confirmation, get_package_path, PACKAGE_MANAGER};
const PACKAGE_MANAGER: &str = "paru";
pub fn rebuild(noconfirm: bool) -> Result<(), Box<dyn std::error::Error>> { pub fn rebuild(noconfirm: bool) -> Result<(), Box<dyn std::error::Error>> {
println!( println!(

View File

@@ -1,10 +1,9 @@
use crate::packages::PACKAGE_MANAGER;
use crate::{file, packages::get_package_path}; use crate::{file, packages::get_package_path};
use colored::Colorize; use colored::Colorize;
use std::io::Write; use std::io::Write;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
const PACKAGE_MANAGER: &str = "paru";
pub fn remove(unwanted_packages: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { pub fn remove(unwanted_packages: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
println!("{} {}", "::".bold().green(), "Removing packages...".bold()); println!("{} {}", "::".bold().green(), "Removing packages...".bold());

View File

@@ -3,9 +3,7 @@ use std::io::Write;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use crate::file; use crate::file;
use crate::packages::{ask_confirmation, get_package_path}; use crate::packages::{ask_confirmation, get_package_path, PACKAGE_MANAGER};
const PACKAGE_MANAGER: &str = "paru";
pub fn sync(noconfirm: bool) -> Result<(), Box<dyn std::error::Error>> { pub fn sync(noconfirm: bool) -> Result<(), Box<dyn std::error::Error>> {
println!("{} {}", "::".bold().green(), "Syncing packages...".bold()); println!("{} {}", "::".bold().green(), "Syncing packages...".bold());

View File

@@ -1,9 +1,7 @@
use colored::Colorize; use colored::Colorize;
use std::process::Command; use std::process::Command;
use crate::packages::ask_confirmation; use crate::packages::{ask_confirmation, PACKAGE_MANAGER};
const PACKAGE_MANAGER: &str = "paru";
pub fn upgrade(noconfirm: bool) -> Result<(), Box<dyn std::error::Error>> { pub fn upgrade(noconfirm: bool) -> Result<(), Box<dyn std::error::Error>> {
println!("{} {}", "::".bold().green(), "Upgrading packages...".bold()); println!("{} {}", "::".bold().green(), "Upgrading packages...".bold());