From 079fdf86c7a56b81ce360209156a3f48cd39c399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ci=C4=99=C5=BCarkiewicz?= Date: Mon, 4 Dec 2023 00:03:27 -0800 Subject: [PATCH 1/2] fix(fedimint-cli): create data dir if doesn't exist --- fedimint-cli/src/lib.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/fedimint-cli/src/lib.rs b/fedimint-cli/src/lib.rs index f7a48f9e6aa..506833f4231 100644 --- a/fedimint-cli/src/lib.rs +++ b/fedimint-cli/src/lib.rs @@ -253,6 +253,15 @@ impl Opts { .ok_or_cli_msg(CliErrorKind::IOError, "`--data-dir=` argument not set.") } + /// Get and create if doesn't exist the workdir + async fn workdir_create(&self) -> CliResult<&PathBuf> { + let dir = self.workdir()?; + + tokio::fs::create_dir_all(&dir).await.map_err_cli_io()?; + + Ok(dir) + } + fn admin_client(&self, cfg: &ClientConfig) -> CliResult { let our_id = &self .our_id @@ -277,7 +286,7 @@ impl Opts { } async fn load_rocks_db(&self) -> CliResult> { - let db_path = self.workdir()?.join("client.db"); + let db_path = self.workdir_create().await?.join("client.db"); let lock_path = db_path.with_extension("db.lock"); Ok(LockedBuilder::new(&lock_path) .await From 06783681b40729b9695ad2599c5b3f85fb8a9fa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ci=C4=99=C5=BCarkiewicz?= Date: Mon, 4 Dec 2023 00:16:42 -0800 Subject: [PATCH 2/2] chore: don't use old `workdir` term anymore --- devimint/src/federation.rs | 11 ++++++----- docs/tutorial.md | 2 +- fedimint-cli/src/lib.rs | 14 +++++++------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/devimint/src/federation.rs b/devimint/src/federation.rs index 9dfac6428dc..e458bfc6f8a 100644 --- a/devimint/src/federation.rs +++ b/devimint/src/federation.rs @@ -51,15 +51,15 @@ impl Client { /// TODO: Get rid of built-in client, make it a normal `Client` and let them /// fork each other as they please. async fn new_forked(name: &str) -> Result { - let workdir: PathBuf = env::var("FM_DATA_DIR")?.parse()?; - let client_dir = workdir.join("clients").join(name); + let data_dir: PathBuf = env::var("FM_DATA_DIR")?.parse()?; + let client_dir = data_dir.join("clients").join(name); std::fs::create_dir_all(&client_dir)?; cmd!( "cp", "-R", - workdir.join("client.db").display(), + data_dir.join("client.db").display(), client_dir.join("client.db").display() ) .run() @@ -131,9 +131,10 @@ impl Federation { load_from_file(&cfg_path) } + /// Read the invite code from the client data dir pub fn invite_code(&self) -> Result { - let workdir: PathBuf = env::var("FM_DATA_DIR")?.parse()?; - let invite_code = fs::read_to_string(workdir.join("invite-code"))?; + let data_dir: PathBuf = env::var("FM_DATA_DIR")?.parse()?; + let invite_code = fs::read_to_string(data_dir.join("invite-code"))?; Ok(invite_code) } diff --git a/docs/tutorial.md b/docs/tutorial.md index e86d58d4e17..02511e06499 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -191,7 +191,7 @@ Commands: help Print this message or the help of the given subcommand(s) Options: - --data-dir The working directory of the client containing the config and db [env: FM_DATA_DIR=] + --data-dir The working directory of the client containing the config and db [env: FM_CLIENT_DIR=] --our-id Peer id of the guardian [env: FM_OUR_ID=] --password Guardian password for authentication [env: FM_PASSWORD=] -h, --help Print help diff --git a/fedimint-cli/src/lib.rs b/fedimint-cli/src/lib.rs index 506833f4231..9233e477562 100644 --- a/fedimint-cli/src/lib.rs +++ b/fedimint-cli/src/lib.rs @@ -232,7 +232,7 @@ impl fmt::Display for CliError { struct Opts { /// The working directory of the client containing the config and db #[arg(long = "data-dir", alias = "workdir", env = "FM_DATA_DIR")] - workdir: Option, + data_dir: Option, /// Peer id of the guardian #[arg(env = "FM_OUR_ID", long, value_parser = parse_peer_id)] @@ -247,15 +247,15 @@ struct Opts { } impl Opts { - fn workdir(&self) -> CliResult<&PathBuf> { - self.workdir + fn data_dir(&self) -> CliResult<&PathBuf> { + self.data_dir .as_ref() .ok_or_cli_msg(CliErrorKind::IOError, "`--data-dir=` argument not set.") } - /// Get and create if doesn't exist the workdir - async fn workdir_create(&self) -> CliResult<&PathBuf> { - let dir = self.workdir()?; + /// Get and create if doesn't exist the data dir + async fn data_dir_create(&self) -> CliResult<&PathBuf> { + let dir = self.data_dir()?; tokio::fs::create_dir_all(&dir).await.map_err_cli_io()?; @@ -286,7 +286,7 @@ impl Opts { } async fn load_rocks_db(&self) -> CliResult> { - let db_path = self.workdir_create().await?.join("client.db"); + let db_path = self.data_dir_create().await?.join("client.db"); let lock_path = db_path.with_extension("db.lock"); Ok(LockedBuilder::new(&lock_path) .await