-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (110 loc) · 2.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"log"
"os"
"regexp"
"text/template"
"time"
)
const VERSION = `0.1`
var (
ErrorLog = log.New(os.Stderr, `error#`, log.Lshortfile)
DebugLog = log.New(os.Stdout, `debug#`, log.Lshortfile)
)
type TemplateData struct {
FileName string
Name string
Index int
ModTime time.Time
Size int64
}
func helpText() {
fmt.Println(`https://github.com/vvampirius/batch-file-rename`)
flag.PrintDefaults()
}
func GetDstName(n int, srcFileName string, nameRegexp *regexp.Regexp, dstTemplate *template.Template) (string, error) {
fileInfo, err := os.Stat(srcFileName)
if err != nil {
return "", err
}
if fileInfo.IsDir() {
return "", errors.New(`is directory`)
}
matchName := nameRegexp.FindStringSubmatch(srcFileName)
if len(matchName) != 2 {
return "", errors.New(`can't parse name`)
}
buffer := bytes.NewBuffer(nil)
data := TemplateData{
FileName: srcFileName,
Name: matchName[1],
Index: n,
ModTime: fileInfo.ModTime(),
Size: fileInfo.Size(),
}
if err := dstTemplate.Execute(buffer, data); err != nil {
return "", err
}
return buffer.String(), nil
}
func main() {
help := flag.Bool("h", false, "print this help")
ver := flag.Bool("v", false, "Show version")
nameRegexpFlag := flag.String("name", `(.*)`, "Name regexp")
templateFlag := flag.String("template", `{{.Name}}`, "Dst filename template")
testFlag := flag.Bool("test", false, "Don't really rename! Only print new filename.")
flag.Parse()
if *help {
helpText()
os.Exit(0)
}
if *ver {
fmt.Println(VERSION)
os.Exit(0)
}
srcFileNames := flag.Args()
if len(srcFileNames) == 0 {
helpText()
os.Exit(1)
}
nameRegexp, err := regexp.Compile(*nameRegexpFlag)
if err != nil {
ErrorLog.Fatalln(err.Error())
}
dstTemplate, err := template.New(`dst`).Parse(*templateFlag)
if err != nil {
ErrorLog.Fatalln(err.Error())
}
errsCount := 0
n := 0
for _, srcFileName := range srcFileNames {
n++
dstFileName, err := GetDstName(n, srcFileName, nameRegexp, dstTemplate)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR '%s': %s\n", srcFileName, err.Error())
errsCount++
continue
}
fmt.Printf("'%s' -> '%s'\n", srcFileName, dstFileName)
if srcFileName == dstFileName {
fmt.Fprintf(os.Stderr, "ERROR: '%s' = '%s'\n", srcFileName, dstFileName)
errsCount++
continue
}
if *testFlag {
continue
}
if err := os.Rename(srcFileName, dstFileName); err != nil {
fmt.Fprintf(os.Stderr, "ERROR '%s': %s\n", srcFileName, err.Error())
errsCount++
continue
}
}
if errsCount > 0 {
os.Exit(1)
}
}