Skip to content

Commit

Permalink
Merge pull request #67 from marcusbuffett/create-dirs
Browse files Browse the repository at this point in the history
Create directories if needed (#43)
  • Loading branch information
mtimkovich authored May 21, 2023
2 parents 2db7c4e + b422547 commit ba3249e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pipe-rename"
version = "1.6.2"
version = "1.6.3"
authors = ["Marcus Buffett <[email protected]>"]
description = "Rename your files using your favorite text editor"
homepage = "https://github.com/marcusbuffett/pipe-rename"
Expand Down
30 changes: 23 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use text_diff::{calculate_text_diff, TextDiff};
#[derive(Parser, Debug)]
#[clap(
version = env!("CARGO_PKG_VERSION"),
author = "Marcus B. <me@mbufett.com>",
author = "Marcus B. <me@mbuffett.com>",
about = "https://github.com/marcusbuffett/pipe-rename",
long_about = "Takes a list of files and renames/moves them by piping them through an external editor"
)]
Expand Down Expand Up @@ -220,7 +220,12 @@ fn open_editor(input_files: &[String], editor_string: &str) -> anyhow::Result<Ve
.args(&editor_parsed[1..])
.arg(tmpfile.path())
.spawn()
.with_context(|| format!("Failed to execute editor command: '{}'", shell_words::join(editor_parsed)))?;
.with_context(|| {
format!(
"Failed to execute editor command: '{}'",
shell_words::join(editor_parsed)
)
})?;

let output = child.wait_with_output()?;
if !output.status.success() {
Expand Down Expand Up @@ -306,7 +311,19 @@ fn execute_renames(
.arg(&replacement.new)
.join()?;
} else {
fs::rename(&replacement.original, &replacement.new)?;
match fs::rename(&replacement.original, &replacement.new) {
Ok(()) => (),
// If renaming fails, try creating parent directories and try again.
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
let dir = &replacement.new.parent();
if let Some(dir) = dir {
fs::create_dir_all(&dir)?;
}

fs::rename(&replacement.original, &replacement.new)?;
}
Err(e) => return Err(e.into()),
};
}
}

Expand Down Expand Up @@ -417,10 +434,9 @@ fn main() -> anyhow::Result<()> {

let default_editor = if cfg!(windows) { "notepad.exe" } else { "vim" };
let default_editor = default_editor.to_string();
let editor = opts.editor
.unwrap_or_else(
|| env::var("EDITOR")
.unwrap_or(default_editor));
let editor = opts
.editor
.unwrap_or_else(|| env::var("EDITOR").unwrap_or(default_editor));
let mut buffer = input_files.clone();

loop {
Expand Down

0 comments on commit ba3249e

Please sign in to comment.