From c3fdc9d623b3e57d965d6e6ec9a23339ca752114 Mon Sep 17 00:00:00 2001 From: n0m1s Date: Wed, 1 Jan 2020 22:47:29 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20DL=20JSON=20to=20tmp=20file=20to?= =?UTF-8?q?=20always=20have=20a=20clean=20one?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gitmoji.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/gitmoji.rs b/src/gitmoji.rs index 7528ec1..456de3d 100644 --- a/src/gitmoji.rs +++ b/src/gitmoji.rs @@ -2,7 +2,7 @@ extern crate git2; extern crate curl; extern crate derive_more; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::io::Write; use std::fs::File; @@ -93,7 +93,14 @@ pub fn update(url: &Url, repo_path: &Path, json_path: &Path) -> Result<(), Retri return Ok(()); } - let mut file = File::create(&json_path)?; + let mut json_tmp_path = PathBuf::new(); + json_tmp_path.push(json_path); + json_tmp_path.set_extension("json.new"); + let json_tmp_path = json_tmp_path; + + println!("tmp: \"{}\", path: \"{}\"", json_tmp_path.display(), json_path.display()); + + let mut file = File::create(&json_tmp_path)?; let mut curl = Easy::new(); curl.url(&url.json_url)?; @@ -105,5 +112,7 @@ pub fn update(url: &Url, repo_path: &Path, json_path: &Path) -> Result<(), Retri })?; transfer.perform()?; + std::fs::rename(json_tmp_path, json_path)?; + Ok(()) }