Skip to content

Commit fa8932e

Browse files
Migrate old config (#883)
* feat: Migrate old config * docs: Udpate changelog * feat: Update espflash.toml config
1 parent dd0b18d commit fa8932e

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
- Run some arguments checks for monitoring flags. (#842)
2828
- Add support for the ESP32-C5 (#863)
2929
- `--after` options now work with `espflash board-info`, `espflash read-flash` and `espflash checksum-md5` (#867)
30-
- Add support for serial port configuration files. (#777)
30+
- Add support for serial port configuration files. (#777, #883)
3131
- Add a `check-app-descriptor` bool option to `ImageArgs` and add the flag to `flash` commad(#872)
3232
- `Connection::into_serial` to get the underlying port from the connection (#882)
3333

@@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5050
- Updated bootloaders with `release/v5.4` ones from IDF (#857)
5151
- Refactor image formatting to allow supporting more image formats in a backward compatible way (#877)
5252
- Avoid having ESP-IDF format assumptions in the codebase (#877)
53+
- Automatically migrate `espflash@3` configuration files to the new format (#883)
5354
- `Flasher` now takes the `Connection` in new, instead of constructing the connection inside `Flasher::connect` (#882)
5455
- `detect_chip` has moved to the `Connection` struct (#882)
5556
- `Flasher::into_serial` has been replaced by `Flasher::into_connection` (#882)

espflash/src/cli/config.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ impl Config {
141141

142142
let mut port_config = if let Ok(data) = read_to_string(&port_config_file) {
143143
toml::from_str(&data).into_diagnostic()?
144+
} else if let Ok(data) = read_to_string(&project_config_file) {
145+
if data.contains("[connection]") || data.contains("[[usb_device]]") {
146+
log::info!(
147+
"espflash@3 configuration detected. Migrating port config to port_config_file: {:#?}",
148+
&port_config_file
149+
);
150+
// Write the updated configs
151+
let port_config = toml::from_str(&data).into_diagnostic()?;
152+
Self::write_config(&port_config, &port_config_file)?;
153+
Self::write_config(&project_config, &project_config_file)?;
154+
155+
port_config
156+
} else {
157+
PortConfig::default()
158+
}
144159
} else {
145160
PortConfig::default()
146161
};
@@ -153,26 +168,28 @@ impl Config {
153168
})
154169
}
155170

171+
fn write_config<T: Serialize>(config: &T, path: &PathBuf) -> Result<()> {
172+
let serialized = toml::to_string(config)
173+
.into_diagnostic()
174+
.wrap_err("Failed to serialize config")?;
175+
176+
if let Some(parent) = path.parent() {
177+
create_dir_all(parent)
178+
.into_diagnostic()
179+
.wrap_err("Failed to create config directory")?;
180+
}
181+
182+
write(path, serialized)
183+
.into_diagnostic()
184+
.wrap_err_with(|| format!("Failed to write config to {}", path.display()))
185+
}
186+
156187
/// Save port configuration to the configuration file
157188
pub fn save_with<F: Fn(&mut Self)>(&self, modify_fn: F) -> Result<()> {
158189
let mut copy = self.clone();
159190
modify_fn(&mut copy);
160191

161-
let serialized = toml::to_string(&copy.port_config)
162-
.into_diagnostic()
163-
.wrap_err("Failed to serialize config")?;
164-
165-
create_dir_all(self.port_config.save_path.parent().unwrap())
166-
.into_diagnostic()
167-
.wrap_err("Failed to create config directory")?;
168-
write(&self.port_config.save_path, serialized)
169-
.into_diagnostic()
170-
.wrap_err_with(|| {
171-
format!(
172-
"Failed to write config to {}",
173-
self.port_config.save_path.display()
174-
)
175-
})
192+
Self::write_config(&copy.port_config, &self.port_config.save_path)
176193
}
177194

178195
fn project_config_path() -> Result<PathBuf, Error> {

0 commit comments

Comments
 (0)