Skip to content

Commit

Permalink
Merge pull request fedimint#3860 from dpc/23-12-06-backport-mk-data-dir
Browse files Browse the repository at this point in the history
fix(fedimint-cli): create data dir if doesn't exist
  • Loading branch information
elsirion authored Dec 7, 2023
2 parents 5b7d058 + 0678368 commit 33ad57d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
11 changes: 6 additions & 5 deletions devimint/src/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client> {
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()
Expand Down Expand Up @@ -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<String> {
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)
}

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ Commands:
help Print this message or the help of the given subcommand(s)
Options:
--data-dir <WORKDIR> The working directory of the client containing the config and db [env: FM_DATA_DIR=]
--data-dir <DATA_DIR> The working directory of the client containing the config and db [env: FM_CLIENT_DIR=]
--our-id <OUR_ID> Peer id of the guardian [env: FM_OUR_ID=]
--password <PASSWORD> Guardian password for authentication [env: FM_PASSWORD=]
-h, --help Print help
Expand Down
17 changes: 13 additions & 4 deletions fedimint-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
data_dir: Option<PathBuf>,

/// Peer id of the guardian
#[arg(env = "FM_OUR_ID", long, value_parser = parse_peer_id)]
Expand All @@ -247,12 +247,21 @@ 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 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()?;

Ok(dir)
}

fn admin_client(&self, cfg: &ClientConfig) -> CliResult<WsAdminClient> {
let our_id = &self
.our_id
Expand All @@ -277,7 +286,7 @@ impl Opts {
}

async fn load_rocks_db(&self) -> CliResult<Locked<fedimint_rocksdb::RocksDb>> {
let db_path = self.workdir()?.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
Expand Down

0 comments on commit 33ad57d

Please sign in to comment.