Copy `path` to ` .bak` if it exists. Used before overwriting a user config so an unexpected change is recoverable (issue #63).
(path: &Path)
| 1429 | /// Copy `path` to `<path>.bak` if it exists. Used before overwriting a user |
| 1430 | /// config so an unexpected change is recoverable (issue #63). |
| 1431 | fn backup_file(path: &Path) -> Result<()> { |
| 1432 | if !path.exists() { |
| 1433 | return Ok(()); |
| 1434 | } |
| 1435 | let mut backup = path.as_os_str().to_owned(); |
| 1436 | backup.push(".bak"); |
| 1437 | let backup = std::path::PathBuf::from(backup); |
| 1438 | std::fs::copy(path, &backup).map_err(|e| TraceDecayError::Config { |
| 1439 | message: format!( |
| 1440 | "failed to back up {} to {}: {e}", |
| 1441 | path.display(), |
| 1442 | backup.display() |
| 1443 | ), |
| 1444 | })?; |
| 1445 | eprintln!( |
| 1446 | "\x1b[32m✔\x1b[0m Backed up {} to {}", |
| 1447 | path.display(), |
| 1448 | backup.display() |
| 1449 | ); |
| 1450 | Ok(()) |
| 1451 | } |
| 1452 | |
| 1453 | /// Write a TOML value to a file, backing up any existing file first. |
| 1454 | pub fn write_toml_file(path: &Path, value: &toml::Value) -> Result<()> { |
no test coverage detected