Skip to content

Commit

Permalink
utils: Add get_configuration_for_binaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Sep 6, 2024
1 parent 2cfff4a commit f6216a5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn get_configuration() -> BridgeConfig {
/// Reads configuration file, parses it and generates a `BridgeConfig` from
/// given cli arguments.
pub fn get_configuration_from(args: Args) -> Result<BridgeConfig, BridgeError> {
match BridgeConfig::try_parse_file(args.config_file) {
match BridgeConfig::try_parse_file(args.config_file.clone()) {
Ok(c) => Ok(c),
Err(e) => Err(BridgeError::ConfigError(e.to_string())),
}
Expand Down
55 changes: 51 additions & 4 deletions core/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::cli::Args;
use crate::config::BridgeConfig;
use crate::errors::BridgeError;
use crate::transaction_builder::TxHandler;
use bitcoin;
Expand Down Expand Up @@ -51,6 +53,50 @@ pub fn parse_hex_to_btc_tx(
}
}

/// Gets configuration from CLI, for binaries. If there are any errors, print
/// error to stderr and exit program.
///
/// Steps:
///
/// 1. Get CLI arguments
/// 2. Initialize logger
/// 3. Get configuration file
///
/// These steps are pretty standard and binaries can use this to get a
/// `BridgeConfig`.
///
/// # Returns
///
/// A tuple, containing:
///
/// - [`BridgeConfig`] from CLI argument
/// - [`Args`] from CLI options
pub fn get_configuration_for_binaries() -> (BridgeConfig, Args) {
let args = match crate::cli::parse() {
Ok(args) => args,
Err(e) => {
eprintln!("{e}");
exit(1);
}
};
match crate::utils::initialize_logger(args.verbose) {
Ok(args) => args,
Err(e) => {
eprintln!("{e}");
exit(1);
}
};
let config = match crate::cli::get_configuration_from(args.clone()) {
Ok(config) => config,
Err(e) => {
eprintln!("{e}");
exit(1);
}
};

(config, args)
}

pub fn usize_to_var_len_bytes(x: usize) -> Vec<u8> {
let usize_bytes = (usize::BITS / 8) as usize;
let bits = x.max(1).ilog2() + 1;
Expand Down Expand Up @@ -123,7 +169,7 @@ pub fn get_claim_reveal_indices(depth: usize, count: u32) -> Vec<(usize, usize)>
///
/// Returns `Err` if `tracing` can't be initialized. Multiple subscription error
/// is emmitted and will return `Ok(())`.
pub fn initialize_logger(level: u8) -> Result<(), tracing_subscriber::util::TryInitError> {
pub fn initialize_logger(level: u8) -> Result<(), BridgeError> {
let level = match level {
0 => None,
1 => Some(LevelFilter::ERROR),
Expand All @@ -132,8 +178,9 @@ pub fn initialize_logger(level: u8) -> Result<(), tracing_subscriber::util::TryI
4 => Some(LevelFilter::DEBUG),
5 => Some(LevelFilter::TRACE),
_ => {
eprintln!("Verbosity level can only be between 0 and 5 (given {level})!");
exit(1);
return Err(BridgeError::ConfigError(format!(
"Verbosity level can only be between 0 and 5 (given {level})!"
)))
}
};

Expand All @@ -151,7 +198,7 @@ pub fn initialize_logger(level: u8) -> Result<(), tracing_subscriber::util::TryI
// If it failed because of a re-initialization, do not care about
// the error.
if e.to_string() != "a global default trace dispatcher has already been set" {
return Err(e);
return Err(BridgeError::ConfigError(e.to_string()));
}

tracing::trace!("Tracing is already initialized, skipping without errors...");
Expand Down

0 comments on commit f6216a5

Please sign in to comment.