From 985c53237c5b327d92f507b859235b3ca2b032ae Mon Sep 17 00:00:00 2001 From: Beefki Date: Sun, 15 Oct 2017 10:22:05 -0500 Subject: [PATCH] Multicommand actions should now work --- src/config.rs | 67 +++++++++++++++++------------- src/main.rs | 12 ++++-- src/packagemanager.rs | 97 +++++++++++++++++++++++-------------------- 3 files changed, 96 insertions(+), 80 deletions(-) diff --git a/src/config.rs b/src/config.rs index dfe487c..3c5a5b0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,39 +10,46 @@ impl Config { /// Actually runs the package manager. Checks what command was requested /// and loads the appropriate data from the passed `PackageManager`. /// Finally calls the program and passes the appropriate arguments - pub fn run(&self, pac: PackageManager) -> Child { - let prog = match self.action { - Management::Search => (pac.search.0, pac.search.1), - Management::Install => (pac.install.0, pac.install.1), - Management::Uninstall => (pac.uninstall.0, pac.uninstall.1), - Management::Remove => (pac.purge.0, pac.purge.1), - Management::Update => (pac.update.0, pac.update.1), - Management::Upgrade => (pac.upgrade.0, pac.upgrade.1), - Management::Full => (pac.sup.0, pac.sup.1), - Management::Clear => (pac.cache_clear.0, pac.cache_clear.1), - Management::CompleteClear => (pac.complete_cache_clear.0, pac.complete_cache_clear.1), + pub fn run(&self, pac: PackageManager) { + let vec = match self.action { + Management::Search => pac.search, + Management::Install => pac.install, + Management::Uninstall => pac.uninstall, + Management::Remove => pac.purge, + Management::Update => pac.update, + Management::Upgrade => pac.upgrade, + Management::Full => pac.sup, + Management::Clear => pac.cache_clear, + Management::CompleteClear => pac.complete_cache_clear, }; - match (&prog.1, &self.term) { - (&None, &None) => { - Command::new(&prog.0).spawn().expect( - "Failed to call package manager", - ) + let mut prog = match vec { + None => { + println!("Sorry, your package manager does not support this action"); + std::process::exit(0); + }, + Some(x) => x, + }; + + let command = prog.remove(0); + + let term = match &self.term { + &None => "", + &Some(ref val) => val, + }; + + if prog.first().is_none() { + let mut child = Command::new(&command).spawn().expect("Broken?"); + child.wait().expect("Failed to wait on child"); + }else if self.term.is_none() { + while prog.first().is_some() { + let mut child = Command::new(&command).arg(prog.remove(0)).spawn().expect("Broken2?"); + child.wait().expect("Failed to wait on child"); } - (&Some(ref arg), &None) => { - Command::new(&prog.0).arg(arg).spawn().expect( - "Failed to call package manager", - ) - } - (&Some(ref arg1), &Some(ref arg2)) => { - Command::new(&prog.0).arg(arg1).arg(arg2).spawn().expect( - "Failed to call package manager", - ) - } - _ => { - println!("{:?}, {:?}", &prog.1, &self.term); - println!("Arguments formed incorrectly, no action taken"); - std::process::exit(1) + }else { + while prog.first().is_some() { + let mut child = Command::new(&command).arg(prog.remove(0)).arg(term).spawn().expect("Broken3?"); + child.wait().expect("Failed to wait on child"); } } } diff --git a/src/main.rs b/src/main.rs index 609f16e..2aed628 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use management::Management; mod config; use config::Config; -use std::process::{Command, Child}; +use std::process::{Command};//, Child}; use std::io; use std::path::Path; @@ -22,8 +22,9 @@ fn main() { if Path::new("default.conf").is_file() { let pac = read_default(); - let mut child = config.run(pac); - child.wait().expect("Failed to wait on child"); + config.run(pac); + //let mut child = config.run(pac); + //child.wait().expect("Failed to wait on child"); std::process::exit(0); } @@ -32,8 +33,8 @@ fn main() { let aptget = PackageManager::apt(); let xbps = PackageManager::xbps(); - let managers: Vec = vec![pacmatic, pacman, aptget, xbps]; + //let managers: Vec = vec![pacmatic]; let mut found: bool = false; for prog in managers { @@ -64,8 +65,11 @@ fn main() { } } + /* let mut child = config.run(prog); child.wait().expect("Failed to wait on child"); + */ + config.run(prog); } } } diff --git a/src/packagemanager.rs b/src/packagemanager.rs index 6fa3837..d7c81c4 100644 --- a/src/packagemanager.rs +++ b/src/packagemanager.rs @@ -10,18 +10,20 @@ use super::*; // Maybe a Vec? // Currently actions that require multiple inputs will not work // such as `apt-get update && apt-get upgrade` +// Option> used to handle package managers that don't support +// that type of action at all, user will be notified. #[derive(Debug, Clone, Default)] pub struct PackageManager { pub name: String, - pub search: (String, Option), - pub install: (String, Option), - pub uninstall: (String, Option), - pub sup: (String, Option), - pub purge: (String, Option), - pub update: (String, Option), - pub upgrade: (String, Option), - pub cache_clear: (String, Option), - pub complete_cache_clear: (String, Option), + pub search: Option>, + pub install: Option>, + pub uninstall: Option>, + pub sup: Option>, + pub purge: Option>, + pub update: Option>, + pub upgrade: Option>, + pub cache_clear: Option>, + pub complete_cache_clear: Option>, pub exe: PathBuf, } @@ -30,15 +32,16 @@ impl PackageManager { pub fn pacmatic() -> PackageManager { PackageManager { name: "pacmatic".to_string(), - search: ("pacmatic".to_string(), Some("-Ss".to_string())), - install: ("pacmatic".to_string(), Some("-S".to_string())), - uninstall: ("pacmatic".to_string(), Some("-R".to_string())), - sup: ("pacmatic".to_string(), Some("-Syu".to_string())), - purge: ("pacmatic".to_string(), Some("-Rdns".to_string())), - upgrade: ("pacmatic".to_string(), Some("-Su".to_string())), - update: ("pacmatic".to_string(), Some("-Sy".to_string())), - cache_clear: ("pacmatic".to_string(), Some("-Sc".to_string())), - complete_cache_clear: ("pacmatic".to_string(), Some("-Scc".to_string())), + search: Some(vec!["pacmatic".to_string(), "-Ss".to_string()]), + install: Some(vec!["pacmatic".to_string(), "-S".to_string()]), + uninstall: Some(vec!["pacmatic".to_string(), "-R".to_string()]), + sup: Some(vec!["pacmatic".to_string(), "-Syu".to_string()]), + //sup: Some(vec!["pacmatic".to_string(), "-Sy".to_string(), "-Su".to_string()]), + purge: Some(vec!["pacmatic".to_string(), "-Rdns".to_string()]), + upgrade: Some(vec!["pacmatic".to_string(), "-Su".to_string()]), + update: Some(vec!["pacmatic".to_string(), "-Sy".to_string()]), + cache_clear: Some(vec!["pacmatic".to_string(), "-Sc".to_string()]), + complete_cache_clear: Some(vec!["pacmatic".to_string(), "-Scc".to_string()]), exe: PathBuf::from("/bin/pacmatic"), } } @@ -46,15 +49,15 @@ impl PackageManager { pub fn pacman() -> PackageManager { PackageManager { name: "pacman".to_string(), - search: ("pacman".to_string(), Some("-Ss".to_string())), - install: ("pacman".to_string(), Some("-S".to_string())), - uninstall: ("pacman".to_string(), Some("-R".to_string())), - sup: ("pacman".to_string(), Some("-Syu".to_string())), - purge: ("pacman".to_string(), Some("-Rdns".to_string())), - upgrade: ("pacman".to_string(), Some("-Su".to_string())), - update: ("pacman".to_string(), Some("-Sy".to_string())), - cache_clear: ("pacman".to_string(), Some("-Sc".to_string())), - complete_cache_clear: ("pacman".to_string(), Some("-Scc".to_string())), + search: Some(vec!["pacman".to_string(), "-Ss".to_string()]), + install: Some(vec!["pacman".to_string(), "-S".to_string()]), + uninstall: Some(vec!["pacman".to_string(), "-R".to_string()]), + sup: Some(vec!["pacman".to_string(), "-Syu".to_string()]), + purge: Some(vec!["pacman".to_string(), "-Rdns".to_string()]), + upgrade: Some(vec!["pacman".to_string(), "-Su".to_string()]), + update: Some(vec!["pacman".to_string(), "-Sy".to_string()]), + cache_clear: Some(vec!["pacman".to_string(), "-Sc".to_string()]), + complete_cache_clear: Some(vec!["pacman".to_string(), "-Scc".to_string()]), exe: PathBuf::from("/bin/pacman"), } } @@ -62,15 +65,15 @@ impl PackageManager { pub fn apt() -> PackageManager { PackageManager { name: "apt".to_string(), - search: ("apt".to_string(), Some("search".to_string())), - install: ("apt".to_string(), Some("install".to_string())), - uninstall: ("apt".to_string(), Some("remove".to_string())), - sup: ("apt".to_string(), Some("update".to_string())), - purge: ("apt".to_string(), Some("purge".to_string())), - upgrade: ("apt".to_string(), Some("upgrade".to_string())), - update: ("apt".to_string(), Some("update".to_string())), - cache_clear: ("apt".to_string(), Some("clean".to_string())), - complete_cache_clear: ("apt-get".to_string(), Some("autoclean".to_string())), + search: Some(vec!["apt".to_string(), "search".to_string()]), + install: Some(vec!["apt".to_string(), "install".to_string()]), + uninstall:Some(vec!["apt".to_string(), "remove".to_string()]), + sup: Some(vec!["apt".to_string(), "update".to_string(), "upgrade".to_string()]), + purge: Some(vec!["apt".to_string(), "purge".to_string()]), + upgrade: Some(vec!["apt".to_string(), "upgrade".to_string()]), + update: Some(vec!["apt".to_string(), "update".to_string()]), + cache_clear: Some(vec!["apt".to_string(), "clean".to_string()]), + complete_cache_clear: Some(vec!["apt-get".to_string(), "autoclean".to_string()]), exe: PathBuf::from("/usr/bin/apt"), } } @@ -78,19 +81,19 @@ impl PackageManager { pub fn xbps() -> PackageManager { PackageManager { name: "xbps".to_string(), - search: ("xbps-query".to_string(), Some("-Rs".to_string())), - install: ("xbps-install".to_string(), Some("-S".to_string())), - uninstall: ("xbps-remove".to_string(), None), - sup: ("xbps-install".to_string(), Some("-Su".to_string())), - purge: ("xbps-remove".to_string(), Some("-R".to_string())), - upgrade: ("xbps-install".to_string(), Some("-Su".to_string())), - update: ("xbps-install".to_string(), None), - cache_clear: ("xbps-remove".to_string(), Some("-0".to_string())), - complete_cache_clear: ("xbps-remove".to_string(), Some("-0".to_string())), + search: Some(vec!["xbps-query".to_string(), "-Rs".to_string()]), + install: Some(vec!["xbps-install".to_string(), "-S".to_string()]), + uninstall: Some(vec!["xbps-remove".to_string()]), + sup: Some(vec!["xbps-install".to_string(), "-Su".to_string()]), + purge: Some(vec!["xbps-remove".to_string(), "-R".to_string()]), + upgrade: Some(vec!["xbps-install".to_string(), "-Su".to_string()]), + update: Some(vec!["xbps-install".to_string()]), + cache_clear: Some(vec!["xbps-remove".to_string(), "-0".to_string()]), + complete_cache_clear: Some(vec!["xbps-remove".to_string(), "-0".to_string()]), exe: PathBuf::from("bin/xbps-install"), } } - // allows setting the default.conf + // Allows setting the default.conf // TODO: put default.conf somewhere it actually belongs rather than in // the current folder. pub fn set_default(&self) -> std::io::Result<()> { @@ -110,9 +113,11 @@ pub fn read_default() -> PackageManager { for line in buffered.lines() { match &*line.unwrap() { "pacmatic" => return PackageManager::pacmatic(), + /* "pacman" => return PackageManager::pacman(), "apt" => return PackageManager::apt(), "xbps" => return PackageManager::xbps(), + */ _ => { println!("Default either not set or has been changed"); std::process::exit(1)