You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
amethyst/src/main.rs

128 lines
4.8 KiB
Rust

mod mods;
use toml;
use serde;
use mods::{clearcache::clearcache, clone::clone, help::help, install::install, search::{a_search, r_search}, uninstall::uninstall, upgrade::upgrade, flatpak::flatpak, config::printconfig};
use std::{fs, fs::File, io::prelude::*, env, process::exit, process::Command};
3 years ago
#[derive(serde::Deserialize)]
struct General {
cache: Option<String>,
backends: Backends,
pacman: Pacman,
}
#[derive(serde::Deserialize)]
struct Backends {
pacman: Option<bool>,
flatpak: Option<bool>,
aur: Option<bool>,
}
#[derive(serde::Deserialize)]
struct Pacman {
noconfirm: Option<bool>,
}
3 years ago
fn main() {
let args: Vec<String> = env::args().collect();
let mut confile = File::open("/etc/ame.toml").expect("Unable to open the Config file, did you delete ame.toml from /etc/??");
let mut config = String::new();
let defaultconfig = format!(r#"
cache = "{}/.cache/ame"
[backends]
pacman = true
flatpak = true
aur = true
[pacman]
noconfirm = false
"#, std::env::var("HOME").unwrap());
let mut configfile: General = toml::from_str(&defaultconfig).unwrap();
if fs::read_to_string("/etc/ame.toml").expect("unable to open config file! (/etc/ame.toml)") != "" {
confile.read_to_string(&mut config).expect("Unable to read the Config file (/etc/ame.toml)");
let homepath = std::env::var("HOME").unwrap();
config=config.replace("~", &homepath);
configfile = toml::from_str(&config).unwrap();
}
if args.len() <= 1 {
help();
exit(1);
}
3 years ago
let oper = &args[1];
let cache_path=configfile.cache.unwrap();
if oper == "-S" || oper == "ins" || oper == "install" {
3 years ago
for arg in env::args().skip(2) {
if configfile.backends.pacman.unwrap() == true {
let out = Command::new("pacman").arg("-Ss").arg(&arg).status().unwrap();
if out.success() {
let configoption_noconfirm = configfile.pacman.noconfirm.unwrap();
install(configoption_noconfirm, &arg);
} else {
if configfile.backends.aur.unwrap() == true {
clone(&arg, &cache_path);
} else {
println!("ERROR: the package wasn't found in the repos and aur support is disabled");
println!("Please enable aur support if you wish to check if this package exists in the aur");
exit(1);
}
}
} else if configfile.backends.aur.unwrap() == true {
clone(&arg, &cache_path)
} else {
println!("ERROR: it seems like neither pacman, nor aur support is enabled!");
println!("Please enable either one of those option and try again");
exit(1);
}
}
} else if oper == "-R" || oper=="rem" || oper=="remove" {
3 years ago
for arg in env::args().skip(2) {
let configoption_noconfirm = configfile.pacman.noconfirm.unwrap();
uninstall(configoption_noconfirm, &arg);
3 years ago
}
} else if oper == "-Syu" || oper=="upg" || oper=="upgrade" {
let configoption_noconfirm = configfile.pacman.noconfirm.unwrap();
upgrade(configoption_noconfirm, &cache_path);
} else if oper == "-Ss" || oper=="sear" || oper=="search" {
for arg in env::args().skip(2) {
r_search(&arg);
a_search(&arg);
}
} else if oper == "-Sa" || oper=="sera" || oper=="search-aur" {
for arg in env::args().skip(2) {
a_search(&arg);
}
} else if oper == "-Sr" || oper=="serr" || oper=="search-rep" {
for arg in env::args().skip(2) {
r_search(&arg);
}
} else if oper == "-Cc" || oper=="clrca" || oper=="clear-cache" {
clearcache();
} else if oper == "-f" || oper=="flat" || oper=="flatpak" {
if configfile.backends.flatpak.unwrap() == true {
let b = std::path::Path::new("/usr/bin/flatpak").exists();
if b == true {
for arg in env::args().skip(2) {
flatpak(&arg);
}
} else {
println!("ERROR: flatpak not found, please install flatpak and try again!");
println!("If you do have flatpak installed, please open an issue on the ame github repo!");
exit(1);
}
} else {
println!("ERROR: flatpak support is disabled in your ame config!");
println!("Enable flatpak support in your configuration and try again!");
exit(1);
}
} else if oper == "-Pc" || oper=="pricon" || oper=="printconf" {
printconfig();
} else {
help();
exit(0);
3 years ago
}
}