From 3d6dc458ce459ccc458656c8c2d7438185852ff1 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 24 Oct 2024 22:33:09 +0200 Subject: [PATCH] actually move files using git mv --- main.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index d9f938e..b6ef912 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,10 @@ package main import ( -"fmt" - "path/filepath" + "fmt" "io/fs" + "os/exec" + "path/filepath" "regexp" ) @@ -13,12 +14,22 @@ func main() { if err != nil { panic(err) } - filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error{ + filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error { path = path[len(dir):] matches := journalRegex.FindStringSubmatch(path) - if len(matches) > 0 { - fmt.Println(path) - fmt.Println(matches[1]+"/"+matches[2]+" - "+ matches[3]+"/"+matches[1]+"-"+matches[2]+"-"+matches[4] + " - " + matches[5]) + if len(matches) == 0 { + return nil + } + + year, month := matches[1], matches[2] + monthName := matches[3] + day, title := matches[4], matches[5] + newPath := fmt.Sprintf("%s/%s - %s/%s-%s-%s - %s", year, month, monthName, year, month, day, title) + mvCommand := exec.Command("git", "mv", path, newPath) + mvCommand.Dir = dir + output, err := mvCommand.CombinedOutput() + if err != nil { + fmt.Printf("error moving '%s' to '%s':\n%s\n\n", path, newPath, output) } return nil })