-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniname.go
187 lines (166 loc) · 3.93 KB
/
uniname.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
var version string
var (
optHelp bool
optVersion bool
optRename bool
optOutputDir string
optMD5 bool
optSha1 bool
optSha256 bool
optSha512 bool
)
func display(s string) {
fmt.Fprintln(os.Stderr, s)
}
func commandName() (name string) {
name = filepath.Base(os.Args[0])
if name == "" {
name = "uniname"
}
return
}
func displayUsage() {
name := commandName()
display(fmt.Sprintf("Usage:\n %[1]s [-r] [--sha1|--sha256|--sha512] <input file>"+
"\n %[1]s [--sha1|--sha256|--sha512] <input file> -d <output directory>", name))
display(fmt.Sprintf("\nExample:\n %[1]s -r foo.png"+
"\n %[1]s -r --sha256 foo.png\n %[1]s --sha512 foo.png -d foo/images", name))
display("\nOptional flags:")
flag.PrintDefaults()
}
func displayVersion() {
display(fmt.Sprintf("%s version %s", commandName(), version))
}
func init() {
flag.Usage = displayUsage
}
func fileSum(mode, file string) (string, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}
switch mode {
case "sha1":
h := sha1.New()
h.Write(data)
return strings.ToLower(hex.EncodeToString(h.Sum(nil))), nil
case "sha256":
h := sha256.New()
h.Write(data)
return strings.ToLower(hex.EncodeToString(h.Sum(nil))), nil
case "sha512":
h := sha512.New()
h.Write(data)
return strings.ToLower(hex.EncodeToString(h.Sum(nil))), nil
default:
h := md5.New()
h.Write(data)
return strings.ToLower(hex.EncodeToString(h.Sum(nil))), nil
}
}
func copyFile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
if _, err = io.Copy(dstFile, srcFile); err != nil {
return err
}
if err = dstFile.Sync(); err != nil {
return err
}
return nil
}
func main() {
flag.BoolVar(&optHelp, "h", false, "print help")
flag.BoolVar(&optVersion, "v", false, "print version")
flag.BoolVar(&optRename, "r", false, "rename the input file")
flag.StringVar(&optOutputDir, "d", "", "rename to the specified directory")
flag.BoolVar(&optMD5, "md5", true, "using md5sum")
flag.BoolVar(&optSha1, "sha1", false, "using sha1sum")
flag.BoolVar(&optSha256, "sha256", false, "using sha256sum")
flag.BoolVar(&optSha512, "sha512", false, "using sha512sum")
flag.Parse()
if optVersion {
displayVersion()
os.Exit(0)
}
if optHelp || flag.NArg() == 0 {
displayUsage()
os.Exit(0)
}
srcFilePath := flag.Arg(0)
if f, err := os.Stat(srcFilePath); err != nil {
if os.IsNotExist(err) {
log.Fatal(err)
}
log.Fatal(err)
} else if f.IsDir() {
log.Fatal(os.ErrNotExist)
}
if optOutputDir != "" {
if optOutputDir == "." {
optRename = true
optOutputDir = ""
} else if f, err := os.Stat(optOutputDir); err != nil {
if err = os.MkdirAll(optOutputDir, 0700); err != nil {
log.Fatal(err)
}
} else if !f.IsDir() {
optOutputDir = filepath.Dir(optOutputDir)
}
}
var mode string
switch {
case optSha1:
mode = "sha1"
case optSha256:
mode = "sha256"
case optSha512:
mode = "sha512"
default:
mode = "md5"
}
dstFileName, err := fileSum(mode, srcFilePath)
if err != nil {
log.Fatal(err)
}
dstFileName += filepath.Ext(srcFilePath)
if optRename {
dstFilePath := filepath.Join(filepath.Dir(srcFilePath), dstFileName)
if err = os.Rename(srcFilePath, dstFilePath); err != nil {
log.Fatal(err)
}
display(fmt.Sprintf("%s: '%s' rename to '%s'", mode, srcFilePath, dstFileName))
} else if optOutputDir != "" {
dstFilePath := filepath.Join(optOutputDir, dstFileName)
if err = copyFile(srcFilePath, dstFilePath); err != nil {
log.Fatal(err)
}
display(fmt.Sprintf("%s: '%s' copy to '%s'", mode, srcFilePath, dstFilePath))
} else {
display(fmt.Sprintf("%s: unique name is '%s'", mode, dstFileName))
}
}