1 Commits

Author SHA1 Message Date
Beefki
ec50d9c6b1 Probably wrong file locations, but mostly complete 2017-10-26 01:15:04 -05:00
3 changed files with 134 additions and 57 deletions

View File

@@ -2,7 +2,6 @@ use super::{std, help, PackageManager};
use management::Management; use management::Management;
use std::process::Command; use std::process::Command;
/// A struct to hold parsed arg data. /// A struct to hold parsed arg data.
// TODO?: update to take mutltiple terms?
#[derive(Debug)] #[derive(Debug)]
pub struct Config { pub struct Config {
pub action: Management, pub action: Management,
@@ -26,48 +25,54 @@ impl Config {
Management::CompleteClear => pac.complete_cache_clear, Management::CompleteClear => pac.complete_cache_clear,
}; };
// takes `Option(Vec<&str>)` and returns `Vec<&str>` if `Some(_)` // takes `Some(Vec<&str>)` and returns `Vec<&str>`
// If `None`, tell user and exit. // checks to make sure the package manager supports the action
let mut prog = get.unwrap_or_else(|| { let mut prog = match get {
println!("Sorry, your package manager does not support this action"); None => {
std::process::exit(0) println!("Sorry, your package manager does not support this action");
}); std::process::exit(0)
},
Some(vec) => vec,
};
//Pop's from the front of the `Vec<&str>`, which should always be the command name //Pop's from the front of the `Vec<&str>`, which should always be the command name
let command = prog.remove(0); let command = prog.remove(0);
// Tuple match, `prog.first()` returns an `Option<T>` on the first element of // Tuple match, `prog.first()` returns an `Option<T>` on the first element of
// a Vec, `self.term` is also an `Option<T>` so this checks both for `Some`/`None` // a Vec, `self.term` is also an `Option<T>` so this checks both for `Some`/`None`
// and formats the program call as needed // and formats the program as needed
match (prog.first(), &self.term) { match (prog.first(), &self.term) {
(None, &None) => { (None, &None) => {
let mut child = Command::new(&command).spawn().expect( 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"); child.wait().expect("Failed to wait on package manager");
} },
(None, &Some(ref term)) => { (None, &Some(ref term)) => {
let mut child = Command::new(&command).arg(term).spawn().expect( let mut child = Command::new(&command)
"Failed to open package manager", .arg(term)
); .spawn()
.expect("Failed to open package manager");
child.wait().expect("Failed to wait on package manager"); child.wait().expect("Failed to wait on package manager");
} },
(Some(_), &None) => { (Some(_), &None) => {
for arg in &prog { for arg in &prog {
let mut child = Command::new(&command).arg(arg).spawn().expect( let mut child =Command::new(&command)
"Failed to open package manager", .arg(arg)
); .spawn()
.expect("Failed to open package manager");
child.wait().expect("Failed to wait on child"); child.wait().expect("Failed to wait on child");
} }
} },
(Some(_), &Some(ref term)) => { (Some(_), &Some(ref term)) => {
for arg in &prog { for arg in &prog {
let mut child = Command::new(&command).arg(arg).arg(term).spawn().expect( let mut child = Command::new(&command)
"Failed to open package manager", .arg(arg)
); .arg(term)
.spawn()
.expect("Failed to open package manager");
child.wait().expect("Failed to wait on child"); child.wait().expect("Failed to wait on child");
} }
} },
} }
} }
/// Creates a `Config` from passed arguments, parses the `Management` enum /// Creates a `Config` from passed arguments, parses the `Management` enum

View File

@@ -9,41 +9,31 @@ use config::Config;
use std::io; use std::io;
use std::path::PathBuf; use std::path::PathBuf;
/// Takes user input. `io::Result<T>` returns either `Ok(T)` fn input() -> Result<String, io::Error> {
/// or `Err(io::Error)` Error handling is common on inputs so brevity
/// is nice.
fn input() -> io::Result<String> {
let mut val = String::new(); let mut val = String::new();
io::stdin().read_line(&mut val)?; io::stdin().read_line(&mut val)?;
Ok(val) Ok(val)
} }
fn main() { fn main() {
//pulls in the args passed and stores them in a `Config` struct
let config = Config::new(); let config = Config::new();
//Sets a path to `~/.config/rux/` let mut ruxconf = std::env::home_dir().unwrap_or_else(|| {
let mut ruxconf = std::env::home_dir().unwrap_or_else(|| PathBuf::new()); PathBuf::new()
});
ruxconf.push(".config/rux/rux.conf"); 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() { if ruxconf.is_file() {
let pac = read_default(ruxconf).unwrap_or_else(|err| { let pac = read_default(ruxconf).unwrap_or_else(|err| {
eprintln!("{:?}", err); eprintln!("{:?}", err);
std::process::exit(1) std::process::exit(1)
}); });
config.run(pac); config.run(pac);
std::process::exit(0); std::process::exit(0);
} }
// Loads all PackageManagers into a Vec to search through
let managers = PackageManager::all(); let managers = PackageManager::all();
// Iterates through the `Vec<PackageManager>` 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; let mut found: bool = false;
for prog in managers { for prog in managers {
if found { if found {
@@ -61,7 +51,10 @@ fn main() {
println!("Would you like to set {} as default? [y/N]", prog.name); println!("Would you like to set {} as default? [y/N]", prog.name);
if input().unwrap().trim().to_lowercase() == "y" { if input().unwrap().trim().to_lowercase() == "y" {
prog.set_default().unwrap_or_else(|err| { prog.set_default().unwrap_or_else(|err| {
println!("An error occurred trying to set default {:?}", err.kind()) println!(
"An error occursed trying to set default {:?}",
err.kind()
)
}); });
} }
config.run(prog); config.run(prog);

View File

@@ -12,7 +12,7 @@ pub struct PackageManager<'a> {
pub name: &'a str, pub name: &'a str,
pub search: Option<Vec<&'a str>>, pub search: Option<Vec<&'a str>>,
pub install: Option<Vec<&'a str>>, pub install: Option<Vec<&'a str>>,
pub uninstall: Option<Vec<&'a str>>, pub uninstall: Option<Vec<&'a str>>,
pub sup: Option<Vec<&'a str>>, pub sup: Option<Vec<&'a str>>,
pub purge: Option<Vec<&'a str>>, pub purge: Option<Vec<&'a str>>,
pub update: Option<Vec<&'a str>>, pub update: Option<Vec<&'a str>>,
@@ -60,7 +60,7 @@ impl<'a> PackageManager<'a> {
name: "apt", name: "apt",
search: Some(vec!["apt", "search"]), search: Some(vec!["apt", "search"]),
install: Some(vec!["apt", "install"]), install: Some(vec!["apt", "install"]),
uninstall: Some(vec!["apt", "remove"]), uninstall:Some(vec!["apt", "remove"]),
sup: Some(vec!["apt", "update", "upgrade"]), sup: Some(vec!["apt", "update", "upgrade"]),
purge: Some(vec!["apt", "purge"]), purge: Some(vec!["apt", "purge"]),
upgrade: Some(vec!["apt", "upgrade"]), upgrade: Some(vec!["apt", "upgrade"]),
@@ -86,33 +86,112 @@ impl<'a> PackageManager<'a> {
exe: PathBuf::from("bin/xbps-install"), exe: PathBuf::from("bin/xbps-install"),
} }
} }
// Allows setting the default `rux.conf` in folder `.config/rux/` in the pub fn brew() -> PackageManager<'a> {
// user's home directory. Will ask once per user, including sudo. PackageManager{
name: "brew",
search: Some(vec!["brew", "search"]),
install: Some(vec!["brew", "install"]),
uninstall: Some(vec!["brew", "uninstall"]),
sup: Some(vec!["brew", "update", "upgrade"]),
purge: Some(vec!["brew", "remove"]),
upgrade: Some(vec!["brew", "upgrade"]),
update: Some(vec!["brew", "update"]),
cache_clear: Some(vec!["brew", "cleanup"]),
complete_cache_clear: None,
exe: PathBuf::from("/bin/brew/"),
}
}
pub fn dnf() -> PackageManager<'a> {
PackageManager{
name: "dnf",
search: Some(vec!["dnf", "search"]),
install: Some(vec!["dnf", "install"]),
uninstall: Some(vec!["dnf", "remove"]),
sup: Some(vec!["dnf", "--refresh upgrade"]),
purge: Some(vec!["dnf", "autoremove"]),
upgrade: Some(vec!["dnf", "upgrade"]),
update: Some(vec!["dnf", "--refresh check-update"]),
cache_clear: Some(vec!["dnf", "clean all"]),
complete_cache_clear: None,
exe: PathBuf::from("/bin/dnf"),
}
}
pub fn eopkg() -> PackageManager<'a> {
PackageManager{
name: "eopkg",
search: Some(vec!["eopkg", "sr"]),
install: Some(vec!["eopkg", "it"]),
uninstall: Some(vec!["eopkg", "rm"]),
sup: Some(vec!["eopkg", "up"]),
purge: Some(vec!["eopkg", "rm --purge"]),
upgrade: Some(vec!["eopkg", "up"]),
update: Some(vec!["eopkg", "ur"]),
cache_clear: Some(vec!["eopkg", "dc"]),
complete_cache_clear: None,
exe: PathBuf::from("/bin/eopkg"),
}
}
pub fn yum() -> PackageManager<'a> {
PackageManager{
name: "yum",
search: Some(vec!["yum", "search"]),
install: Some(vec!["yum", "install"]),
uninstall: Some(vec!["yum", "remove"]),
sup: Some(vec!["yum", "check-update", "update"]),
purge: Some(vec!["yum", "remove"]),
upgrade: Some(vec!["yum", "update"]),
update: Some(vec!["yum", "check-update"]),
cache_clear: Some(vec!["yum", "autoremove"]),
complete_cache_clear: None,
exe: PathBuf::from("/bin/yum"),
}
}
/* Template to copy paste the bare minimum
pub fn name() -> PackageManager<'a> {
PackageManager{
name: "",
search: Some(vec![]),
install: Some(vec![]),
uninstall: Some(vec![]),
sup: Some(vec![]),
purge: Some(vec![]),
upgrade: Some(vec![]),
update: Some(vec![]),
cache_clear: Some(vec![]),
complete_cache_clear: Some(vec![]),
exe: PathBuf::from(""),
}
}
*/
// Allows setting the default in rux.conf
pub fn set_default(&self) -> std::io::Result<()> { pub fn set_default(&self) -> std::io::Result<()> {
let conf = env::home_dir().unwrap_or_else(|| { let mut home = match env::home_dir() {
println!("Could not find home directory. Default cannot be saved properly!"); Some(path) => path,
std::process::exit(1) 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| {
println!("! {:?}", why.kind());
}); });
home.push("rux.conf");
let conf = conf.join(".config").join("rux"); let mut file = File::create(home)?;
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())?; file.write_all(self.name.as_bytes())?;
Ok(()) Ok(())
} }
pub fn all() -> Vec<PackageManager<'a>> { pub fn all() -> Vec<PackageManager<'a>> {
let pacmatic = PackageManager::pacmatic(); let pacmatic = PackageManager::pacmatic();
let pacman = PackageManager::pacman(); let pacman = PackageManager::pacman();
let aptget = PackageManager::apt(); let aptget = PackageManager::apt();
let xbps = PackageManager::xbps(); let xbps = PackageManager::xbps();
let brew = PackageManager::brew();
let dnf = PackageManager::dnf();
let eopkg = PackageManager::eopkg();
let yum = PackageManager::yum();
vec![pacmatic, pacman, aptget, xbps] vec![pacmatic, pacman, aptget, xbps, brew, dnf, eopkg, yum]
} }
} }