diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2022-12-28 18:18:39 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2022-12-28 18:46:42 -0500 |
| commit | d7eec191b9b48a52fbca9b6a90357da0e36680ba (patch) | |
| tree | 10c57675415bf8899ec1898180046ceb0e99b6bb /go/cmd/fixpths/main.go | |
| parent | d0ad22b295eae397f39de3a9542ade6320a0591b (diff) | |
| download | dotfiles-0.0.1.tar.gz dotfiles-0.0.1.tar.xz | |
Add a couple of tiny little go utilitiesv0.0.1
Diffstat (limited to 'go/cmd/fixpths/main.go')
| -rw-r--r-- | go/cmd/fixpths/main.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/go/cmd/fixpths/main.go b/go/cmd/fixpths/main.go new file mode 100644 index 0000000..6730276 --- /dev/null +++ b/go/cmd/fixpths/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "flag" + "fmt" + "io/fs" + "log" + "os" + "path/filepath" + "regexp" +) + +func main() { + fl := flag.NewFlagSet("fixpths", flag.ExitOnError) + pth := fl.String("p", "", "Path to scan and fix windows incompatible chars on") + rename := fl.Bool("rename", false, "Whether or not to actually rename the files to work on windows") + fl.Parse(os.Args[1:]) + + if *pth == "" { + fmt.Println("need a path") + os.Exit(2) + } + + invalidChars := regexp.MustCompile(`(<|>|:|\||"|\\|\*|\?)`) + + err := filepath.WalkDir(*pth, func(path string, d fs.DirEntry, err error) error { + if d.IsDir() { + return nil + } + + if invalidChars.MatchString(path) { + if !*rename { + fmt.Printf("%s\n", + invalidChars.ReplaceAllString(path, "\033[1;31m${1}\033[0m")) + } + + if *rename { + npath := invalidChars.ReplaceAllString(path, "_") + err := os.Rename(path, npath) + fmt.Printf("%s->%s\n", path, npath) + if err != nil { + log.Println(err) + } + } + } + + return nil + }) + + if err != nil { + log.Println(err) + } + +} |
