Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bin/miden-cli/src/commands/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use miden_client::{Client, PrettyPrint, ZERO};

use crate::config::CliConfig;
use crate::errors::CliError;
use crate::utils::{load_config_file, load_faucet_details_map, parse_account_id};
use crate::utils::{get_cli_config, load_faucet_details_map, parse_account_id};
use crate::{client_binary_name, create_dynamic_table};

pub const DEFAULT_ACCOUNT_ID_KEY: &str = "default_account_id";
Expand Down Expand Up @@ -41,7 +41,7 @@ pub struct AccountCmd {

impl AccountCmd {
pub async fn execute<AUTH>(&self, mut client: Client<AUTH>) -> Result<(), CliError> {
let (cli_config, _) = load_config_file()?;
let cli_config = get_cli_config()?;
match self {
AccountCmd {
list: false,
Expand All @@ -50,7 +50,7 @@ impl AccountCmd {
..
} => {
let account_id = parse_account_id(&client, id).await?;
show_account(client, account_id, &cli_config, self.with_code).await?;
show_account(client, account_id, cli_config, self.with_code).await?;
},
AccountCmd {
list: false,
Expand Down
4 changes: 2 additions & 2 deletions bin/miden-cli/src/commands/new_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tracing::debug;

use crate::commands::account::set_default_account_if_unset;
use crate::errors::CliError;
use crate::{CliKeyStore, client_binary_name, load_config_file};
use crate::{CliKeyStore, client_binary_name, get_cli_config};

// CLI TYPES
// ================================================================================================
Expand Down Expand Up @@ -200,7 +200,7 @@ impl NewAccountCmd {
/// Reads component templates from the given file paths.
// TODO: IO errors should have more context
fn load_component_templates(paths: &[PathBuf]) -> Result<Vec<AccountComponentTemplate>, CliError> {
let (cli_config, _) = load_config_file()?;
let cli_config = get_cli_config()?;
let components_base_dir = &cli_config.component_template_directory;
let mut templates = Vec::new();
for path in paths {
Expand Down
4 changes: 2 additions & 2 deletions bin/miden-cli/src/commands/new_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use crate::create_dynamic_table;
use crate::errors::CliError;
use crate::utils::{
SHARED_TOKEN_DOCUMENTATION,
get_cli_config,
get_input_acc_id_by_prefix_or_default,
load_config_file,
load_faucet_details_map,
parse_account_id,
};
Expand Down Expand Up @@ -411,7 +411,7 @@ async fn execute_transaction<AUTH: TransactionAuthenticator + Sync + 'static>(
.collect::<Vec<_>>();

if delegated_proving {
let (cli_config, _) = load_config_file()?;
let cli_config = get_cli_config()?;
let remote_prover_endpoint =
cli_config.remote_prover_endpoint.as_ref().ok_or(CliError::Config(
"Remote prover endpoint".to_string().into(),
Expand Down
4 changes: 2 additions & 2 deletions bin/miden-cli/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use miden_client::store::NoteFilter;
use super::config::CliConfig;
use crate::commands::account::DEFAULT_ACCOUNT_ID_KEY;
use crate::errors::CliError;
use crate::load_config_file;
use crate::get_cli_config;

pub async fn print_client_info<AUTH: TransactionAuthenticator + Sync + 'static>(
client: &Client<AUTH>,
) -> Result<(), CliError> {
let (config, _) = load_config_file()?;
let config = get_cli_config()?;

println!("Client version: {}", env!("CARGO_PKG_VERSION"));
print_config_stats(&config)?;
Expand Down
4 changes: 2 additions & 2 deletions bin/miden-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use commands::sync::SyncCmd;
use commands::tags::TagsCmd;
use commands::transactions::TransactionCmd;

use self::utils::load_config_file;
use self::utils::get_cli_config;

pub type CliKeyStore = FilesystemKeyStore<StdRng>;

Expand Down Expand Up @@ -168,7 +168,7 @@ impl Cli {
};

// Create the client
let (cli_config, _config_path) = load_config_file()?;
let cli_config = get_cli_config()?;

let keystore = CliKeyStore::new(cli_config.secret_keys_directory.clone())
.map_err(CliError::KeyStore)?;
Expand Down
31 changes: 20 additions & 11 deletions bin/miden-cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

use figment::Figment;
use figment::providers::{Format, Toml};
Expand Down Expand Up @@ -77,18 +78,26 @@ pub(crate) async fn parse_account_id<AUTH>(
}
}

/// Loads config file from current directory and default filename and returns it alongside its path.
///
/// This function will look for the configuration file at the provided path. If the path is
/// relative, searches in parent directories all the way to the root as well.
pub(super) fn load_config_file() -> Result<(CliConfig, PathBuf), CliError> {
let mut current_dir = std::env::current_dir()?;
// CACHED CONFIG
// ================================================================================================

static CLI_CONFIG: OnceLock<CliConfig> = OnceLock::new();

/// Returns a reference to the global CLI configuration, loading it once on first access.
pub fn get_cli_config() -> Result<&'static CliConfig, CliError> {
// Resolve config path relative to current working directory
let mut current_dir = std::env::current_dir().map_err(CliError::IO)?;
current_dir.push(CLIENT_CONFIG_FILE_NAME);
let config_path = current_dir.as_path();
let config_path = current_dir;

let cli_config = load_config(config_path)?;
if let Some(cfg) = CLI_CONFIG.get() {
return Ok(cfg);
}

Ok((cli_config, config_path.into()))
let cfg = load_config(config_path.as_path())?;
// If another thread initialized first, ignore the error and return the stored one
let _ = CLI_CONFIG.set(cfg);
Ok(CLI_CONFIG.get().expect("CLI_CONFIG must be initialized"))
}

/// Loads the client configuration.
Expand All @@ -100,6 +109,6 @@ fn load_config(config_file: &Path) -> Result<CliConfig, CliError> {

/// Returns the faucet details map using the config file.
pub fn load_faucet_details_map() -> Result<FaucetDetailsMap, CliError> {
let (config, _) = load_config_file()?;
FaucetDetailsMap::new(config.token_symbol_map_filepath)
let config = get_cli_config()?;
FaucetDetailsMap::new(config.token_symbol_map_filepath.clone())
}