Skip to content

Commit

Permalink
[fix]: make --config optional
Browse files Browse the repository at this point in the history
It is still possible to set full config via env
(e.g. `iroha_swarm` case)

Signed-off-by: Dmitry Balashov <[email protected]>
  • Loading branch information
0x009922 committed Feb 13, 2024
1 parent 9a2b9b1 commit 7ec4478
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>,
pub fn read_config_and_genesis<P: AsRef<Path>>(
path: Option<P>,
submit_genesis: bool,
) -> Result<(Config, Option<GenesisNetwork>)> {
use iroha_config::parameters::actual::Genesis;
Expand Down
10 changes: 5 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
/// Whether to enable ANSI colored output or not
///
/// By default, Iroha determines whether the terminal supports colors or not.
Expand Down Expand Up @@ -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);
Expand All @@ -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<bool> {
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);
Expand All @@ -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(())
}
Expand Down
12 changes: 8 additions & 4 deletions config/src/parameters/actual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>, cli: CliContext) -> Result<Self, eyre::Report> {
let config = RootPartial::from_toml(path)?;
let config = config.merge(RootPartial::from_env(&StdEnv)?);
let config = config.unwrap_partial()?.parse(cli)?;
pub fn load<P: AsRef<Path>>(path: Option<P>, cli: CliContext) -> Result<Self, eyre::Report> {
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)
}
}
Expand Down

0 comments on commit 7ec4478

Please sign in to comment.