From 493ca63f07f50e58d8da32f3cf6e3dc15023ee82 Mon Sep 17 00:00:00 2001 From: Beefki Date: Fri, 27 Oct 2017 17:39:52 -0500 Subject: [PATCH 1/5] Small changes --- src/main.rs | 4 ++-- src/packagemanager.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index ab93cb8..4f98d49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use config::Config; use std::io; use std::path::PathBuf; -fn input() -> Result { +fn input() -> io::Result { let mut val = String::new(); io::stdin().read_line(&mut val)?; Ok(val) @@ -52,7 +52,7 @@ fn main() { if input().unwrap().trim().to_lowercase() == "y" { prog.set_default().unwrap_or_else(|err| { println!( - "An error occursed trying to set default {:?}", + "An error occurred trying to set default {:?}", err.kind() ) }); diff --git a/src/packagemanager.rs b/src/packagemanager.rs index 74a4919..90c05d7 100644 --- a/src/packagemanager.rs +++ b/src/packagemanager.rs @@ -88,19 +88,19 @@ impl<'a> PackageManager<'a> { } // Allows setting the default in rux.conf pub fn set_default(&self) -> std::io::Result<()> { - let mut home = match env::home_dir() { + let mut conf = match env::home_dir() { Some(path) => path, None => { println!("Could not find home! Default cannot set properly!"); std::process::exit(1) } }; - home.push(".config/rux/"); - fs::create_dir_all(&home).unwrap_or_else(|why| { + conf.push(".config/rux/"); + fs::create_dir_all(&conf).unwrap_or_else(|why| { println!("! {:?}", why.kind()); }); - home.push("rux.conf"); - let mut file = File::create(home)?; + conf.push("rux.conf"); + let mut file = File::create(conf)?; file.write_all(self.name.as_bytes())?; Ok(()) } From b8895723061561c469aa26395ff136010de9c2a4 Mon Sep 17 00:00:00 2001 From: Beefki Date: Fri, 27 Oct 2017 17:43:26 -0500 Subject: [PATCH 2/5] Comments --- src/packagemanager.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/packagemanager.rs b/src/packagemanager.rs index 90c05d7..0e6fa1e 100644 --- a/src/packagemanager.rs +++ b/src/packagemanager.rs @@ -86,7 +86,8 @@ impl<'a> PackageManager<'a> { exe: PathBuf::from("bin/xbps-install"), } } - // Allows setting the default in rux.conf + // Allows setting the default `rux.conf` in folder `.config/rux/` in the + // user's home directory. Will ask once per user, including sudo. pub fn set_default(&self) -> std::io::Result<()> { let mut conf = match env::home_dir() { Some(path) => path, From 9892fefd2f808e0fe7f86990ac3b59a1557599bc Mon Sep 17 00:00:00 2001 From: Beefki Date: Sat, 28 Oct 2017 14:45:09 -0500 Subject: [PATCH 3/5] Some comments and minor edits --- src/config.rs | 19 +++++++++---------- src/main.rs | 12 ++++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/config.rs b/src/config.rs index ad416db..c783585 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,7 @@ use super::{std, help, PackageManager}; use management::Management; use std::process::Command; /// A struct to hold parsed arg data. +// TODO?: update to take mutltiple terms? #[derive(Debug)] pub struct Config { pub action: Management, @@ -25,21 +26,19 @@ impl Config { Management::CompleteClear => pac.complete_cache_clear, }; - // takes `Some(Vec<&str>)` and returns `Vec<&str>` - // checks to make sure the package manager supports the action - let mut prog = match get { - None => { - println!("Sorry, your package manager does not support this action"); - std::process::exit(0) - }, - Some(vec) => vec, - }; + // takes `Option(Vec<&str>)` and returns `Vec<&str>` if `Some(_)` + // If `None`, tell user and exit. + let mut prog = get.unwrap_or_else(|| { + println!("Sorry, your package manager does not support this action"); + std::process::exit(0) + }); //Pop's from the front of the `Vec<&str>`, which should always be the command name let command = prog.remove(0); + // Tuple match, `prog.first()` returns an `Option` on the first element of // a Vec, `self.term` is also an `Option` so this checks both for `Some`/`None` - // and formats the program as needed + // and formats the program call as needed match (prog.first(), &self.term) { (None, &None) => { let mut child = Command::new(&command).spawn().expect( diff --git a/src/main.rs b/src/main.rs index 4f98d49..2507c04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,9 @@ use config::Config; use std::io; use std::path::PathBuf; +/// Takes user input. `io::Result` returns either `Ok(T)` +/// or `Err(io::Error)` Error handling is common on inputs so brevity +/// is nice. fn input() -> io::Result { let mut val = String::new(); io::stdin().read_line(&mut val)?; @@ -16,13 +19,17 @@ fn input() -> io::Result { } fn main() { + //pulls in the args passed and stores them in a `Config` struct let config = Config::new(); + //Sets a path to `~/.config/rux/` let mut ruxconf = std::env::home_dir().unwrap_or_else(|| { PathBuf::new() }); ruxconf.push(".config/rux/rux.conf"); + // Checks if `~/.config/rux/rux.conf` exists and skips loading any + // other package manager and searching for them. if ruxconf.is_file() { let pac = read_default(ruxconf).unwrap_or_else(|err| { eprintln!("{:?}", err); @@ -32,8 +39,13 @@ fn main() { std::process::exit(0); } + // Loads all PackageManagers into a Vec to search through let managers = PackageManager::all(); + // Iterates through the `Vec` until it finds a match, + // asks the user if they want to use that manager, and if they want to + // set it as default to ALWAYS use that manager. Finally sends all the + // needed information to run the package manager let mut found: bool = false; for prog in managers { if found { From 9542d122b505d077d117c09a9c2a5a232455604d Mon Sep 17 00:00:00 2001 From: Beefki Date: Tue, 7 Nov 2017 21:07:22 -0600 Subject: [PATCH 4/5] Better beahvior setting default --- src/packagemanager.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/packagemanager.rs b/src/packagemanager.rs index 0e6fa1e..102faa0 100644 --- a/src/packagemanager.rs +++ b/src/packagemanager.rs @@ -89,22 +89,23 @@ impl<'a> PackageManager<'a> { // Allows setting the default `rux.conf` in folder `.config/rux/` in the // user's home directory. Will ask once per user, including sudo. pub fn set_default(&self) -> std::io::Result<()> { - let mut conf = match env::home_dir() { - Some(path) => path, - None => { - println!("Could not find home! Default cannot set properly!"); - std::process::exit(1) - } - }; - conf.push(".config/rux/"); - fs::create_dir_all(&conf).unwrap_or_else(|why| { - println!("! {:?}", why.kind()); + let conf = env::home_dir().unwrap_or_else(|| { + println!("Could not find home directory. Default cannot be saved properly!"); + std::process::exit(1) }); - conf.push("rux.conf"); + + let conf = conf.join(".config").join("rux"); + fs::create_dir_all(&conf).unwrap_or_else(|err| { + println!("Error setting default: {}", err); + return () + }); + let conf = conf.join("rux.conf"); + let mut file = File::create(conf)?; file.write_all(self.name.as_bytes())?; Ok(()) } + pub fn all() -> Vec> { let pacmatic = PackageManager::pacmatic(); let pacman = PackageManager::pacman(); From a92f51e20a81efc4759f35bf599d38c71fb2bcf3 Mon Sep 17 00:00:00 2001 From: Beefki Date: Tue, 7 Nov 2017 21:27:32 -0600 Subject: [PATCH 5/5] Better formatting --- src/config.rs | 32 ++++++++++++++------------------ src/main.rs | 13 ++++--------- src/packagemanager.rs | 8 ++++---- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/config.rs b/src/config.rs index c783585..385e076 100644 --- a/src/config.rs +++ b/src/config.rs @@ -42,36 +42,32 @@ impl Config { match (prog.first(), &self.term) { (None, &None) => { let mut child = Command::new(&command).spawn().expect( - "Failed to open package manager" + "Failed to open package manager", ); child.wait().expect("Failed to wait on package manager"); - }, + } (None, &Some(ref term)) => { - let mut child = Command::new(&command) - .arg(term) - .spawn() - .expect("Failed to open package manager"); + let mut child = Command::new(&command).arg(term).spawn().expect( + "Failed to open package manager", + ); child.wait().expect("Failed to wait on package manager"); - }, + } (Some(_), &None) => { for arg in &prog { - let mut child =Command::new(&command) - .arg(arg) - .spawn() - .expect("Failed to open package manager"); + let mut child = Command::new(&command).arg(arg).spawn().expect( + "Failed to open package manager", + ); child.wait().expect("Failed to wait on child"); } - }, + } (Some(_), &Some(ref term)) => { for arg in &prog { - let mut child = Command::new(&command) - .arg(arg) - .arg(term) - .spawn() - .expect("Failed to open package manager"); + let mut child = Command::new(&command).arg(arg).arg(term).spawn().expect( + "Failed to open package manager", + ); child.wait().expect("Failed to wait on child"); } - }, + } } } /// Creates a `Config` from passed arguments, parses the `Management` enum diff --git a/src/main.rs b/src/main.rs index 2507c04..2776f42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,9 +23,7 @@ fn main() { let config = Config::new(); //Sets a path to `~/.config/rux/` - let mut ruxconf = std::env::home_dir().unwrap_or_else(|| { - PathBuf::new() - }); + let mut ruxconf = std::env::home_dir().unwrap_or_else(|| PathBuf::new()); ruxconf.push(".config/rux/rux.conf"); // Checks if `~/.config/rux/rux.conf` exists and skips loading any @@ -34,7 +32,7 @@ fn main() { let pac = read_default(ruxconf).unwrap_or_else(|err| { eprintln!("{:?}", err); std::process::exit(1) - }); + }); config.run(pac); std::process::exit(0); } @@ -42,7 +40,7 @@ fn main() { // Loads all PackageManagers into a Vec to search through let managers = PackageManager::all(); - // Iterates through the `Vec` until it finds a match, + // Iterates through the `Vec` until it finds a match, // asks the user if they want to use that manager, and if they want to // set it as default to ALWAYS use that manager. Finally sends all the // needed information to run the package manager @@ -63,10 +61,7 @@ fn main() { println!("Would you like to set {} as default? [y/N]", prog.name); if input().unwrap().trim().to_lowercase() == "y" { prog.set_default().unwrap_or_else(|err| { - println!( - "An error occurred trying to set default {:?}", - err.kind() - ) + println!("An error occurred trying to set default {:?}", err.kind()) }); } config.run(prog); diff --git a/src/packagemanager.rs b/src/packagemanager.rs index 102faa0..55c003e 100644 --- a/src/packagemanager.rs +++ b/src/packagemanager.rs @@ -12,7 +12,7 @@ pub struct PackageManager<'a> { pub name: &'a str, pub search: Option>, pub install: Option>, - pub uninstall: Option>, + pub uninstall: Option>, pub sup: Option>, pub purge: Option>, pub update: Option>, @@ -60,7 +60,7 @@ impl<'a> PackageManager<'a> { name: "apt", search: Some(vec!["apt", "search"]), install: Some(vec!["apt", "install"]), - uninstall:Some(vec!["apt", "remove"]), + uninstall: Some(vec!["apt", "remove"]), sup: Some(vec!["apt", "update", "upgrade"]), purge: Some(vec!["apt", "purge"]), upgrade: Some(vec!["apt", "upgrade"]), @@ -86,7 +86,7 @@ impl<'a> PackageManager<'a> { exe: PathBuf::from("bin/xbps-install"), } } - // Allows setting the default `rux.conf` in folder `.config/rux/` in the + // Allows setting the default `rux.conf` in folder `.config/rux/` in the // user's home directory. Will ask once per user, including sudo. pub fn set_default(&self) -> std::io::Result<()> { let conf = env::home_dir().unwrap_or_else(|| { @@ -97,7 +97,7 @@ impl<'a> PackageManager<'a> { let conf = conf.join(".config").join("rux"); fs::create_dir_all(&conf).unwrap_or_else(|err| { println!("Error setting default: {}", err); - return () + return (); }); let conf = conf.join("rux.conf");