preperation for codebase audit

i18n
axtloss 3 years ago
parent 27b5367dee
commit ea63e29c73
No known key found for this signature in database
GPG Key ID: CB97071D2AF98474

@ -4,6 +4,27 @@ 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};
// Code audit notes from axtlos:
/* Maybe we could change the code style for the if..elif..else structure so that it isnt
if <condition> {
} elif <condition> {
}
but rather
if <condition>
{
} elif <condition>
{
}
So that the code is a bit more readable
We should also check where we can "merge" variables or create new ones
*/
#[derive(serde::Deserialize)]
struct General {
cache: Option<String>,
@ -25,7 +46,7 @@ struct Pacman {
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 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"
@ -40,7 +61,7 @@ fn main() {
"#, 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)") != "" {
if fs::read_to_string("/etc/ame.toml").expect("unable to open config file! (/etc/ame.toml)") != "" { //maybe print out a warning when the config file is empty so that the user knows the hardcoded one is being used
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);
@ -57,7 +78,7 @@ fn main() {
if oper == "-S" || oper == "ins" || oper == "install" {
for arg in env::args().skip(2) {
if configfile.backends.pacman.unwrap() == true {
let out = Command::new("pacman").arg("-Ss").arg(&arg).status().unwrap();
let out = Command::new("pacman").arg("-Ss").arg(&arg).status().unwrap(); // How do we silence this command?? using > /dev/null seems to also silence the returncode which is needed here
if out.success() {
let configoption_noconfirm = configfile.pacman.noconfirm.unwrap();
install(configoption_noconfirm, &arg);

@ -1,11 +1,19 @@
use git2::Repository;
use std::{env, fs, path::Path, process::Command};
// Code audit notes from axtlos:
/*
try to resolve the warning because of unused std::result::Result, no idea how to do that
*/
pub fn clone(pkg: &str, cachedir: &str) {
let error = format!("Couldn't install {}", &pkg);
let path = Path::new(&cachedir);
let pkgdir=format!("{}/{}", &cachedir, &pkg);
let pkgpath = Path::new(&pkgdir);
if !path.is_dir() {
fs::create_dir(&path);
}
env::set_current_dir(&pkgdir);
fs::create_dir(&pkg);
let results = raur::search(&pkg).expect(&error);

@ -39,7 +39,7 @@ pub fn printconfig() {
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)") != "" {
if fs::read_to_string("/etc/ame.toml").expect("unable to open config file! (/etc/ame.toml)") != "" { //maybe print out a warning when the config file is empty so that the user knows the hardcoded one is being used
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);

@ -1,4 +1,4 @@
pub fn help() {
pub fn help() { // work on a proper error message, the python ame one isnt really better in my opinion
println!("\
Usage:\n
ame -S <pkg> - install a package

@ -2,7 +2,7 @@ use runas::Command;
pub fn install(noconfirm: bool, pkg: &str) {
println!("{}",noconfirm);
let errstr = format!("Oops.. Something went wrong!");
let errstr = format!("Oops.. Something went wrong!"); // we should make one set way of how error messages are written
if noconfirm == false {
Command::new("pacman").arg("-S").arg(&pkg).status().expect(&errstr);
} else {

@ -3,7 +3,7 @@ use std::{ops::Deref, process::Command};
pub fn a_search(pkg: &str) {
let results = raur::search(&pkg);
for res in &results {
println!("aur/{} {}\n {}", res[0].name, res[0].version, res[0].description.as_ref().map_or("n/a", String::deref));
println!("aur/{} {}\n {}", res[0].name, res[0].version, res[0].description.as_ref().map_or("n/a", String::deref)); //i like your funny words, magic man (seriously, what does this do??)
}
}

@ -1,7 +1,7 @@
use runas::Command;
pub fn uninstall(noconfirm: bool, pkg: &str) {
let errstr = format!("Could not remove package {}", pkg);
let errstr = format!("Could not remove package {}", pkg); //again, we should choose one way to do error messages
if noconfirm == false {
Command::new("pacman").arg("-R").arg(&pkg).status().expect(&errstr);

@ -1,8 +1,9 @@
use runas::Command;
use walkdir::WalkDir;
use git2::Repository;
use std::{path, env};
// fix unused std::result::Result
pub fn upgrade(noconfirm: bool, cachedir: &str){
let errstr = format!("Something happened");
if noconfirm == true {
@ -20,7 +21,7 @@ pub fn upgrade(noconfirm: bool, cachedir: &str){
for file in std::fs::read_dir(&cachedir).unwrap() {
let dir = &file.unwrap().path();
env::set_current_dir(&dir);
let output = std::process::Command::new("git").arg("pull").output().unwrap();
let output = std::process::Command::new("git").arg("pull").output().unwrap(); //figure out how to pull with the git2 crate!
let update_available = String::from_utf8(output.stdout).unwrap();
if update_available != "Already up to date." {
let path_as_str = &dir.display().to_string();
@ -28,7 +29,7 @@ pub fn upgrade(noconfirm: bool, cachedir: &str){
println!("{} is up to date", pkg[pkg.len()-1]);
} else {
env::set_current_dir(&dir);
std::process::Command::new("makepkg").arg("-si");
std::process::Command::new("makepkg").arg("-si").status();
}
}
}

Loading…
Cancel
Save