-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
180 lines (153 loc) · 3.35 KB
/
ui.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
package main
import (
"flag"
"fmt"
"os"
"runtime/debug"
"sync"
"time"
)
var (
verbose = flag.Bool("v", false, "verbose mode (list each file)")
quiet = flag.Bool("q", false, "quiet mode")
)
func Verbosef(format string, args ...interface{}) {
if *verbose {
fmt.Printf(format+"\n", args...)
}
}
func Printf(format string, args ...interface{}) {
if !*quiet {
fmt.Printf(format+"\n", args...)
}
}
func Fatalf(format string, args ...interface{}) {
fmt.Printf(format+"\n", args...)
os.Exit(1)
}
func PrintVersion() {
info, _ := debug.ReadBuildInfo()
rev := info.Main.Version
ts := time.Time{}
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
rev = s.Value
case "vcs.time":
ts, _ = time.Parse(time.RFC3339, s.Value)
}
}
Printf("summer version %s (%s)", rev, ts)
}
type Progress struct {
start time.Time
isTTY bool
wg sync.WaitGroup
mu sync.Mutex
matched, modified, missing, corrupted int64
done chan bool
}
func NewProgress(isTTY bool) *Progress {
p := &Progress{
start: time.Now(),
done: make(chan bool),
isTTY: isTTY,
}
p.wg.Add(1)
go p.periodicPrint()
return p
}
func (p *Progress) Stop() {
p.done <- true
p.wg.Wait()
}
func (p *Progress) periodicPrint() {
defer p.wg.Done()
ticker := time.NewTicker(250 * time.Millisecond)
defer ticker.Stop()
if *quiet {
<-p.done
return
}
for {
select {
case <-p.done:
p.print(true)
return
case <-ticker.C:
if p.isTTY {
p.print(false)
}
}
}
}
func (p *Progress) print(last bool) {
p.mu.Lock()
defer p.mu.Unlock()
prefix := "\r"
suffix := ""
if last {
suffix = "\n"
}
// Usually we just overwrite the previous line.
// But when verbose, just print one after the other.
// For non-TTY, never overwrite.
if *verbose || !p.isTTY {
prefix = ""
suffix = "\n"
}
fmt.Printf(
prefix+"%v: %d matched, %d modified, %d new, %d corrupted"+suffix,
time.Since(p.start).Round(time.Second),
p.matched, p.modified, p.missing, p.corrupted,
)
}
func (p *Progress) PrintCorrupted(path string, expected, got ChecksumV1) {
p.mu.Lock()
defer p.mu.Unlock()
p.corrupted++
Printf("%q: FILE CORRUPTED - expected:%x, got:%x",
path, expected.CRC32C, got.CRC32C)
}
func (p *Progress) PrintNew(path string, cs ChecksumV1) {
p.mu.Lock()
defer p.mu.Unlock()
p.missing++
Verbosef("%q: writing checksum (checksum:%x, mtime:%d)",
path, cs.CRC32C, cs.ModTimeUsec)
}
func (p *Progress) PrintMissing(path string, cs *ChecksumV1) {
p.mu.Lock()
defer p.mu.Unlock()
p.missing++
if cs == nil {
Verbosef("%q: missing checksum attribute", path)
} else {
Verbosef("%q: missing checksum attribute, adding it "+
"(checksum:%x, mtime:%d)",
path, cs.CRC32C, cs.ModTimeUsec)
}
}
func (p *Progress) PrintModified(path string, old, new_ ChecksumV1) {
p.mu.Lock()
defer p.mu.Unlock()
p.modified++
Verbosef("%q: file modified (not corrupted) "+
"(checksum: %x -> %x, mtime: %d -> %d)",
path, old.CRC32C, new_.CRC32C, old.ModTimeUsec, new_.ModTimeUsec)
}
func (p *Progress) PrintMatched(path string, cs ChecksumV1) {
p.mu.Lock()
defer p.mu.Unlock()
p.matched++
Verbosef("%q: match (checksum:%x, mtime:%d)",
path, cs.CRC32C, cs.ModTimeUsec)
}
type RepeatedStringFlag []string
func (f *RepeatedStringFlag) String() string {
return fmt.Sprintf("%v", *f)
}
func (f *RepeatedStringFlag) Set(value string) error {
*f = append(*f, value)
return nil
}