149 lines
5.5 KiB
Rust
149 lines
5.5 KiB
Rust
use std::path::{PathBuf, Path};
|
|
use std::io::BufReader;
|
|
use std::io::prelude::*;
|
|
use std::fs::{self, File};
|
|
use std::env;
|
|
use super::std;
|
|
|
|
/// Creates a `PackageManager` struct to hold in the needed data
|
|
/// to run our commands.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct PackageManager<'a> {
|
|
pub name: &'a str,
|
|
pub search: Option<Vec<&'a str>>,
|
|
pub install: Option<Vec<&'a str>>,
|
|
pub uninstall: Option<Vec<&'a str>>,
|
|
pub sup: Option<Vec<&'a str>>,
|
|
pub purge: Option<Vec<&'a str>>,
|
|
pub update: Option<Vec<&'a str>>,
|
|
pub upgrade: Option<Vec<&'a str>>,
|
|
pub cache_clear: Option<Vec<&'a str>>,
|
|
pub complete_cache_clear: Option<Vec<&'a str>>,
|
|
pub exe: PathBuf,
|
|
}
|
|
|
|
/// This holds the various implementations of `PackageManagers` so far.
|
|
impl<'a> PackageManager<'a> {
|
|
pub fn pacmatic() -> PackageManager<'a> {
|
|
PackageManager {
|
|
name: "pacmatic",
|
|
search: Some(vec!["pacmatic", "-Ss"]),
|
|
install: Some(vec!["pacmatic", "-S"]),
|
|
uninstall: Some(vec!["pacmatic", "-R"]),
|
|
sup: Some(vec!["pacmatic", "-Syu"]),
|
|
purge: Some(vec!["pacmatic", "-Rdns"]),
|
|
upgrade: Some(vec!["pacmatic", "-Su"]),
|
|
update: Some(vec!["pacmatic", "-Sy"]),
|
|
cache_clear: Some(vec!["pacmatic", "-Sc"]),
|
|
complete_cache_clear: Some(vec!["pacmatic", "-Scc"]),
|
|
exe: PathBuf::from("/bin/pacmatic"),
|
|
}
|
|
}
|
|
pub fn pacman() -> PackageManager<'a> {
|
|
PackageManager {
|
|
name: "pacman",
|
|
search: Some(vec!["pacman", "-Ss"]),
|
|
install: Some(vec!["pacman", "-S"]),
|
|
uninstall: Some(vec!["pacman", "-R"]),
|
|
sup: Some(vec!["pacman", "-Syu"]),
|
|
purge: Some(vec!["pacman", "-Rdns"]),
|
|
upgrade: Some(vec!["pacman", "-Su"]),
|
|
update: Some(vec!["pacman", "-Sy"]),
|
|
cache_clear: Some(vec!["pacman", "-Sc"]),
|
|
complete_cache_clear: Some(vec!["pacman", "-Scc"]),
|
|
exe: PathBuf::from("/bin/pacman"),
|
|
}
|
|
}
|
|
|
|
pub fn apt() -> PackageManager<'a> {
|
|
PackageManager {
|
|
name: "apt",
|
|
search: Some(vec!["apt", "search"]),
|
|
install: Some(vec!["apt", "install"]),
|
|
uninstall:Some(vec!["apt", "remove"]),
|
|
sup: Some(vec!["apt", "update", "upgrade"]),
|
|
purge: Some(vec!["apt", "purge"]),
|
|
upgrade: Some(vec!["apt", "upgrade"]),
|
|
update: Some(vec!["apt", "update"]),
|
|
cache_clear: Some(vec!["apt", "clean"]),
|
|
complete_cache_clear: Some(vec!["apt-get", "autoclean"]),
|
|
exe: PathBuf::from("/usr/bin/apt"),
|
|
}
|
|
}
|
|
|
|
pub fn xbps() -> PackageManager<'a> {
|
|
PackageManager {
|
|
name: "xbps",
|
|
search: Some(vec!["xbps-query", "-Rs"]),
|
|
install: Some(vec!["xbps-install", "-S"]),
|
|
uninstall: Some(vec!["xbps-remove"]),
|
|
sup: Some(vec!["xbps-install", "-Su"]),
|
|
purge: Some(vec!["xbps-remove", "-R"]),
|
|
upgrade: Some(vec!["xbps-install", "-Su"]),
|
|
update: Some(vec!["xbps-install"]),
|
|
cache_clear: Some(vec!["xbps-remove", "-0"]),
|
|
complete_cache_clear: Some(vec!["xbps-remove", "-0"]),
|
|
exe: PathBuf::from("bin/xbps-install"),
|
|
}
|
|
}
|
|
// 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());
|
|
});
|
|
conf.push("rux.conf");
|
|
let mut file = File::create(conf)?;
|
|
file.write_all(self.name.as_bytes())?;
|
|
Ok(())
|
|
}
|
|
pub fn all() -> Vec<PackageManager<'a>> {
|
|
let pacmatic = PackageManager::pacmatic();
|
|
let pacman = PackageManager::pacman();
|
|
let aptget = PackageManager::apt();
|
|
let xbps = PackageManager::xbps();
|
|
|
|
vec![pacmatic, pacman, aptget, xbps]
|
|
}
|
|
}
|
|
|
|
/// 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<'a, P: AsRef<Path>>(ruxconf: P) -> Result<PackageManager<'a>, std::io::Error> {
|
|
/*
|
|
let mut home = env::home_dir().unwrap_or_else(|| {
|
|
eprintln!("Cannot load file, home directory cannot be found");
|
|
std::process::exit(1)
|
|
});
|
|
home.push(".config/rux/rux.conf");
|
|
*/
|
|
let file = File::open(&ruxconf)?;
|
|
let buffered = BufReader::new(file);
|
|
for line in buffered.lines() {
|
|
match &*line.unwrap() {
|
|
"pacmatic" => return Ok(PackageManager::pacmatic()),
|
|
"pacman" => return Ok(PackageManager::pacman()),
|
|
"apt" => return Ok(PackageManager::apt()),
|
|
"xbps" => return Ok(PackageManager::xbps()),
|
|
_ => {
|
|
eprintln!("Default either not set or has been changed");
|
|
std::fs::remove_file(ruxconf).unwrap_or_else(|err| {
|
|
eprintln!("Failed to delete: {:?}", err);
|
|
});
|
|
std::process::exit(1)
|
|
}
|
|
}
|
|
}
|
|
Ok(PackageManager::default())
|
|
}
|