Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config save #11

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
11 changes: 10 additions & 1 deletion locales/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 更新失败
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: 配置保存失败
31 changes: 25 additions & 6 deletions src/cli_args/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use clap::Parser;
use clap::{arg, ValueEnum};
use config::{Config, ConfigError, File};
use log::{error, info};
use serde::Deserialize;
use log::{debug, error, info};
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<PathBuf>,
pub idf_path: Option<PathBuf>,
Expand Down Expand Up @@ -161,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));
}

Expand All @@ -171,17 +175,32 @@ 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()
}

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)]
Expand Down
37 changes: 27 additions & 10 deletions src/wizard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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![
Expand All @@ -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 {
Expand Down Expand Up @@ -528,7 +529,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)
Expand All @@ -553,7 +554,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)
Expand Down Expand Up @@ -583,7 +584,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)
Expand Down Expand Up @@ -629,7 +630,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![
Expand All @@ -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,
Expand All @@ -672,14 +673,15 @@ 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(),
));

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)
Expand Down Expand Up @@ -782,6 +784,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(())
}
Loading