-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (113 loc) · 3.04 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
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"errors"
"flag"
"fmt"
"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"os"
"strconv"
"strings"
"time"
)
var Log = logrus.New()
// fingerprint file name
var RFingFileName string
// create full fingerprint file = include all files fingerprints
var FullFing bool
func init() {
Log.Formatter = new(prefixed.TextFormatter)
}
func main() {
start := time.Now()
quitLoadingPrint := make(chan struct{})
defer close(quitLoadingPrint)
root, err := parseArgs(quitLoadingPrint)
if err != nil {
Log.Fatal(err)
return
}
if root == nil {
return
}
Log.Infof("Root %s", *root)
Log.Infof("File %s", RFingFileName)
// files fingerprints
ffings, skippedCount, err := BuildFFings(*root)
if err != nil {
Log.Fatal(err)
}
// root fingerprint path
rfingPath := *root + RFingFileName
// load old root fingerprint
oldRfing, err := ReadRFing(rfingPath)
if err != nil {
Log.Error(err)
}
// creat new root fingerprint
newRfing := BuildRFing(ffings)
// indicates if the fingerprint has changed
changed := Compare(oldRfing, &newRfing)
newRfing.Changed = changed
// save new root fingerprint
SaveRFing(newRfing, rfingPath)
outputResults(time.Since(start).Seconds(), ffings, skippedCount, oldRfing, newRfing, rfingPath, changed)
}
// parseArgs parses flags/args and returns root
func parseArgs(quitLoadingPrint <-chan struct{}) (root *string, err error) {
flag.StringVar(&RFingFileName, "f", ".fingerprint", "fingerprint file name")
flag.BoolVar(&FullFing, "files", false, "files, include all files fingerprints in fingerprint file, mind that there might me a lot of them")
debug := flag.Bool("d", false, "debug, turn on debug logging")
quiet := flag.Bool("q", false, "quiet, turn off logging, only print result")
help := flag.Bool("h", false, "help, display usage")
flag.Parse()
if *help {
flag.Usage()
return nil, nil
}
switch {
case *debug:
Log.Level = logrus.DebugLevel
case *quiet:
// extra logging to show we are still alive
go func() {
for {
select {
case <-quitLoadingPrint:
return
default:
for range time.Tick(2 * time.Second) {
fmt.Printf(".")
}
}
}
}()
Log.Level = logrus.FatalLevel
default:
Log.Level = logrus.InfoLevel
}
args := flag.Args()
if len(args) == 0 {
flag.Usage()
return root, errors.New("wrong usage, missing root argument")
}
root = &args[0]
if !strings.HasSuffix(*root, string(os.PathSeparator)) {
tmp := *root + string(os.PathSeparator)
root = &tmp
}
return root, nil
}
func outputResults(seconds float64, ffings []FFing, skippedCount int, oldRfing *RFing, newRfing RFing, rfingPath string, rfingChanged bool) {
Log.Infof("Took %.5f sec", seconds)
Log.Infof("For %d files", len(ffings))
Log.Infof("Skip %d files\n", skippedCount)
oldFingerprint := ""
if oldRfing != nil {
oldFingerprint = oldRfing.Fingerprint
}
fmt.Printf("Old [%s]\n", oldFingerprint)
fmt.Printf("New [%s]\n", newRfing.Fingerprint)
fmt.Printf("@ %s\n", rfingPath)
fmt.Printf("Diff %s\n", strconv.FormatBool(rfingChanged))
}