diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 7e4e49d1b3c..3b54edd35bc 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -494,8 +494,8 @@ fn genesis_domain(public_key: PublicKey) -> Domain { /// - If failed to read the config /// - If failed to load the genesis block /// - If failed to build a genesis network -pub fn read_config_and_genesis( - path: impl AsRef, +pub fn read_config_and_genesis>( + path: Option

, submit_genesis: bool, ) -> Result<(Config, Option)> { use iroha_config::parameters::actual::Genesis; diff --git a/cli/src/main.rs b/cli/src/main.rs index b76c8cc0467..34c7909ef9d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -18,7 +18,7 @@ fn default_terminal_colors_str() -> clap::builder::OsStr { struct Args { /// Path to the configuration file #[arg(long, short, value_name("PATH"), value_hint(clap::ValueHint::FilePath))] - config: PathBuf, + config: Option, /// Whether to enable ANSI colored output or not /// /// By default, Iroha determines whether the terminal supports colors or not. @@ -85,7 +85,7 @@ mod tests { #[test] #[allow(clippy::bool_assert_comparison)] // for expressiveness fn default_args() -> Result<()> { - let args = Args::try_parse_from(["test", "--config", "config.toml"])?; + let args = Args::try_parse_from(["test"])?; assert_eq!(args.terminal_colors, is_colouring_supported()); assert_eq!(args.submit_genesis, false); @@ -97,11 +97,11 @@ mod tests { #[allow(clippy::bool_assert_comparison)] // for expressiveness fn terminal_colors_works_as_expected() -> Result<()> { fn try_with(arg: &str) -> Result { - Ok(Args::try_parse_from(["test", arg, "--config", "config.toml"])?.terminal_colors) + Ok(Args::try_parse_from(["test", arg])?.terminal_colors) } assert_eq!( - Args::try_parse_from(["test", "--config", "config.toml"])?.terminal_colors, + Args::try_parse_from(["test"])?.terminal_colors, is_colouring_supported() ); assert_eq!(try_with("--terminal-colors")?, true); @@ -116,7 +116,7 @@ mod tests { fn user_provided_config_path_works() -> Result<()> { let args = Args::try_parse_from(["test", "--config", "/home/custom/file.json"])?; - assert_eq!(args.config, PathBuf::from("/home/custom/file.json")); + assert_eq!(args.config, Some(PathBuf::from("/home/custom/file.json"))); Ok(()) } diff --git a/config/src/parameters/actual.rs b/config/src/parameters/actual.rs index fc3c66afea0..0be623cac18 100644 --- a/config/src/parameters/actual.rs +++ b/config/src/parameters/actual.rs @@ -53,10 +53,14 @@ impl Root { /// - unable to load config from a TOML file /// - unable to parse config from envs /// - the config is invalid - pub fn load(path: impl AsRef, cli: CliContext) -> Result { - let config = RootPartial::from_toml(path)?; - let config = config.merge(RootPartial::from_env(&StdEnv)?); - let config = config.unwrap_partial()?.parse(cli)?; + pub fn load>(path: Option

, cli: CliContext) -> Result { + let from_file = path.map(RootPartial::from_toml).transpose()?; + let from_env = RootPartial::from_env(&StdEnv)?; + let merged = match from_file { + Some(x) => x.merge(from_env), + None => from_env, + }; + let config = merged.unwrap_partial()?.parse(cli)?; Ok(config) } }