From 93829f77ceda2e7adde5d957d2c216238a97807c Mon Sep 17 00:00:00 2001 From: Petr Gadorek Date: Fri, 12 Jul 2024 14:27:40 +0200 Subject: [PATCH 1/3] added possibility to save the config file after the sucessfull instalation --- Cargo.lock | 3 ++- Cargo.toml | 1 + locales/app.yml | 11 ++++++++++- src/cli_args/mod.rs | 20 ++++++++++++++++++-- src/wizard/mod.rs | 34 +++++++++++++++++++++++++--------- 5 files changed, 56 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 253e993..cbc8964 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1774,13 +1774,14 @@ dependencies = [ "serde", "simple_logger", "tokio", + "toml 0.8.14", "walkdir", ] [[package]] name = "idf-im-lib" version = "0.1.0" -source = "git+https://github.com/espressif/idf-im-lib.git?branch=master#828fabcb78fb9a8ac092ff248094e9ffe775c9e2" +source = "git+https://github.com/espressif/idf-im-lib.git?branch=master#9d0913152a1e8562c9670ead3edcad3605ad52e4" dependencies = [ "colored", "decompress", diff --git a/Cargo.toml b/Cargo.toml index 4a4a409..f457453 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,4 @@ log = "0.4.21" simple_logger = "5.0.0" rust-i18n = "3.0.1" dirs = "5.0.1" +toml = "0.8.14" diff --git a/locales/app.yml b/locales/app.yml index 983229e..09987ef 100644 --- a/locales/app.yml +++ b/locales/app.yml @@ -163,4 +163,13 @@ wizard.shellrc.update.success: cn: ESP-IDF shellrc 已更新成功 wizard.shellrc.update.error: en: ESP-IDF shellrc update failed - cn: ESP-IDF shellrc 更新失败 \ No newline at end of file + cn: ESP-IDF shellrc 更新失败 +wizard.after_install.save_config.prompt: + en: Do you want to save the installer configuration? + cn: 是否要保存安装器配置 +wizard.after_install.config.saved: + en: Configuration saved successfully to config.toml + cn: 配置已保存成功到 config.toml +wizard.after_install.config.save_failed: + en: Configuration save failed + cn: 配置保存失败 \ No newline at end of file diff --git a/src/cli_args/mod.rs b/src/cli_args/mod.rs index 80a33ec..d680ce9 100644 --- a/src/cli_args/mod.rs +++ b/src/cli_args/mod.rs @@ -2,14 +2,17 @@ use clap::Parser; use clap::{arg, ValueEnum}; use config::{Config, ConfigError, File}; use log::{error, info}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use simple_logger::SimpleLogger; +use std::fs::OpenOptions; +use std::io::Write; use std::path::PathBuf; use std::{fmt, str::FromStr}; +use toml::Value; const VERSION: &str = env!("CARGO_PKG_VERSION"); -#[derive(Debug, Deserialize, Default)] +#[derive(Debug, Deserialize, Default, Serialize)] pub struct Settings { pub path: Option, pub idf_path: Option, @@ -182,6 +185,19 @@ impl Settings { // You can deserialize (and thus freeze) the entire configuration cfg.try_deserialize() } + + pub fn save(&self, file_path: &str) -> Result<(), config::ConfigError> { + let toml_value = toml::to_string(&self).unwrap(); + let mut file = OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open(file_path) + .unwrap(); + file.write_all(toml_value.as_bytes()).unwrap(); + + Ok(()) + } } #[derive(Clone, Debug)] diff --git a/src/wizard/mod.rs b/src/wizard/mod.rs index 4432b51..6365157 100644 --- a/src/wizard/mod.rs +++ b/src/wizard/mod.rs @@ -450,12 +450,12 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { } } } - let idf_versions = config.idf_version.unwrap(); + let idf_versions = config.idf_version.clone().unwrap(); debug!("Selected idf version: {}", idf_versions); // select folder // instalation path consist from base path and idf version let mut instalation_path: PathBuf = PathBuf::new(); - if let Some(path) = config.path { + if let Some(path) = config.path.clone() { instalation_path.push(&path); } else { let mut default_path = "/tmp/esp-new/".to_string(); @@ -482,7 +482,7 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { config.idf_path = Some(idf_path.clone()); idf_im_lib::add_path_to_path(idf_path.to_str().unwrap()); - let idf_mirror = match config.idf_mirror { + let idf_mirror = match config.idf_mirror.clone() { Some(mirror) => mirror, None => { let mirrors = vec![ @@ -528,7 +528,7 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { let mut tool_download_directory = PathBuf::new(); tool_download_directory.push(&instalation_path); - if let Some(name) = config.tool_download_folder_name { + if let Some(name) = config.tool_download_folder_name.clone() { tool_download_directory.push(&name); } else { let name = match Input::with_theme(&theme) @@ -553,7 +553,7 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { } let mut tool_install_directory = PathBuf::new(); tool_install_directory.push(&instalation_path); - if let Some(name) = config.tool_install_folder_name { + if let Some(name) = config.tool_install_folder_name.clone() { tool_install_directory.push(&name); } else { let name = match Input::with_theme(&theme) @@ -583,7 +583,7 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { let mut tools_json_file = PathBuf::new(); tools_json_file.push(&idf_path); - if let Some(file) = config.tools_json_file { + if let Some(file) = config.tools_json_file.clone() { tools_json_file.push(&file); } else { let name = match Input::with_theme(&theme) @@ -629,7 +629,7 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { ); } }; - let dl_mirror = match config.mirror { + let dl_mirror = match config.mirror.clone() { Some(mirror) => mirror, None => { let mirrors = vec![ @@ -672,6 +672,7 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { python_env_path.push("python"); env::set_var("IDF_PYTHON_ENV_PATH", &python_env_path); + debug!("Python env path: {}", python_env_path.display()); env_vars.push(( "IDF_PYTHON_ENV_PATH".to_string(), python_env_path.to_str().unwrap().to_string(), @@ -679,7 +680,7 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { let mut idf_tools_path = PathBuf::new(); idf_tools_path.push(&idf_path); - if let Some(file) = config.idf_tools_path { + if let Some(file) = config.idf_tools_path.clone() { idf_tools_path.push(&file); } else { let name = match Input::with_theme(&theme) @@ -782,6 +783,21 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { } } - // TODO: offer to save settings + match Confirm::new() + .with_prompt(t!("wizard.after_install.save_config.prompt")) + .interact() + { + Ok(true) => match config.save("config.toml") { + // TODO: make path configurable + Ok(_) => println!("{}", t!("wizard.after_install.config.saved")), + Err(err) => panic!( + "{} {:?}", + t!("wizard.after_install.config.save_failed"), + err.to_string() + ), + }, + _ => (), + } + Ok(()) } From bd099d1c2565ba83003fdeac2d6630df119f23e8 Mon Sep 17 00:00:00 2001 From: Petr Gadorek Date: Fri, 12 Jul 2024 15:03:11 +0200 Subject: [PATCH 2/3] added mirrors to the config --- src/cli_args/mod.rs | 11 +++++++---- src/wizard/mod.rs | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/cli_args/mod.rs b/src/cli_args/mod.rs index d680ce9..ea171cd 100644 --- a/src/cli_args/mod.rs +++ b/src/cli_args/mod.rs @@ -1,7 +1,7 @@ use clap::Parser; use clap::{arg, ValueEnum}; use config::{Config, ConfigError, File}; -use log::{error, info}; +use log::{debug, error, info}; use serde::{Deserialize, Serialize}; use simple_logger::SimpleLogger; use std::fs::OpenOptions; @@ -164,6 +164,7 @@ impl Settings { // If a config file was specified via cli arg, add it here if let Some(config_path) = cli.config.clone() { + debug!("Using config file: {}", config_path.display()); builder = builder.add_source(File::from(config_path)); } @@ -174,14 +175,16 @@ impl Settings { // Now that we've gathered all our config sources, let's merge them let mut cfg = builder.build()?; + // Add in cli-specified values for (key, value) in cli.into_iter() { if let Some(v) = value { - cfg.set(&key, v)?; + if key != "config" { + debug!("Setting {} to {:?}", key, v); + cfg.set(&key, v)?; + } } } - // Add in cli-specified values - // You can deserialize (and thus freeze) the entire configuration cfg.try_deserialize() } diff --git a/src/wizard/mod.rs b/src/wizard/mod.rs index 6365157..bc3fe7d 100644 --- a/src/wizard/mod.rs +++ b/src/wizard/mod.rs @@ -499,6 +499,7 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { .to_string() } }; + config.idf_mirror = Some(idf_mirror.clone()); let group_name = if idf_mirror.contains("https://gitee.com/") { Some("EspressifSystems") } else { @@ -646,7 +647,7 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { .to_string() } }; - + config.mirror = Some(dl_mirror.clone()); let downloaded_tools_list = download_tools( tools.clone(), &target, From 8aa6a17cbf13ad3f5a99ae4b2b51d055f2c4612c Mon Sep 17 00:00:00 2001 From: Petr Gadorek Date: Fri, 12 Jul 2024 15:29:21 +0200 Subject: [PATCH 3/3] added config to readme --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index d82631d..4fac781 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,20 @@ If you go trought the wizard, your choices will have the higher precedence. the installer takes config toml file. it serches for it in the default location ./config/default.toml but you can specify path to the config with `--config` cli argument +example config: +```toml +path = "/tmp/esp-new/" +idf_path = "/tmp/esp-new/v5.2.2/esp-idf" +tool_download_folder_name = "dist" +tool_install_folder_name = "tools" +target = "esp32" +idf_version = "v5.2.2" +tools_json_file = "tools/tools.json" +idf_tools_path = "tools/idf_tools.py" +mirror = "https://github.com" +idf_mirror = "https://github.com" +``` + ### Env variables you can override any of the settings by exporting env variable prefixed by `ESP_` like `ESP_TARGET`