Go: Added --always

Fortune for lazy-rebuild's current commit: Future blessing − 末吉
rust-version
Avril 3 years ago
parent d37532960c
commit d7421a1583
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,5 +1,7 @@
all: clean lazy-rebuild test .PHONY: all
all: | clean
$(MAKE) lazy-rebuild test
clean: clean:
rm -rf test rm -rf test

@ -0,0 +1,3 @@
module lazy-rebuild
go 1.17

@ -1,4 +1,3 @@
//go:binary-only-package
package main package main
import ( import (
@ -12,6 +11,7 @@ import (
"os" "os"
"sync" "sync"
"strconv" "strconv"
"regexp"
) )
var keepLongExt bool = false var keepLongExt bool = false
@ -42,6 +42,12 @@ func hash(file string) []byte {
return h.Sum(nil) return h.Sum(nil)
} }
var isHashRE *regexp.Regexp = regexp.MustCompile("^[0-9a-f]{64}$")
func is_hash(name string) bool {
name = strings.Split(name, ".")[0]
return isHashRE.MatchString(name)
}
func extractExt(x string) string { func extractExt(x string) string {
if (keepLongExt) { if (keepLongExt) {
return last(strings.SplitN(x, ".", 2)[1:]) return last(strings.SplitN(x, ".", 2)[1:])
@ -50,8 +56,13 @@ func extractExt(x string) string {
} }
} }
var alwaysCheck bool = false
func dofile(i int, y string) { func dofile(i int, y string) {
loc, x := filepath.Split(y) loc, x := filepath.Split(y)
if !alwaysCheck && is_hash(x) {
fmt.Printf("E: %s already a hash, skipping.\n", x)
return
}
sha := hash(y) sha := hash(y)
str := fmt.Sprintf("%x", sha) str := fmt.Sprintf("%x", sha)
newname := fmt.Sprintf("%s%s%s", loc, str, extractExt(x)) newname := fmt.Sprintf("%s%s%s", loc, str, extractExt(x))
@ -72,7 +83,7 @@ func main() {
var wg sync.WaitGroup var wg sync.WaitGroup
if (len(ar) < 1) { if (len(ar) < 1) {
fmt.Printf("Usage: %s [--long] [--fake] [--recurse] [-quiet] [--threads <number>] <file ...>\n\t--long\tKeep long file extensions\n\t--fake\tDo not rename files.\n\t--recurse\tWalk path recursively.\n\t--quiet\tDo not show (some) errors.\n", os.Args[0]) fmt.Printf("Usage: %s [--long] [--fake] [--recurse] [-quiet] [--threads <number>] [--always] <file ...>\n\t--long\tKeep long file extensions\n\t--fake\tDo not rename files.\n\t--recurse\tWalk path recursively.\n\t--quiet\tDo not show (some) errors.\n\t--always\tDo not skip filenames which are already hashes.\n", os.Args[0])
return; return;
} }
var ignore = false var ignore = false
@ -91,6 +102,8 @@ func main() {
recurse = !recurse recurse = !recurse
case "quiet": case "quiet":
noErr = !noErr noErr = !noErr
case "always":
alwaysCheck = true
case "threads": case "threads":
if i<len(ar)-1 { if i<len(ar)-1 {
if th, err := strconv.Atoi(ar[i+1]); err ==nil { if th, err := strconv.Atoi(ar[i+1]); err ==nil {

Loading…
Cancel
Save