diff --git a/Cargo.lock b/Cargo.lock index 293f55b..650304b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,6 +67,16 @@ dependencies = [ "wait-timeout", ] +[[package]] +name = "atomicwrites" +version = "0.4.2" +source = "git+https://github.com/jackpot51/rust-atomicwrites#043ab4859d53ffd3d55334685303d8df39c9f768" +dependencies = [ + "rustix", + "tempfile", + "windows-sys 0.48.0", +] + [[package]] name = "bitflags" version = "2.6.0" @@ -147,6 +157,7 @@ name = "cosmic-ctl" version = "1.0.0" dependencies = [ "assert_cmd", + "atomicwrites", "bracoxide", "clap", "etcetera", diff --git a/Cargo.toml b/Cargo.toml index 4cd269b..79546bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "cosmic-ctl" description = "CLI for COSMIC Desktop configuration management" -version = "1.0.0" +version = "1.1.0" authors = ["Heitor Augusto "] homepage = "https://github.com/cosmic-utils/cosmic-ctl" repository = "https://github.com/cosmic-utils/cosmic-ctl" @@ -13,6 +13,7 @@ documentation = "https://github.com/cosmic-utils/cosmic-ctl/wiki" readme = "README.md" [dependencies] +atomicwrites = { git = "https://github.com/jackpot51/rust-atomicwrites" } bracoxide = "0.1.4" clap = { version = "4.5.21", features = ["derive"] } etcetera = "0.8.0" diff --git a/default.nix b/default.nix index e7d02a8..e643466 100644 --- a/default.nix +++ b/default.nix @@ -10,7 +10,7 @@ pkgs.callPackage ( cosmic-comp, }: let - version = "1.0.0"; + version = "1.1.0"; in rustPlatform.buildRustPackage { pname = "cosmic-ctl"; @@ -21,7 +21,8 @@ pkgs.callPackage ( path = ./.; }; - cargoHash = "sha256-Nrg7NOAUrVQcwBz7nV3hILRYHr1dprQ5VJj2u7Zf3Q0="; + useFetchCargoVendor = true; + cargoHash = "sha256-EReo2hkBaIO1YOBx4D9rQSXlx+3NK5VQtj59jfZZI/0="; doInstallCheck = true; nativeInstallCheckInputs = [ versionCheckHook ]; diff --git a/flake.lock b/flake.lock index 725679f..ad56595 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1731676054, - "narHash": "sha256-OZiZ3m8SCMfh3B6bfGC/Bm4x3qc1m2SVEAlkV6iY7Yg=", + "lastModified": 1734649271, + "narHash": "sha256-4EVBRhOjMDuGtMaofAIqzJbg4Ql7Ai0PSeuVZTHjyKQ=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "5e4fbfb6b3de1aa2872b76d49fafc942626e2add", + "rev": "d70bd19e0a38ad4790d3913bf08fcbfc9eeca507", "type": "github" }, "original": { diff --git a/src/config.rs b/src/config.rs index 201fed8..23d0eb1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,10 +1,11 @@ +use atomicwrites::{AtomicFile, OverwriteBehavior}; use etcetera::{ base_strategy::{BaseStrategy, Xdg}, choose_base_strategy, }; use std::{ fs, - io::{Error, ErrorKind}, + io::{Error, ErrorKind, Write}, path::{Path, PathBuf}, }; use unescaper::unescape; @@ -60,18 +61,23 @@ pub fn write_configuration( } } - fs::create_dir_all(path.parent().unwrap_or_else(|| Path::new(""))).map_err(|e| { - Error::new( - ErrorKind::Other, - format!("Failed to create directory structure: {}", e), - ) - })?; - fs::write(&path, unescaped_value).map_err(|e| { - Error::new( - ErrorKind::Other, - format!("Failed to write configuration to {}: {}", path.display(), e), - ) - })?; + if let Some(parent) = path.parent() { + fs::create_dir_all(parent).map_err(|e| { + Error::new( + ErrorKind::Other, + format!("Failed to create directory structure: {}", e), + ) + })?; + } + + let af = AtomicFile::new(&path, OverwriteBehavior::AllowOverwrite); + af.write(|f| f.write_all(unescaped_value.as_bytes())) + .map_err(|e| { + Error::new( + ErrorKind::Other, + format!("Failed to write configuration to {}: {}", path.display(), e), + ) + })?; Ok(true) }