diff --git a/.vscode/settings.json b/.vscode/settings.json index e807669..fc97c31 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,10 @@ { - "cSpell.words": ["sqlx"] + "cSpell.words": [ + "sqlx" + ], + "workbench.colorCustomizations": { + "activityBar.background": "#462511", + "titleBar.activeBackground": "#613417", + "titleBar.activeForeground": "#FDFAF8" + } } diff --git a/Cargo.lock b/Cargo.lock index 227806f..2a08b84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -643,6 +643,27 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "doc-comment" version = "0.3.3" @@ -1371,6 +1392,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "os_str_bytes" version = "6.5.1" @@ -1633,6 +1660,17 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall 0.2.16", + "thiserror", +] + [[package]] name = "regex" version = "1.9.4" @@ -2359,8 +2397,10 @@ dependencies = [ "clap 4.4.0", "console", "dialoguer", + "dirs", "include_dir", "indicatif", + "lazy_static", "lettre", "serde", "sqlx", diff --git a/Cargo.toml b/Cargo.toml index 69a431b..72182d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,10 @@ chrono = "0.4.26" clap = {version = "4.3.23", features = ["derive"]} console = "0.15.7" dialoguer = {version = "0.10.4", features = ["fuzzy-select", "completion"]} +dirs = "5.0.1" include_dir = "0.7.3" indicatif = "0.17.6" +lazy_static = "1.4.0" lettre = "0.10.4" # migration = {path = "migration"}# depends on your needs serde = {versio = "1.0.185", features = ["derive"]} diff --git a/src/commands/store.rs b/src/commands/store.rs index c2a1fda..1518381 100644 --- a/src/commands/store.rs +++ b/src/commands/store.rs @@ -1,7 +1,6 @@ -use crate::database::StoreModel; +use crate::{database::StoreModel, style::PrintColoredText}; use clap::{Args, Subcommand}; use serde::{Deserialize, Serialize}; -// let id = Uuid::new_v4(); #[derive(Args, Debug, Serialize)] pub struct StoreCommands { @@ -41,6 +40,8 @@ impl StoreCommands { /*store the key value pair in the database after checking that the key does not exist, if the key exist prompt use to overwrite */ async fn add(key: &str, value: &str) { StoreModel::new(key, value).save().await.unwrap(); + let message = format!("{key} successfully stored"); + PrintColoredText::success(&message); } /* accept a key and update the value of the key */ fn set(key: &str, value: &str) { diff --git a/src/database.rs b/src/database.rs index 8ca3fd5..f0311b2 100644 --- a/src/database.rs +++ b/src/database.rs @@ -5,15 +5,14 @@ use serde::{Deserialize, Serialize}; use sqlx::{migrate::MigrateDatabase, FromRow, Pool, Row, Sqlite, SqlitePool}; use uuid::Uuid; -use crate::style::PrintColoredText; -const DB_URL: &str = "sqlite://utils.db"; +use crate::{style::PrintColoredText, DB_URL}; pub struct Database; #[allow(unused)] impl Database { pub async fn init() { - if !Sqlite::database_exists(DB_URL).await.unwrap_or(false) { - match Sqlite::create_database(DB_URL).await { + if !Sqlite::database_exists(&DB_URL).await.unwrap_or(false) { + match Sqlite::create_database(&DB_URL).await { Ok(_) => PrintColoredText::success("Database initialized"), Err(_error) => PrintColoredText::error("error creating utility store"), } @@ -26,7 +25,7 @@ impl Database { let store_create_table = "CREATE TABLE IF NOT EXISTS store (id VARCHAR, key VARCHAR, value TEXT, date_added TEXT, last_updated TEXT)"; - let db = SqlitePool::connect(DB_URL).await.unwrap(); + let db = SqlitePool::connect(&DB_URL).await.unwrap(); let _ = sqlx::query(store_create_table).execute(&db).await.unwrap(); let _ = sqlx::query(email_create_table).execute(&db).await.unwrap(); @@ -47,8 +46,7 @@ impl Database { // return connection to the database; pub async fn conn() -> Pool { - - SqlitePool::connect(DB_URL).await.unwrap() + SqlitePool::connect(&DB_URL).await.unwrap() } pub async fn tables() -> HashMap { @@ -121,7 +119,7 @@ impl StoreModel { /// find all pub async fn find() -> Vec { let db = Database::conn().await; - + sqlx::query_as::<_, Self>("SELECT * FROM store") .fetch_all(&db) .await diff --git a/src/main.rs b/src/main.rs index 816c75c..9ba746d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,24 @@ +use lazy_static::lazy_static; + use include_dir::{include_dir, Dir}; pub const SOURCE_DIR: Dir = include_dir!("src/templates"); +lazy_static! { + pub static ref DB_URL: std::string::String = { + // create the database in the home directory of the system + //create "utils" directory in the home dir and / save files to $HOME/utils; + let os_default_home_dir = dirs::home_dir().unwrap(); + let db_path = format!( + "{home_dir}/{upload_dir}", + home_dir = os_default_home_dir.display(), + upload_dir = "utils" + ); + // create the path if not exist path if not exist + let _ = std::fs::create_dir_all(&db_path); + format!("sqlite://{db_path}/utils.db") + }; +} mod commands; mod database; mod parser;