Implement fmoditer
This commit is contained in:
40
main.go
40
main.go
@@ -5,9 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -15,17 +16,13 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
padSize := flag.Int("n", 5, "Pad size for new file names")
|
|
||||||
recreate := flag.Bool("f", false, "First rename all files to <name>.bak and then rename them to iter (to prevent conflicts of already existing files)")
|
recreate := flag.Bool("f", false, "First rename all files to <name>.bak and then rename them to iter (to prevent conflicts of already existing files)")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
nameFormat := fmt.Sprintf("%%0%dd", *padSize)
|
|
||||||
|
|
||||||
originalExtensions := make(map[string]string)
|
originalExtensions := make(map[string]string)
|
||||||
var mapMutex sync.RWMutex
|
var mapMutex sync.RWMutex
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
var mutex sync.Mutex
|
|
||||||
extensionRegex := regexp.MustCompile(`\.[a-zA-Z0-9]+$`)
|
extensionRegex := regexp.MustCompile(`\.[a-zA-Z0-9]+$`)
|
||||||
var i int32
|
|
||||||
|
|
||||||
if *recreate {
|
if *recreate {
|
||||||
log.Printf("Recreating all files first")
|
log.Printf("Recreating all files first")
|
||||||
@@ -33,15 +30,22 @@ func main() {
|
|||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(file string) {
|
go func(file string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
_, err := os.Stat(file)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
log.Printf("File %s does not exist", file)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
res := extensionRegex.FindAllString(file, -1)
|
res := extensionRegex.FindAllString(file, -1)
|
||||||
if len(res) == 0 {
|
if len(res) == 0 {
|
||||||
log.Printf("No extension found for file %s", file)
|
log.Printf("No extension found for file %s", file)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
dir := path.Dir(file)
|
||||||
extension := res[0]
|
extension := res[0]
|
||||||
bckpFile := file + ".bak"
|
bckpFile := fmt.Sprintf("%s/%s.bak", dir, file)
|
||||||
|
|
||||||
_, err := os.Stat(bckpFile)
|
_, err = os.Stat(bckpFile)
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
log.Printf("File %s already exists", bckpFile)
|
log.Printf("File %s already exists", bckpFile)
|
||||||
return
|
return
|
||||||
@@ -59,9 +63,9 @@ func main() {
|
|||||||
}(file)
|
}(file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
wg = sync.WaitGroup{}
|
wg = sync.WaitGroup{}
|
||||||
|
|
||||||
for _, file := range flag.Args() {
|
for _, file := range flag.Args() {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(file string) {
|
go func(file string) {
|
||||||
@@ -81,12 +85,12 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex.Lock()
|
modtime := info.ModTime()
|
||||||
atomic.AddInt32(&i, 1)
|
modtimeString := modtime.Local().Format("2006-01-02T15-04-05")
|
||||||
newFile := fmt.Sprintf(nameFormat, i)
|
newFile := modtimeString
|
||||||
mutex.Unlock()
|
|
||||||
|
|
||||||
res := extensionRegex.FindAllString(file, -1)
|
bakless := strings.TrimRight(file, ".bak")
|
||||||
|
res := extensionRegex.FindAllString(bakless, -1)
|
||||||
if len(res) == 0 {
|
if len(res) == 0 {
|
||||||
log.Printf("No extension found for file %s", file)
|
log.Printf("No extension found for file %s", file)
|
||||||
return
|
return
|
||||||
@@ -100,12 +104,18 @@ func main() {
|
|||||||
extension = prevExtension
|
extension = prevExtension
|
||||||
}
|
}
|
||||||
|
|
||||||
newFile = newFile + extension
|
dir := path.Dir(file)
|
||||||
|
newFile = fmt.Sprintf("%s/%s%s", dir, newFile, extension)
|
||||||
|
newFile = path.Clean(newFile)
|
||||||
|
newFile = strings.TrimRight(newFile, ".bak")
|
||||||
|
|
||||||
_, err = os.Stat(newFile)
|
_, err = os.Stat(newFile)
|
||||||
if !os.IsNotExist(err) {
|
if os.IsExist(err) {
|
||||||
log.Printf("File %s already exists", newFile)
|
log.Printf("File %s already exists", newFile)
|
||||||
return
|
return
|
||||||
|
} else if !os.IsNotExist(err) {
|
||||||
|
log.Printf("An unexpected error occurred: %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%s -> %s", file, newFile)
|
log.Printf("%s -> %s", file, newFile)
|
||||||
|
Reference in New Issue
Block a user