Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix edit bug
If the edit file existed and you chose the "Abort" option the file would
be deleted by mistake.
  • Loading branch information
rossmacarthur committed Mar 13, 2021
1 parent b5f5de0 commit a4a0602
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
11 changes: 11 additions & 0 deletions RELEASES.md
@@ -1,5 +1,16 @@
# Releases

## 0.6.2

*Unreleased*

- Fix edit bug. If the edit file existed and you chose the "Abort" option the
file would be deleted by mistake.
- [Always include details section in version output.][92a23b5] This was
previously excluded if there was no Git information.

[92a23b5]: https://github.com/rossmacarthur/sheldon/commit/92a23b5289c4c206a228e6bf11ce937c4649047b

## 0.6.1

*February 12th, 2021*
Expand Down
13 changes: 7 additions & 6 deletions src/editor.rs
Expand Up @@ -97,11 +97,12 @@ impl Editor {
pub fn edit(self, path: &Path, contents: &str) -> Result<Child> {
let (overwrite, temp) = match TempPath::new(path) {
Ok(temp) => (true, temp),
Err(temp) => {
let temp_contents = fs::read_to_string(&temp.path())
.context("failed to read from temporary file")?;
Err(path) => {
let temp_contents =
fs::read_to_string(&path).context("failed to read from temporary file")?;
let temp = || TempPath::new_unchecked(path);
if temp_contents == contents {
(false, temp)
(false, temp())
} else {
match casual::prompt(
"It looks like you already started editing the config file, what do you \
Expand All @@ -110,8 +111,8 @@ impl Editor {
.get()
{
Choice::Abort => bail!("aborted!"),
Choice::Reopen => (false, temp),
Choice::Overwrite => (true, temp),
Choice::Reopen => (false, temp()),
Choice::Overwrite => (true, temp()),
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/util.rs
Expand Up @@ -131,16 +131,15 @@ impl TempPath {
/// # Errors
///
/// If the temporary path already exists.
pub fn new(original_path: &Path) -> result::Result<Self, Self> {
pub fn new(original_path: &Path) -> result::Result<Self, PathBuf> {
let mut path = original_path.parent().unwrap().to_path_buf();
let mut file_name = ffi::OsString::from("~");
file_name.push(original_path.file_name().unwrap());
path.push(file_name);
let temp = Self { path: Some(path) };
if temp.path().exists() {
Err(temp)
if path.exists() {
Err(path)
} else {
Ok(temp)
Ok(Self::new_unchecked(path))
}
}

Expand All @@ -149,13 +148,18 @@ impl TempPath {
pub fn new_force(original_path: &Path) -> Result<Self> {
match Self::new(original_path) {
Ok(temp) => Ok(temp),
Err(temp) => {
nuke_path(temp.path())?;
Ok(temp)
Err(path) => {
nuke_path(&path)?;
Ok(Self::new_unchecked(path))
}
}
}

/// Create a new `TempPath` using the given temporary path.
pub fn new_unchecked(path: PathBuf) -> Self {
Self { path: Some(path) }
}

/// Access the underlying `Path`.
pub fn path(&self) -> &Path {
self.path.as_ref().unwrap()
Expand Down

0 comments on commit a4a0602

Please sign in to comment.