mostly finished the statically defined packages, doesnt respect update option yet (sorry jnats this is like the worst code ever)

i18n
Amy 3 years ago
parent f3c8a6d463
commit db74d45d56
No known key found for this signature in database
GPG Key ID: 6672E6DD65BEA50B

@ -130,7 +130,9 @@ pub fn clone(noconfirm: bool, as_dep: bool, pkg: &str) {
match install_result { match install_result {
Ok(_) => { Ok(_) => {
uninstall_make_depend(pkg); uninstall_make_depend(pkg);
add_pkg(false, pkg); let mut vec = Vec::new();
vec.push(pkg);
add_pkg(false, &vec);
} }
Err(_) => { Err(_) => {
err_unrec(format!("Couldn't install {}", pkg)); err_unrec(format!("Couldn't install {}", pkg));
@ -145,7 +147,9 @@ pub fn clone(noconfirm: bool, as_dep: bool, pkg: &str) {
match install_result.code() { match install_result.code() {
Some(0) => { Some(0) => {
uninstall_make_depend(pkg); uninstall_make_depend(pkg);
add_pkg(false, pkg); let mut vec = Vec::new();
vec.push(pkg);
add_pkg(false, &vec);
} }
Some(_) => { Some(_) => {
err_unrec(format!("Couldn't install {}", pkg)); err_unrec(format!("Couldn't install {}", pkg));
@ -166,7 +170,9 @@ pub fn clone(noconfirm: bool, as_dep: bool, pkg: &str) {
match install_result { match install_result {
Ok(_) => { Ok(_) => {
uninstall_make_depend(pkg); uninstall_make_depend(pkg);
add_pkg(false, pkg); let mut vec = Vec::new();
vec.push(pkg);
add_pkg(false, &vec);
} }
Err(_) => { Err(_) => {
err_unrec(format!("Couldn't install {}", pkg)); err_unrec(format!("Couldn't install {}", pkg));

@ -74,7 +74,7 @@ pub fn rem_pkg(pkgs: &[String]) {
for i in pkgs { for i in pkgs {
let result = connection.execute(format!( let result = connection.execute(format!(
" "
DELETE FROM pkgs WHERE name = {}; DELETE FROM pkgs WHERE name = \"{}\";
", ",
i i
)); ));
@ -85,24 +85,39 @@ pub fn rem_pkg(pkgs: &[String]) {
} }
} }
pub fn add_pkg(_from_repo: bool, pkg: &str) { pub fn add_pkg(from_repo: bool, pkgs: &Vec<&str>) {
let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap()); for pkg in pkgs {
let connection = sqlite::open(file).unwrap(); let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap());
let results = raur::search(&pkg); let connection = sqlite::open(file).unwrap();
let mut package_name = String::new(); let results = raur::search(&pkg);
let mut package_version = String::new(); let mut package_name = String::new();
for res in &results { let mut package_version = String::new();
package_name = res[0].name.to_string(); for res in &results {
package_version = res[0].version.to_string(); package_name = res[0].name.to_string();
} package_version = res[0].version.to_string();
let result = connection.execute(format!( }
" if from_repo == false {
INSERT INTO pkgs (name, version) VALUES (\"{}\", \"{}\"); let result = connection.execute(format!(
", "
package_name, package_version INSERT INTO pkgs (name, version) VALUES (\"{}\", \"{}\");
)); ",
match result { package_name, package_version
Ok(_) => inf(format!("Added {} to database", package_name)), ));
Err(_) => err_unrec(format!("Couldn't add {} to database", package_name)), match result {
Ok(_) => inf(format!("Added {} to database", package_name)),
Err(_) => err_unrec(format!("Couldn't add {} to database", package_name)),
}
} else {
let result = connection.execute(format!(
"
INSERT INTO pkgs (name, version) VALUES (\"{}\", \"{}\");
",
pkg, "from_repo".to_string()
));
match result {
Ok(_) => inf(format!("Added {} to database", package_name)),
Err(_) => err_unrec(format!("Couldn't add {} to database", package_name)),
}
}
} }
} }

@ -1,4 +1,5 @@
use crate::mods::strs::{err_unrec, succ}; use crate::mods::strs::{err_unrec, succ};
use crate::mods::database::add_pkg;
use runas::Command; use runas::Command;
pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) { pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) {
@ -14,7 +15,10 @@ pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) {
.status() .status()
.expect("Couldn't call pacman"); .expect("Couldn't call pacman");
match result.code() { match result.code() {
Some(0) => succ(format!("Succesfully installed packages: {}", pkg)), Some(0) => {
succ(format!("Succesfully installed packages: {}", pkg));
add_pkg(true, &pkgs);
},
Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)), Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)),
None => err_unrec(format!("Couldn't install packages: {}", pkg)), None => err_unrec(format!("Couldn't install packages: {}", pkg)),
}; };
@ -26,7 +30,10 @@ pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) {
.status() .status()
.expect("Couldn't call pacman"); .expect("Couldn't call pacman");
match result.code() { match result.code() {
Some(0) => succ(format!("Succesfully installed packages: {}", pkg)), Some(0) => {
succ(format!("Succesfully installed packages: {}", pkg));
add_pkg(true, &pkgs);
},
Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)), Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)),
None => err_unrec(format!("Couldn't install packages: {}", pkg)), None => err_unrec(format!("Couldn't install packages: {}", pkg)),
}; };
@ -41,7 +48,10 @@ pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) {
.status() .status()
.expect("Couldn't call pacman"); .expect("Couldn't call pacman");
match result.code() { match result.code() {
Some(0) => succ(format!("Succesfully installed packages: {}", pkg)), Some(0) => {
succ(format!("Succesfully installed packages: {}", pkg));
add_pkg(true, &pkgs);
},
Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)), Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)),
None => err_unrec(format!("Couldn't install packages: {}", pkg)), None => err_unrec(format!("Couldn't install packages: {}", pkg)),
}; };

@ -8,10 +8,10 @@ pub fn stat_create_database() {
let _cdar = fs::create_dir_all(format!("/{}/.local/ame/",homepath)); let _cdar = fs::create_dir_all(format!("/{}/.local/ame/",homepath));
match _cdar { match _cdar {
Ok(_) => { Ok(_) => {
inf(format!("Created path for database (previously missing)")) inf("Created path for database (previously missing)".to_string());
} }
Err(_) => { Err(_) => {
err_unrec(format!("Couldn't create path for database (~/.local/rhare/ame)")) err_unrec("Couldn't create path for database (~/.local/share/ame)".to_string())
} }
} }
} }
@ -24,6 +24,29 @@ pub fn stat_create_database() {
.unwrap(); .unwrap();
} }
pub fn stat_dump_dat() -> Vec<String> {
let homepath = env::var("HOME").unwrap();
let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap());
let connection = sqlite::open(file).unwrap();
let mut dat_pkgs = Vec::new();
let result = connection
.iterate("SELECT name FROM static_pkgs", |pairs| {
for &(column, value) in pairs.iter() {
dat_pkgs.push(value.unwrap().to_string());
}
true
});
match result {
Ok(_) => {
//nf("Dumped static packages".to_string());
}
Err(_) => {
err_unrec("Couldn't dump packages from database".to_string())
}
}
return dat_pkgs;
}
pub fn stat_get_value(pkg: &str, sear_value: &str) -> bool { 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 file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap());
let connection = sqlite::open(file).unwrap(); let connection = sqlite::open(file).unwrap();
@ -42,11 +65,7 @@ pub fn stat_get_value(pkg: &str, sear_value: &str) -> bool {
Ok(_) => {}, Ok(_) => {},
Err(_) => err_unrec("Couldn't get value from database".to_string()), Err(_) => err_unrec("Couldn't get value from database".to_string()),
} }
if return_val == true { return return_val == true;
return true;
} else {
return false;
}
}, },
"update" => { "update" => {
let result = connection.iterate(format!("SELECT pin FROM static_pkgs WHERE name = \"{}\";",&pkg), |pairs| { let result = connection.iterate(format!("SELECT pin FROM static_pkgs WHERE name = \"{}\";",&pkg), |pairs| {
@ -60,11 +79,7 @@ pub fn stat_get_value(pkg: &str, sear_value: &str) -> bool {
Ok(_) => {}, Ok(_) => {},
Err(_) => err_unrec("Couldn't get value from database".to_string()), Err(_) => err_unrec("Couldn't get value from database".to_string()),
} }
if return_val == true { return return_val == true
return true;
} else {
return false;
}
}, },
_ => { _ => {
return_val = false return_val = false
@ -76,7 +91,7 @@ pub fn stat_get_value(pkg: &str, sear_value: &str) -> bool {
pub fn stat_rem_pkg(static_pkgs: &Vec<String>) { pub fn stat_rem_pkg(static_pkgs: &Vec<String>) {
let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap()); let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap());
let connection = sqlite::open(file).unwrap(); let connection = sqlite::open(file).unwrap();
print!("{:?}",static_pkgs);
for i in static_pkgs { for i in static_pkgs {
let result = connection.execute( let result = connection.execute(
format!(" format!("
@ -88,7 +103,7 @@ pub fn stat_rem_pkg(static_pkgs: &Vec<String>) {
inf(format!("Removed {} from database", i)) inf(format!("Removed {} from database", i))
} }
Err(_) => { Err(_) => {
err_unrec(format!("Couldn't remove {} from database", i)) err_unrec(format!("Couldn't remove {} from database (static packages table)", i))
} }
} }
} }

@ -2,10 +2,14 @@ use std::{fs, env};
use crate::inf; use crate::inf;
use toml::{Value, toml}; use toml::{Value, toml};
use crate::{ use crate::{
err_rec,
stat_add_pkg, stat_add_pkg,
stat_create_database, stat_create_database,
stat_get_value, stat_get_value,
stat_rem_pkg, stat_rem_pkg,
inssort,
stat_dump_dat,
uninstall,
}; };
pub fn rebuild(noconfirm: bool) { pub fn rebuild(noconfirm: bool) {
@ -13,7 +17,7 @@ pub fn rebuild(noconfirm: bool) {
let file = format!("{}/.config/ame/pkgs.toml", env::var("HOME").unwrap()); let file = format!("{}/.config/ame/pkgs.toml", env::var("HOME").unwrap());
let mut database = String::new(); let mut database = String::new();
database = fs::read_to_string(&file).expect("Can't Open Database"); database = fs::read_to_string(&file).expect("Can't Open Database");
inf(format!("installing crystal config")); inf("installing crystal config".to_string());
let db_parsed = database.parse::<toml::Value>().expect("Invalid Database"); let db_parsed = database.parse::<toml::Value>().expect("Invalid Database");
let mut pkgs = Vec::new(); let mut pkgs = Vec::new();
@ -27,11 +31,51 @@ pub fn rebuild(noconfirm: bool) {
pkgs.push(tempvec); pkgs.push(tempvec);
} }
} }
let mut pkgs_to_add: Vec<String> = Vec::new(); let mut pkgs_to_add: Vec<Vec<String>> = Vec::new();
let mut pkgs_to_install: Vec<String> = Vec::new();
for i in pkgs { for i in pkgs {
if !stat_get_value(&i[0], "name") { if !stat_get_value(&i[0], "name") {
pkgs_to_add.push(i[0].to_string()); let mut tempvec = Vec::new();
tempvec.push(i[0].to_string());
tempvec.push(i[1].to_string());
pkgs_to_add.push(tempvec);
pkgs_to_install.push(i[0].to_string());
}
}
let mut config_no_change = 0;
if pkgs_to_install.len() > 0 {
inf(format!("Installing {}", pkgs_to_install.join(", ")));
inssort(noconfirm, false, pkgs_to_install);
for i in pkgs_to_add {
stat_add_pkg(&i[1], &i[0]);
} }
config_no_change += 1;
}
let dat_pkgs = stat_dump_dat();
let mut pkgs = Vec::new();
for entry in db_parsed.as_table() {
for (key, value) in &*entry {
pkgs.push(key);
}
}
let mut pkgs_to_remove: Vec<String> = Vec::new();
for i in dat_pkgs {
if !pkgs.contains(&&i) {
pkgs_to_remove.push(i.to_string());
}
config_no_change += 1;
}
if pkgs_to_remove.len() > 0 {
inf(format!("Removing {}", pkgs_to_remove.join(", ")));
stat_rem_pkg(&pkgs_to_remove);
uninstall(noconfirm, pkgs_to_remove);
}
if config_no_change != 0 {
inf("Rebuild Complete".to_string());
} else {
err_rec("Configuration not changed!".to_string());
} }
inf(format!("Installing {}", pkgs_to_add.join(", ")));
} }
Loading…
Cancel
Save