From 881641c05a36d4e3f8ca0fe4cadfb397cb2b9768 Mon Sep 17 00:00:00 2001 From: Mykhailo Slyvka Date: Wed, 29 Jan 2025 16:36:28 +0200 Subject: [PATCH 1/2] refactor datadir path and history save --- Cargo.toml | 3 ++- bin/core-cli/src/main.rs | 8 ++++---- crates/cli/Cargo.toml | 3 ++- crates/cli/src/cli.rs | 21 +++++++++++++++++++-- crates/cli/tests/cli_tests.rs | 12 +++++++++--- crates/console/src/console.rs | 15 +++++++++------ crates/modules/src/xcbkey.rs | 8 ++++---- crates/modules/tests/xcbkey_tests.rs | 2 +- 8 files changed, 50 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c9e2d09..8a7ec91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ keywords = ["core blockchain", "xcb", "cli"] license = "MIT OR Apache-2.0" homepage = "https://github.com/core-coin/core-cli" repository = "https://github.com/core-coin/core-cli" -version = "0.0.9" +version = "0.0.10" [workspace.dependencies] # Workspace members @@ -52,6 +52,7 @@ rand = "0.8" rand_core = "0.6" hex = "0.4.3" chrono = "0.4" +dirs = "4.0" # Core libraries base-primitives = { git = "https://github.com/core-coin/base-rs.git",default-features = false} diff --git a/bin/core-cli/src/main.rs b/bin/core-cli/src/main.rs index ec11507..fd4c8d0 100644 --- a/bin/core-cli/src/main.rs +++ b/bin/core-cli/src/main.rs @@ -13,18 +13,18 @@ async fn main() -> Result<(), CliError> { let args = Cli::from_args(); let client: Arc> = match args.client.as_str() { - "go-core" => Arc::new(Mutex::new(GoCoreClient::new(args.backend))), + "go-core" => Arc::new(Mutex::new(GoCoreClient::new(args.backend.to_string()))), _ => return Err(CliError::UnknownClient(args.client)), }; // create datadir if not exists - if !std::path::Path::new(&args.datadir).exists() { - std::fs::create_dir_all(&args.datadir)?; + if !std::path::Path::new(&args.get_datadir()).exists() { + std::fs::create_dir_all(args.get_datadir())?; } let stdout = std::io::stdout(); let editor = DefaultEditor::new().unwrap(); - let mut console = Console::new(client, args.datadir, stdout, editor).await; + let mut console = Console::new(client, args.get_datadir(), stdout, editor).await; console.run().await; Ok(()) diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index b68f2e5..158ee69 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -16,4 +16,5 @@ structopt.workspace = true assert_cmd = {version = "2.0.16"} predicates = {version = "3.1.2"} types.workspace = true -utils.workspace = true \ No newline at end of file +utils.workspace = true +dirs.workspace = true \ No newline at end of file diff --git a/crates/cli/src/cli.rs b/crates/cli/src/cli.rs index c44dda5..c907fc4 100644 --- a/crates/cli/src/cli.rs +++ b/crates/cli/src/cli.rs @@ -1,3 +1,6 @@ +use std::path::PathBuf; + +use dirs::home_dir; use structopt::StructOpt; use types::DEFAULT_BACKEND; @@ -14,6 +17,20 @@ pub struct Cli { )] pub backend: String, - #[structopt(long, short, default_value = "~/.core-cli/data")] - pub datadir: String, + #[structopt(long, short)] + pub datadir: Option, +} + +impl Cli { + pub fn get_datadir(&self) -> PathBuf { + match &self.datadir { + Some(dir) => PathBuf::from(dir), + None => { + let mut default_path = home_dir().expect("Could not determine home directory"); + default_path.push(".core-cli/data"); + println!("Using default datadir: {:?}", default_path); + default_path + } + } + } } diff --git a/crates/cli/tests/cli_tests.rs b/crates/cli/tests/cli_tests.rs index efec0d7..29f35ef 100644 --- a/crates/cli/tests/cli_tests.rs +++ b/crates/cli/tests/cli_tests.rs @@ -1,6 +1,9 @@ #[cfg(test)] mod tests { + use std::path::Path; + use cli::Cli; + use dirs::home_dir; use structopt::StructOpt; use types::DEFAULT_BACKEND; @@ -11,7 +14,10 @@ mod tests { assert_eq!(cli.client, "go-core"); assert_eq!(cli.backend, DEFAULT_BACKEND); - assert_eq!(cli.datadir, "./data"); + assert_eq!( + cli.get_datadir(), + home_dir().unwrap().join(".core-cli/data") + ); } #[test] @@ -29,7 +35,7 @@ mod tests { assert_eq!(cli.client, "custom-client"); assert_eq!(cli.backend, "some-backend"); - assert_eq!(cli.datadir, "some-datadir"); + assert_eq!(cli.get_datadir(), Path::new("some-datadir")); } #[test] @@ -47,6 +53,6 @@ mod tests { assert_eq!(cli.client, "custom-client"); assert_eq!(cli.backend, "some-backend"); - assert_eq!(cli.datadir, "some-datadir"); + assert_eq!(cli.get_datadir(), Path::new("some-datadir")); } } diff --git a/crates/console/src/console.rs b/crates/console/src/console.rs index c24ab4e..d86ce4c 100644 --- a/crates/console/src/console.rs +++ b/crates/console/src/console.rs @@ -7,6 +7,7 @@ use rustyline::history::FileHistory; use rustyline::Editor; use std::collections::HashMap; use std::io::Write; +use std::path::PathBuf; use std::str::FromStr; use std::sync::Arc; use tokio::sync::Mutex; @@ -20,7 +21,7 @@ pub struct Console { modules: HashMap>, base_functions: BaseFunctions, client: Arc>, - datadir: String, + datadir: PathBuf, writer: W, editor: Editor<(), FileHistory>, } @@ -28,7 +29,7 @@ pub struct Console { impl Console { pub async fn new( client: Arc>, - datadir: String, + datadir: PathBuf, writer: W, editor: Editor<(), FileHistory>, ) -> Self { @@ -60,7 +61,10 @@ impl Console { self.write("No previous history."); } self.write("Welcome to the Core Blockchain Console"); - self.write(&format!("Working data directory: {}", self.datadir)); + self.write(&format!( + "Working data directory: {}", + self.datadir.display() + )); self.write(&format!( "Current network_id: {}", self.client.lock().await.get_network_id().await.unwrap() @@ -76,6 +80,7 @@ impl Console { continue; } self.editor.add_history_entry(line.as_str()).unwrap(); + self.editor.save_history(&self.history_file()).unwrap(); match self.evaluate(line).await { Ok(result) => self.write(&result.to_string()), @@ -91,8 +96,6 @@ impl Console { } } } - - self.editor.save_history(&self.history_file()).unwrap(); } // Default command format: module.function(arg1,arg2,...argN) @@ -151,6 +154,6 @@ impl Console { } fn history_file(&self) -> String { - self.datadir.clone() + "/history.txt" + self.datadir.display().to_string() + "/history.txt" } } diff --git a/crates/modules/src/xcbkey.rs b/crates/modules/src/xcbkey.rs index 7d7ed1d..69c71ba 100644 --- a/crates/modules/src/xcbkey.rs +++ b/crates/modules/src/xcbkey.rs @@ -21,7 +21,7 @@ use crate::Module; const ACCOUNT_SUBDIR: &str = "keystore"; pub struct XcbKeyModule { - accounts_dir: String, + accounts_dir: PathBuf, network_id: u64, accounts: Accounts, } @@ -29,11 +29,11 @@ pub struct XcbKeyModule { impl XcbKeyModule { pub async fn new( client: Arc>, - datadir: String, + datadir: PathBuf, accounts: Accounts, ) -> Self { let network_id = client.lock().await.get_network_id().await.unwrap(); - let accounts_dir = format!("{}/{}", datadir, ACCOUNT_SUBDIR); + let accounts_dir = datadir.join(ACCOUNT_SUBDIR); // Create data directory if it does not exist if !PathBuf::from(&accounts_dir).exists() { @@ -272,7 +272,7 @@ impl XcbKeyModule { let account = Account { address: key.address().to_string(), wallet: Some(key.clone()), - path: PathBuf::from(self.accounts_dir.clone() + "/" + &id), + path: self.accounts_dir.join(id), unlocked: 0, }; self.accounts.add_account(account); diff --git a/crates/modules/tests/xcbkey_tests.rs b/crates/modules/tests/xcbkey_tests.rs index 9428f54..692bc1c 100644 --- a/crates/modules/tests/xcbkey_tests.rs +++ b/crates/modules/tests/xcbkey_tests.rs @@ -22,7 +22,7 @@ mod tests { // create a tmp directory for the keystore let accounts = Accounts::new(vec![]); let client = Arc::new(Mutex::new(mock)); - let module = XcbKeyModule::new(client, datadir.display().to_string(), accounts).await; + let module = XcbKeyModule::new(client, datadir.clone(), accounts).await; TestContext { datadir, module } } From fad88d1c47ab24a412ec5f0f9d2d891ca270e798 Mon Sep 17 00:00:00 2001 From: Mykhailo Slyvka Date: Wed, 29 Jan 2025 16:37:31 +0200 Subject: [PATCH 2/2] use version 0.0.9 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8a7ec91..bb79d26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ keywords = ["core blockchain", "xcb", "cli"] license = "MIT OR Apache-2.0" homepage = "https://github.com/core-coin/core-cli" repository = "https://github.com/core-coin/core-cli" -version = "0.0.10" +version = "0.0.9" [workspace.dependencies] # Workspace members