rux/src/config.rs
2017-10-24 03:02:20 -05:00

83 lines
2.8 KiB
Rust

use super::*;
/// A struct to hold parsed arg data.
#[derive(Debug)]
pub struct Config {
pub action: Management,
pub term: Option<String>,
}
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) {
let get = 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,
};
let mut prog = match get {
None => {
println!("Sorry, your package manager does not support this action");
std::process::exit(0);
},
Some(vec) => vec,
};
let command = prog.remove(0);
match (prog.first(), &self.term) {
(None, _) => {
let mut child = Command::new(&command).spawn().expect(
"Failed to open package manager"
);
child.wait().expect("Failed to wait on package manager");
},
(_, &None) => {
for arg in prog.iter() {
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.iter() {
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
/// and term (if any). Requires an action, while the term can be `None`.
pub fn new() -> Config {
let action = match Management::parse(std::env::args().nth(1)) {
None => {
println!("No command actions passed");
help();
std::process::exit(0)
}
Some(command) => command,
};
let term = std::env::args().nth(2);
Config {
action: action,
term: term,
}
}
}