Skip to content

Commit

Permalink
Merge pull request #2 from espressif/logging
Browse files Browse the repository at this point in the history
switched from print! to logging and added verbose flag
  • Loading branch information
Hahihula authored Jun 26, 2024
2 parents 36a8b28 + e2fa7d7 commit 32e8a84
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 147 deletions.
5 changes: 4 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ linker = "/usr/bin/i686-w64-mingw32-gcc"
ar = "/usr/i686-w64-mingw32/bin/ar"

[target.aarch64-pc-windows-msvc]
linker = "x86_64-w64-mingw32-gcc"
linker = "x86_64-w64-mingw32-gcc"

[target.'cfg(all(windows, target_env = "msvc"))']
rustflags = ["-C", "target-feature=+crt-static"]
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ console = "0.15.8"
config = "0.14.0"
serde = { version = "1.0", features = ["derive"] }
git2 = "0.19.0"
log = "0.4.21"
simple_logger = "5.0.0"
94 changes: 24 additions & 70 deletions src/cli_args/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use clap::Parser;
use clap::{arg, ValueEnum};
use config::{Config, ConfigError, File};
use log::error;
use serde::Deserialize;
use simple_logger::SimpleLogger;
use std::path::PathBuf;
use std::{fmt, str::FromStr};
// use clap::{command, Parser, ValueEnum};
use clap::{arg, value_parser, Command, ValueEnum};

const VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down Expand Up @@ -54,6 +55,14 @@ pub struct Cli {
tools_json_file: Option<String>,
#[arg(short, long)]
non_interactive: Option<bool>,

#[arg(
short,
long,
action = clap::ArgAction::Count,
help = "Increase verbosity level (can be used multiple times)"
)]
verbose: u8,
}

impl IntoIterator for Cli {
Expand Down Expand Up @@ -99,6 +108,18 @@ impl IntoIterator for Cli {
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let cli = Cli::parse();
let log_level = match cli.verbose {
0 => log::LevelFilter::Warn,
1 => log::LevelFilter::Info,
2 => log::LevelFilter::Debug,
_ => log::LevelFilter::Trace,
};
match SimpleLogger::new().with_level(log_level).init() {
Ok(_) => {}
Err(e) => {
error!("Failed to initialize logger: {}", e);
}
}

let mut builder = Config::builder()
// Start off by merging in the "default" configuration file
Expand All @@ -114,7 +135,7 @@ impl Settings {
}

// Add in settings from the environment (with a prefix of ESP)
// Eg.. `ESP_DEBUG=1 ./target/app` would set the `debug` key
// Eg.. `ESP_TARGET=esp32` would set the `target` key
builder = builder.add_source(config::Environment::with_prefix("ESP").separator("_"));

// Now that we've gathered all our config sources, let's merge them
Expand Down Expand Up @@ -214,70 +235,3 @@ impl ValueEnum for ChipId {
})
}
}

pub fn get_cli() -> clap::Command {
Command::new("ESP-IDF Installation Manager")
.version(VERSION)
.about("All you need to manage your ESP-IDF installations")
.arg(
arg!(
-p --path <PATH> "base instalation path"
)
.required(false)
.value_parser(value_parser!(PathBuf)),
)
.arg(
arg!(
-t --target <VALUE> "which chip you are using"
)
.required(false)
.value_parser(value_parser!(ChipId)),
)
.arg(
arg!(
--"idf-version" <VALUE> "which version of idf we want to install"
)
.required(false)
.value_parser(value_parser!(String)),
)
.arg(
arg!(
-c --"config-file" <VALUE> "path to file with instalator configuration"
)
.required(false)
.value_parser(value_parser!(PathBuf)),
)
.arg(
arg!(
-n --"non-interactive" <BOOL> "show the wizard"
)
.required(false)
.value_parser(value_parser!(bool))
.default_value(std::ffi::OsStr::new("false")),
)
}

pub fn parse_cli(arg_matches: &clap::ArgMatches) -> Settings {
let mut config = Settings::default();

// we need to parse config file first, because cli params have higher priority
// TODO: we shoud parse env even before
if let Some(config_file) = arg_matches.get_one::<PathBuf>("config-file") {
println!("Value for config_file: {:?}", config_file);
println!("Parsing config file not implemented yet");
}

if let Some(path) = arg_matches.get_one::<PathBuf>("path") {
config.path = Some(path.to_owned());
}
if let Some(target) = arg_matches.get_one::<ChipId>("target") {
config.target = Some(target.to_string().to_lowercase());
}
if let Some(idf_version) = arg_matches.get_one::<String>("idf-version") {
config.idf_version = Some(idf_version.to_owned());
}
if let Some(non_interactive) = arg_matches.get_one::<bool>("non-interactive") {
config.non_interactive = Some(*non_interactive);
}
config
}
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use log::{error, info};
use simple_logger::SimpleLogger;
extern crate idf_im_lib;

mod cli_args;
Expand All @@ -11,13 +13,14 @@ async fn main() {
let result = wizard::run_wizzard_run(settings).await;
match result {
Ok(r) => {
println!("Successfully installed IDF {:?}", r);
info!("Wizard result: {:?}", r);
println!("Successfully installed IDF");
println!("Now you can start using IDF tools");
}
Err(err) => println!("Error: {}", err),
Err(err) => error!("Error: {}", err),
}
}
Err(err) => println!("Error: {}", err),
Err(err) => error!("Error: {}", err),
}

// next step is source the env vars
Expand Down
Loading

0 comments on commit 32e8a84

Please sign in to comment.