use std::{env, path::PathBuf};
use anyhow::{anyhow, Error};
use tracing::{debug, info};
pub fn config_dir() -> Result<PathBuf, Error> {
let mut config_dir = dirs::config_dir().ok_or_else(|| {
anyhow!(
"Default wallet directory is not supported in this platform: \
please specify storage and wallet paths"
)
})?;
config_dir.push("linera");
if !config_dir.exists() {
debug!("Creating default wallet directory {}", config_dir.display());
fs_err::create_dir_all(&config_dir)?;
}
info!("Using default wallet directory {}", config_dir.display());
Ok(config_dir)
}
pub fn wallet_path(explicit: Option<&PathBuf>, suffix: &str) -> Result<PathBuf, Error> {
if let Some(path) = explicit {
return Ok(path.clone());
}
let wallet_env_var = env::var(format!("LINERA_WALLET{suffix}")).ok();
if let Some(path) = wallet_env_var {
return Ok(path.parse()?);
}
Ok(config_dir()?.join("wallet.json"))
}
pub fn keystore_path(explicit: Option<&PathBuf>, suffix: &str) -> Result<PathBuf, Error> {
if let Some(path) = explicit {
return Ok(path.clone());
}
let keystore_env_var = env::var(format!("LINERA_KEYSTORE{suffix}")).ok();
if let Some(path) = keystore_env_var {
return Ok(path.parse()?);
}
Ok(config_dir()?.join("keystore.json"))
}