Skip to content

Commit

Permalink
Fix cli args merging (#49)
Browse files Browse the repository at this point in the history
* fixed cli arg prioritization

* fixed multiple versions and target input over cli args

---------

Co-authored-by: Petr Gadorek <[email protected]>
  • Loading branch information
Hahihula and Petr Gadorek authored Sep 20, 2024
1 parent 1340297 commit 6c79f8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
10 changes: 0 additions & 10 deletions eim_config.toml

This file was deleted.

30 changes: 22 additions & 8 deletions src/cli_args/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use clap::builder::styling::{AnsiColor, Color, Style, Styles};
use clap::{arg, command, ColorChoice, Parser};
use config::{Config, ConfigError, File};
use config::{Config, ConfigError, File, ValueKind};
use idf_im_lib::get_log_directory;
use log::{debug, info, LevelFilter};
use serde::{Deserialize, Serialize};
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use toml::value::Array;

use log4rs::{
append::{console::ConsoleAppender, file::FileAppender},
Expand Down Expand Up @@ -61,13 +62,27 @@ fn custom_styles() -> Styles {
styles = custom_styles()
)]
pub struct Cli {
#[arg(
short,
long,
help = "Base Path to which all the files and folder will be installed"
)]
path: Option<String>,
#[arg(short, long, value_name = "FILE")]
config: Option<PathBuf>,

#[arg(short, long)]
#[arg(
short,
long,
help = "You can provide multiple targets separated by comma"
)]
target: Option<String>,

#[arg(short, long)]
#[arg(
short,
long,
help = "you can provide multiple versions of ESP-IDF separated by comma"
)]
idf_versions: Option<String>,

#[arg(long)]
Expand Down Expand Up @@ -144,17 +159,15 @@ impl Settings {

builder = builder.add_source(config::Environment::with_prefix("ESP").separator("_"));

let mut cfg = builder.build()?;

for (key, value) in cli.into_iter() {
if let Some(v) = value {
if key != "config" {
debug!("Setting {} to {:?}", key, v);
cfg.set(&key, v)?;
builder = builder.set_override(key, v)?;
}
}
}

let cfg = builder.build()?;
cfg.try_deserialize()
}

Expand Down Expand Up @@ -246,6 +259,7 @@ impl IntoIterator for Cli {

fn into_iter(self) -> Self::IntoIter {
vec![
("path".to_string(), self.path.map(Into::into)),
(
"config".to_string(),
self.config.map(|p| p.to_str().unwrap().into()),
Expand All @@ -260,7 +274,7 @@ impl IntoIterator for Cli {
.map(|s| s.split(',').collect::<Vec<&str>>().into()),
),
(
"idf_version".to_string(),
"idf_versions".to_string(),
self.idf_versions
.map(|s| s.split(',').collect::<Vec<&str>>().into()),
),
Expand Down

0 comments on commit 6c79f8c

Please sign in to comment.