rename-journal/main.go
2024-10-24 22:33:09 +02:00

37 lines
899 B
Go

package main
import (
"fmt"
"io/fs"
"os/exec"
"path/filepath"
"regexp"
)
func main() {
dir := "/home/jacob/Notes/Journal/"
journalRegex, err := regexp.Compile("^(\\d\\d\\d\\d)/(\\d\\d) - ([^/]*)/(\\d\\d) - ([^/]*)$")
if err != nil {
panic(err)
}
filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
path = path[len(dir):]
matches := journalRegex.FindStringSubmatch(path)
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
})
}