diff --git a/Cargo.toml b/Cargo.toml index a611c18..70e2c70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ame" version = "0.0.0" -authors = [ "jnats ", "axtlos " ] +authors = [ "jnats ", "axtlos " ] edition = "2018" description = "a fast and efficient aur helper." @@ -19,4 +19,5 @@ toml = "*" bytes = "*" nix = "*" clap = "*" -sqlite = "*" \ No newline at end of file +sqlite = "*" +file_diff = "*" diff --git a/src/main.rs b/src/main.rs index c645a8a..6050c3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,8 @@ use mods::{ upgrade::upgrade, ver::ver, xargs::*, + statpkgs::*, + stat_database::*, }; use std::{env, process::exit}; @@ -53,8 +55,7 @@ fn main() { inssort_from_file(noconfirm, false, &pkgs[0]); // install from file } "-B" | "-Bn" | "build" => { - //rebuild(noconfirm); // install as a dependency - add_pkg(noconfirm, &pkgs[0]); + rebuild(noconfirm); // install as a dependency } "-R" | "-Rn" | "rm" => { uninstall(noconfirm, pkgs); // uninstall diff --git a/src/mods.rs b/src/mods.rs index d8965a1..0eb14d1 100644 --- a/src/mods.rs +++ b/src/mods.rs @@ -7,9 +7,10 @@ pub mod install; pub mod purge; pub mod search; pub mod statpkgs; +pub mod stat_database; pub mod strs; pub mod uninstall; pub mod update; pub mod upgrade; pub mod ver; -pub mod xargs; +pub mod xargs; \ No newline at end of file diff --git a/src/mods/stat_database.rs b/src/mods/stat_database.rs new file mode 100644 index 0000000..5272dd5 --- /dev/null +++ b/src/mods/stat_database.rs @@ -0,0 +1,114 @@ +use crate::{err_unrec, inf}; +use std::{fs, env}; + +pub fn stat_create_database() { + let homepath = env::var("HOME").unwrap(); + let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap()); + if !std::path::Path::new(&format!("{}/.local/share/ame/", env::var("HOME").unwrap())).is_dir() { + let _cdar = fs::create_dir_all(format!("/{}/.local/ame/",homepath)); + match _cdar { + Ok(_) => { + inf(format!("Created path for database (previously missing)")) + } + Err(_) => { + err_unrec(format!("Couldn't create path for database (~/.local/rhare/ame)")) + } + } + } + let connection = sqlite::open(file).unwrap(); + connection.execute( + " + CREATE TABLE static_pkgs (name TEXT, pin INTEGER); + ", + ) + .unwrap(); +} + +pub fn stat_get_value(pkg: &str, sear_value: &str) -> bool { + let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap()); + let connection = sqlite::open(file).unwrap(); + let mut return_val = false; + //println!("{}", pkg); + match sear_value { + "name" => { + let result = connection.iterate(format!("SELECT name FROM static_pkgs WHERE name = \"{}\";",&pkg), |pairs| { + for &(column, value) in pairs.iter() { + return_val = true; + } + return_val + } + ); + match result { + Ok(_) => {}, + Err(_) => err_unrec("Couldn't get value from database".to_string()), + } + if return_val == true { + return true; + } else { + return false; + } + }, + "update" => { + let result = connection.iterate(format!("SELECT pin FROM static_pkgs WHERE name = \"{}\";",&pkg), |pairs| { + for &(column, value) in pairs.iter() { + return_val = true; + } + return_val + } + ); + match result { + Ok(_) => {}, + Err(_) => err_unrec("Couldn't get value from database".to_string()), + } + if return_val == true { + return true; + } else { + return false; + } + }, + _ => { + return_val = false + } + } + return return_val; +} + +pub fn stat_rem_pkg(static_pkgs: &Vec) { + let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap()); + let connection = sqlite::open(file).unwrap(); + + for i in static_pkgs { + let result = connection.execute( + format!(" + DELETE FROM static_pkgs WHERE name = \"{}\"; + ", i), + ); + match result{ + Ok(_) => { + inf(format!("Removed {} from database", i)) + } + Err(_) => { + err_unrec(format!("Couldn't remove {} from database", i)) + } + } + } +} + +pub fn stat_add_pkg(update: &str, pkg: &str) { + let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap()); + let connection = sqlite::open(file).unwrap(); + let pin = if update == "true" { 1 } else { 0 }; + let result = connection.execute( + format!(" + INSERT INTO static_pkgs (name, pin) VALUES (\"{}\", {}); + ", pkg, pin), + ); + match result{ + Ok(_) => { + inf(format!("Added {} to database", pkg)) + } + Err(_) => { + err_unrec(format!("Couldn't add {} to database", pkg)) + } + } +} \ No newline at end of file diff --git a/src/mods/statpkgs.rs b/src/mods/statpkgs.rs index 8f33232..ae5b9dd 100644 --- a/src/mods/statpkgs.rs +++ b/src/mods/statpkgs.rs @@ -1,3 +1,37 @@ +use std::{fs, env}; +use crate::inf; +use toml::{Value, toml}; +use crate::{ + stat_add_pkg, + stat_create_database, + stat_get_value, + stat_rem_pkg, +}; + pub fn rebuild(noconfirm: bool) { - print!("installing crystal config") -} + let homepath = env::var("HOME").unwrap(); + let file = format!("{}/.config/ame/pkgs.toml", env::var("HOME").unwrap()); + let mut database = String::new(); + database = fs::read_to_string(&file).expect("Can't Open Database"); + inf(format!("installing crystal config")); + + let db_parsed = database.parse::().expect("Invalid Database"); + let mut pkgs = Vec::new(); + for entry in db_parsed.as_table() { + for (key, value) in &*entry { + let mut tempvec = Vec::new(); + println!("{}", key); + println!("{}", format!("{}",value).replace("update = ", "")); + tempvec.push(key.to_string()); + tempvec.push(format!("{}",value).replace("update = ", "")); + pkgs.push(tempvec); + } + } + let mut pkgs_to_add: Vec = Vec::new(); + for i in pkgs { + if !stat_get_value(&i[0], "name") { + pkgs_to_add.push(i[0].to_string()); + } + } + inf(format!("Installing {}", pkgs_to_add.join(", "))); +} \ No newline at end of file