rework database to use sql

i18n
Amy 3 years ago
parent 840de5ce76
commit 01beec5d0f
No known key found for this signature in database
GPG Key ID: 6672E6DD65BEA50B

2
.gitignore vendored

@ -2,3 +2,5 @@ target/
Cargo.lock
ame
.vscode
aur_pkgs.db
test.sql

@ -18,4 +18,5 @@ toml_edit = "*"
toml = "*"
bytes = "*"
nix = "*"
clap = "*"
clap = "*"
sqlite = "*"

@ -22,7 +22,12 @@ use mods::{
update::update,
upgrade::upgrade,
ver::ver,
xargs::*
xargs::*,
statpkgs::rebuild,
database::{
add_pkg,
create_database,
},
};
use std::{
env,
@ -43,6 +48,11 @@ fn main() {
exit(1);
}
let file = format!("{}/crystal/ame/aur_pkgs.db", env::var("HOME").unwrap());
if !std::path::Path::new(&file).exists() {
create_database();
}
let oper = &args[0];
let noconfirm: bool = noconf(&args);
@ -54,6 +64,10 @@ fn main() {
"-Sl" | "-Sln" | "insl" => {
inssort_from_file(noconfirm, false, &pkgs[0]); // install from file
}
"-B" | "-Bn" | "build" => {
//rebuild(noconfirm); // install as a dependency
add_pkg(noconfirm, &pkgs[0]);
}
"-R" | "-Rn" | "rm" => {
uninstall(noconfirm, pkgs); // uninstall
}

@ -11,4 +11,5 @@ pub mod update;
pub mod upgrade;
pub mod ver;
pub mod xargs;
pub mod purge;
pub mod purge;
pub mod statpkgs;

@ -134,11 +134,7 @@ pub fn clone(noconfirm: bool, as_dep: bool, pkg: &str) { // clone a package from
match install_result {
Ok(_) => {
uninstall_make_depend(pkg);
let add_pkg_res = add_pkg(false, pkg);
match add_pkg_res {
Ok(_) => inf(format!("Added package {} to database", pkg)),
Err(_) => err_unrec(format!("Couldn't add package {} to database", pkg)),
}
add_pkg(false, pkg);
}
Err(_) => {
err_unrec(format!("Couldn't install {}", pkg));
@ -154,11 +150,7 @@ pub fn clone(noconfirm: bool, as_dep: bool, pkg: &str) { // clone a package from
match install_result.code() {
Some(0) => {
uninstall_make_depend(pkg);
let add_pkg_res = add_pkg(false, pkg);
match add_pkg_res {
Ok(_) => inf(format!("Added package {} to database", pkg)),
Err(_) => err_unrec(format!("Couldn't add package {} to database", pkg)),
}
add_pkg(false, pkg);
}
Some(_) => {
err_unrec(format!("Couldn't install {}", pkg));
@ -179,11 +171,7 @@ pub fn clone(noconfirm: bool, as_dep: bool, pkg: &str) { // clone a package from
match install_result {
Ok(_) => {
uninstall_make_depend(pkg);
let add_pkg_res = add_pkg(false, pkg);
match add_pkg_res {
Ok(_) => inf(format!("Added package {} to database", pkg)),
Err(_) => err_unrec(format!("Couldn't add package {} to database", pkg)),
}
add_pkg(false, pkg);
}
Err(_) => {
err_unrec(format!("Couldn't install {}", pkg));

@ -1,18 +1,11 @@
use crate::{err_unrec, inf};
use std::{fs, io::{Error, Write}, env, path};
use toml_edit::{value, Document};
use crate::mods::strs::{err_rec};
use std::{fs, env};
pub fn get_value(pkg: &str, sear_value: &str) -> String { // Get specific value from database
pub fn create_database() {
let homepath = env::var("HOME").unwrap();
let file = format!("{}/.local/ame/aurPkgs.db", env::var("HOME").unwrap());
let mut database = String::new();
match path::Path::new(&file).exists() {
true => {
database = fs::read_to_string(&file).expect("Can't Open Database");
}
false => {
let _cdar = fs::create_dir_all(format!("/{}/.local/ame/",homepath));
let file = format!("{}/crystal/ame/aur_pkgs.db", env::var("HOME").unwrap());
if !std::path::Path::new(&format!("{}/.local/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)"))
@ -21,152 +14,100 @@ pub fn get_value(pkg: &str, sear_value: &str) -> String { // Get specific value
err_unrec(format!("Couldn't create path for database (~/.local/ame)"))
}
}
err_rec(String::from("Database wasn't found, creating new one"));
let _dbfile = fs::File::create(&file);
match _dbfile {
Ok(_) => {
inf(format!("Created empty database (previously missing)"))
}
Err(_) => {
err_unrec(format!("Couldn't create database"))
}
}
}
}
let db_parsed = database.parse::<toml::Value>().expect("Invalid Database");
let connection = sqlite::open(file).unwrap();
connection.execute(
"
CREATE TABLE pkgs (name TEXT, version TEXT);
",
)
.unwrap();
}
pub fn get_value(pkg: &str, sear_value: &str) -> String {
let file = format!("{}/crystal/ame/aur_pkgs.db", env::var("HOME").unwrap());
let connection = sqlite::open(file).unwrap();
let mut return_val = String::new();
for entry in db_parsed.as_table() {
for (key, value) in &*entry {
if key.contains(pkg) {
let results = raur::search(format!("{}",key));
for res in results {
match sear_value {
"name" => {
return_val = value
.to_string()
.replace("name", "")
.replace("version", "")
.replace(" = ", "")
.replace("\"", "")
.replace(format!("{}", &res[0].version.to_string()).as_str(), "");
}
"version" => {
return_val = value
.to_string()
.replace("name", "")
.replace("version", "")
.replace(" = ", "")
.replace("\"", "")
.replace(format!("{}", &res[0].name.to_string()).as_str(), "");
}
_ => {
err_unrec(format!(""));
}
}
match sear_value {
"name" => {
let result = connection.iterate(format!("SELECT name FROM pkgs WHERE name = {};",&pkg), |pairs| {
for &(column, value) in pairs.iter() {
return_val = value.unwrap().to_string();
}
true
}
}
}
return return_val;
}
pub fn rem_pkg(pkgs: &Vec<String>) { // Remove packages from database
let homepath = env::var("HOME").unwrap();
let file = format!("{}/.local/ame/aurPkgs.db", env::var("HOME").unwrap());
let mut database = String::new();
match path::Path::new(&file).exists() {
true => {
database = fs::read_to_string(&file).expect("Can't Open Database");
}
false => {
let _cdar = fs::create_dir_all(format!("/{}/.local/ame/",homepath));
match _cdar {
Ok(_) => {
inf(format!("Created path for database (previously missing)"))
}
);
match result {
Ok(_) => {}
Err(_) => {
err_unrec(format!("Couldn't create path for database (~/.local/ame)"))
err_unrec(format!("Couldn't get value from database"))
}
}
err_rec(String::from("Database wasn't found, creating new one"));
let _dbfile = fs::File::create(&file);
match _dbfile {
Ok(_) => {
inf(format!("Created empty database (previously missing)"))
},
"version" => {
let result = connection.iterate(format!("SELECT version FROM pkgs WHERE name = {};",&pkg), |pairs| {
for &(column, value) in pairs.iter() {
return_val = value.unwrap().to_string();
}
true
}
);
match result {
Ok(_) => {}
Err(_) => {
err_unrec(format!("Couldn't create database"))
err_unrec(format!("Couldn't get value from database"))
}
}
},
_ => {
return_val = "error".to_string();
}
}
return return_val;
}
pub fn rem_pkg(pkgs: &Vec<String>) {
let file = format!("{}/crystal/ame/aur_pkgs.db", env::var("HOME").unwrap());
let connection = sqlite::open(file).unwrap();
let mut update_database = database;
for i in pkgs {
if update_database.contains(i) {
let results = raur::search(&i);
for res in &results {
let database_entry = format!(
"{} = {{ name = \"{}\", version = \"{}\"}}\n",
&res[0].name, &res[0].name, &res[0].version
);
update_database = format!("{}", update_database.replace(&database_entry, ""));
let result = connection.execute(
format!("
DELETE FROM pkgs WHERE name = {};
", i),
);
match result{
Ok(_) => {
inf(format!("Removed {} from database", i))
}
Err(_) => {
err_unrec(format!("Couldn't remove {} from database", i))
}
}
}
let file_as_path = fs::File::create(path::Path::new(&file)).unwrap();
let db_update_res = write!(&file_as_path, "{}", update_database);
match db_update_res {
Ok(_) => inf(format!("Database update successful")),
Err(_) => err_unrec(format!("Couldn't update database")),
}
}
pub fn add_pkg(from_repo: bool, pkg: &str) -> Result<(), Error> { // Add package to database
let homepath = env::var("HOME").unwrap();
let file = format!("{}/.local/ame/aurPkgs.db", env::var("HOME").unwrap());
let mut database = String::new();
match path::Path::new(&file).exists() {
true => {
database = fs::read_to_string(&file).expect("Can't Open Database");
}
false => {
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/ame)"))
}
}
err_rec(String::from("Database wasn't found, creating new one"));
let _dbfile = fs::File::create(&file);
match _dbfile {
Ok(_) => {
inf(format!("Created empty database (previously missing)"))
}
Err(_) => {
err_unrec(format!("Couldn't create database"))
}
}
}
pub fn add_pkg(_from_repo: bool, pkg: &str) {
let file = format!("{}/crystal/ame/aur_pkgs.db", env::var("HOME").unwrap());
let connection = sqlite::open(file).unwrap();
let results = raur::search(&pkg);
let mut package_name = String::new();
let mut package_version = String::new();
for res in &results {
package_name = res[0].name.to_string();
package_version = res[0].version.to_string();
}
let mut db_parsed = database.parse::<Document>().expect("Invalid Database");
let mut file_as_path = fs::File::create(path::Path::new(&file))?;
if from_repo == false {
let results = raur::search(&pkg);
for res in &results {
db_parsed[&res[0].name]["name"] = value(&res[0].name);
db_parsed[&res[0].name]["version"] = value(&res[0].version);
let result = connection.execute(
format!("
INSERT INTO pkgs (name, version) VALUES (\"{}\", \"{}\");
", package_name, package_version),
);
match result{
Ok(_) => {
inf(format!("Added {} to database", package_name))
}
Err(_) => {
err_unrec(format!("Couldn't add {} to database", package_name))
}
} else {
db_parsed[&pkg]["name"] = value(pkg);
db_parsed[&pkg]["version"] = value(pkg);
}
file_as_path
.write_all(format!("{}", db_parsed).as_bytes())
.unwrap();
Ok(())
}

@ -0,0 +1,3 @@
pub fn rebuild(noconfirm: bool) {
print!("installing crystal config")
}
Loading…
Cancel
Save