use std::path::PathBuf; use std::io::BufReader; use std::io::prelude::*; use std::fs::File; use super::*; /// Creates a `PackageManager` struct to hold in the needed data /// to run our commands. // TODO: figure out a better way to store and access the data. // Maybe a Vec? // Currently actions that require multiple inputs will not work // such as `apt-get update && apt-get upgrade` #[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 exe: PathBuf, } /// This holds the various implementations of `PackageManagers` so far. 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())), exe: PathBuf::from("/bin/pacmatic"), } } 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())), exe: PathBuf::from("/bin/pacman"), } } 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())), exe: PathBuf::from("/usr/bin/apt"), } } 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())), exe: PathBuf::from("bin/xbps-install"), } } // 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<()> { let mut file = File::create("default.conf")?; file.write_all(format!("{}", self.name).as_bytes())?; Ok(()) } } /// Reads from default.conf and returns the appropriate `PackageManager` /// Pulls the first line out of the default.conf and loads that. /// If it doesn't recognize the file it will tell the user that there's an /// issue before exiting. pub fn read_default() -> PackageManager { let file = File::open("default.conf").unwrap(); let buffered = BufReader::new(file); 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) } } } PackageManager::default() }