-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
66 lines (53 loc) · 1.06 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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/vikyd/go-checksum/checksum"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("needs parameters: go.mod or dir")
return
}
file := os.Args[1]
fi, err := os.Stat(file)
if err != nil {
fmt.Println(err)
return
}
switch mode := fi.Mode(); {
case mode.IsDir():
fmt.Println("directory: " + file)
doDir(file)
case mode.IsRegular():
fmt.Println("file: " + file)
doGoMod(file)
}
}
func doDir(dir string) {
if len(os.Args) < 3 {
fmt.Println("needs parameters: module path with version like: github.com/gin-gonic/[email protected]")
return
}
prefix := os.Args[2]
h, err := checksum.HashDir(dir, prefix)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(PrettyPrint(h))
}
func doGoMod(file string) {
h, err := checksum.HashGoMod(file)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(PrettyPrint(h))
}
// PrettyPrint convert struct to pretty string
func PrettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}