Skip to content

Commit

Permalink
protect against overwriting existing stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
polvoazul committed Jul 12, 2024
1 parent 665d89b commit 7ded84e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions vimv.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func main() {
to_rename = append(to_rename, FilePair{from: files[i], to: new_filenames[i]})
}
}
assert_no_swap(to_rename)

report(to_rename)
errors := rename(to_rename)
Expand All @@ -69,6 +70,21 @@ func main() {
}
}

func assert_no_swap(to_rename []FilePair) {
names := make(map[string]int)
for _, pair := range to_rename {
names[pair.to]++
names[pair.from]++
}
for name := range names {
if names[name] > 0 {
cleanup_afterwards = false
fmt.Printf("Error: Filename being renamed already exists, swap is unsuported as of now: '%s'\n", name)
panic(Exit{1})
}
}
}

type FilePair struct {
from string
to string
Expand Down Expand Up @@ -178,6 +194,12 @@ func show_diff(pairs []FilePair) {
func rename(to_rename []FilePair) []error {
var errs []error
for _, fp := range to_rename {
if _, err := os.Stat(fp.to); err == nil {
e := fmt.Errorf("destination file %s already exists", fp.to)
fmt.Println(e)
errs = append(errs, e)
continue
}
err := os.Rename(fp.from, fp.to)
if err != nil {
e := fmt.Errorf("error renaming %s to %s: %v", fp.from, fp.to, err)
Expand Down

0 comments on commit 7ded84e

Please sign in to comment.