Default saves now, other code tweaks

This commit is contained in:
Beefki 2017-10-25 20:11:38 -05:00
parent fd16e0c21b
commit 8e43af7913
2 changed files with 59 additions and 29 deletions

View File

@ -9,7 +9,7 @@ use config::Config;
use std::process::{Command};//, Child};
use std::io;
use std::path::Path;
use std::path::PathBuf;
fn input() -> Result<String, io::Error> {
let mut val = String::new();
@ -20,18 +20,21 @@ fn input() -> Result<String, io::Error> {
fn main() {
let config = Config::new();
if Path::new("default.conf").is_file() {
let pac = read_default();
let mut ruxconf = std::env::home_dir().unwrap_or_else(|| {
PathBuf::new()
});
ruxconf.push(".config/rux/rux.conf");
if ruxconf.is_file() {
let pac = read_default().unwrap_or_else(|err| {
println!("{:?}", err);
std::process::exit(1)
});
config.run(pac);
std::process::exit(0);
}
let pacmatic = PackageManager::pacmatic();
let pacman = PackageManager::pacman();
let aptget = PackageManager::apt();
let xbps = PackageManager::xbps();
let managers: Vec<PackageManager> = vec![pacmatic, pacman, aptget, xbps];
let managers = PackageManager::all();
let mut found: bool = false;
for prog in managers {
@ -49,17 +52,12 @@ fn main() {
found = true;
println!("Would you like to set {} as default? [y/N]", prog.name);
if input().unwrap().trim().to_lowercase() == "y" {
let check = prog.set_default();
match check {
Ok(_) => println!("Default set"),
Err(err) => {
println!(
"An error occured while setting default:\
{}",
err
)
}
}
prog.set_default().unwrap_or_else(|err| {
println!(
"An error occursed trying to set default {:?}",
err.kind()
)
});
}
config.run(prog);
}

View File

@ -1,9 +1,12 @@
use std::path::PathBuf;
use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File;
use std::fs::{self, File};
use std::env;
use super::*;
use std::error::Error;
/// Creates a `PackageManager` struct to hold in the needed data
/// to run our commands.
#[derive(Debug, Clone, Default)]
@ -90,10 +93,31 @@ impl<'a> PackageManager<'a> {
// 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")?;
let mut home = 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| {
println!("! {:?}", why.kind());
});
println!("{:?}", &home.display());
home.push("rux.conf");
let mut file = File::create(home)?;
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`
@ -101,20 +125,28 @@ impl<'a> PackageManager<'a> {
/// If it doesn't recognize the file it will tell the user that there's an
/// issue before exiting.
// TODO: Have this delete malformed default.conf
pub fn read_default<'a>() -> PackageManager<'a> {
let file = File::open("default.conf").unwrap();
pub fn read_default<'a>() -> Result<PackageManager<'a>, Box<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(&home)?;
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(),
"pacmatic" => return Ok(PackageManager::pacmatic()),
"pacman" => return Ok(PackageManager::pacman()),
"apt" => return Ok(PackageManager::apt()),
"xbps" => return Ok(PackageManager::xbps()),
_ => {
println!("Default either not set or has been changed");
std::fs::remove_file(home).unwrap_or_else(|err| {
println!("Failed to delete: {:?}", err);
});
std::process::exit(1)
}
}
}
PackageManager::default()
Ok(PackageManager::default())
}