From df3b83154945bd6e260f62bbb8e5e907e32bde18 Mon Sep 17 00:00:00 2001 From: ShuhanX Date: Sat, 19 Apr 2025 10:53:29 +0800 Subject: [PATCH 1/2] feat(json): Allow users to create data files in specified folders allows users to create data files in a specified folder by using `basilk {filename}` and it will create a file: {filename}.{version}.{json} --- Cargo.lock | 9 ++++++++- Cargo.toml | 1 + src/cli.rs | 5 +++++ src/globals.rs | 7 +++++++ src/json.rs | 13 ++++++++++++- src/main.rs | 1 + 6 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/globals.rs diff --git a/Cargo.lock b/Cargo.lock index 2a1e253..ce28186 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "ahash" @@ -31,6 +31,7 @@ name = "basilk" version = "0.2.1" dependencies = [ "dirs", + "lazy_static", "ratatui", "serde", "serde_json", @@ -188,6 +189,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.155" diff --git a/Cargo.toml b/Cargo.toml index 0c06b5f..07dc12b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,4 @@ serde = { version = "1.0.204", features = ["derive"] } serde_json = "1.0.122" toml = "0.8.19" tui-input = "0.9.0" +lazy_static = "1.5.0" diff --git a/src/cli.rs b/src/cli.rs index f41bb91..377d0ed 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,3 +1,4 @@ +use crate::globals::DATA_FILE; use std::{env, process::exit}; pub struct Cli; @@ -12,6 +13,10 @@ impl Cli { if arg == "--version" { print!(env!("CARGO_PKG_VERSION")); exit(0) + } else { + // get data file name + let mut data_file_name = DATA_FILE.lock().unwrap(); + *data_file_name = Some(arg); } } None => (), diff --git a/src/globals.rs b/src/globals.rs new file mode 100644 index 0000000..e927434 --- /dev/null +++ b/src/globals.rs @@ -0,0 +1,7 @@ +// global variable +use lazy_static::lazy_static; +use std::sync::Mutex; + +lazy_static! { + pub static ref DATA_FILE: Mutex> = Mutex::new(None); // data file name(If it is none, the default file will be used) +} diff --git a/src/json.rs b/src/json.rs index ae4a369..aaab79d 100644 --- a/src/json.rs +++ b/src/json.rs @@ -9,6 +9,7 @@ use std::{ use serde_json::{from_str, to_string, Value}; use crate::{ + globals::DATA_FILE, migration::{Migration, JSON_VERSIONS}, project::Project, }; @@ -20,6 +21,12 @@ static VERSION: Mutex = Mutex::new(String::new()); impl Json { pub fn get_dir_path() -> PathBuf { + // if it is a file in a project, push the correct directory + if DATA_FILE.lock().unwrap().is_some() { + let mut path = PathBuf::new(); + path.push("./"); + return path; + } let mut path = dirs::config_dir().unwrap(); path.push(DIR_CONFIG_NAME); @@ -29,7 +36,11 @@ impl Json { fn get_json_path(version: String) -> PathBuf { let mut path = PathBuf::new(); path.push(Json::get_dir_path().as_path()); - path.push(format!("{version}.json")); + if let Some(data_file) = DATA_FILE.lock().unwrap().clone() { + path.push(format!("{data_file}.{version}.json")); + } else { + path.push(format!("{version}.json")); + } return path; } diff --git a/src/main.rs b/src/main.rs index 26f79ff..fd8a3af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ use tui_input::{backend::crossterm::EventHandler, Input}; mod cli; mod config; +mod globals; mod json; mod migration; mod project; From d67caaadb08d48bd534ccc6a4e15ee81892b96be Mon Sep 17 00:00:00 2001 From: ShuhanX Date: Sat, 19 Apr 2025 11:31:51 +0800 Subject: [PATCH 2/2] doc(readme): Adding guide to use specific data file --- README.md | 7 +++++++ src/json.rs | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cf75250..96d7438 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,13 @@ basilk ``` All available commands are displayed inside +### Use specific data file +Run +```sh +basilk [file-name] +``` +to use a specific data file in current directory. if the file does not exist, create `[file-name].[json-version].json`. + ## Contributing > [!NOTE] > This project is now in beta version and is expected to have bugs diff --git a/src/json.rs b/src/json.rs index aaab79d..f65bb54 100644 --- a/src/json.rs +++ b/src/json.rs @@ -21,7 +21,7 @@ static VERSION: Mutex = Mutex::new(String::new()); impl Json { pub fn get_dir_path() -> PathBuf { - // if it is a file in a project, push the correct directory + // if it is a file in a project, push the current directory if DATA_FILE.lock().unwrap().is_some() { let mut path = PathBuf::new(); path.push("./");