-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
161 lines (132 loc) · 3.12 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
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
// Command sfv-check accepts one or more SFV-formatted checksum files, and
// verifies the contents of the files listed therein. Mismatches, file errors,
// and success notifications are printed to standard output.
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"github.com/bcc32/sfv-check/sfv"
)
var quiet bool
var tap bool
var changeDir bool
func init() {
log.SetFlags(0)
log.SetOutput(os.Stdout)
const (
defaultQuiet = false
usageQuiet = "suppress OK output for each correct file"
defaultTap = false
usageTap = "print results in TAP format (one SFV file only)"
defaultChangeDir = false
usageChangeDir = "chdir to the directory containing each SFV file"
)
flag.BoolVar(&quiet, "quiet", defaultQuiet, usageQuiet)
flag.BoolVar(&quiet, "q", defaultQuiet, usageQuiet+" (shorthand)")
flag.BoolVar(&tap, "tap", defaultTap, usageTap)
flag.BoolVar(&changeDir, "cd", defaultChangeDir, usageChangeDir)
flag.Usage = func() {
fmt.Fprintf(
os.Stderr,
"Usage: %s [options] SFV-FILE [SFV-FILE]...\n",
os.Args[0],
)
flag.PrintDefaults()
}
}
// checkSFVFile returns an error only if the SFV file cannot be read or is
// malformed. File mismatches and errors are recorded in results.
func checkSFVFile(filename string, results *sfv.ResultSummary) error {
scanner, err := sfv.NewFileScanner(filename)
if err != nil {
return err
}
if changeDir {
dir, err := os.Getwd()
if err != nil {
return err
}
os.Chdir(path.Dir(filename))
defer os.Chdir(dir)
}
for {
for scanner.Scan() {
entry := scanner.Entry()
result := entry.Check()
results.Add(result)
if !quiet || result.Err() != nil {
// FIXME this is a bit precarious, since missing the call to
// String() would result in Error() being called.
log.Print(result.String())
}
}
if err := scanner.Err(); err != nil {
log.Print(err)
} else {
break
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
// tapSFVFile returns an error only if the SFV file cannot be read or is
// malformed. File mismatches and errors are recorded in results.
func tapSFVFile(filename string, results *sfv.ResultSummary) error {
entries, err := sfv.ReadAll(filename)
if err != nil {
return err
}
if changeDir {
dir, err := os.Getwd()
if err != nil {
return err
}
os.Chdir(path.Dir(filename))
defer os.Chdir(dir)
}
log.Printf("1..%d\n", len(entries))
for i, entry := range entries {
result := entry.Check()
results.Add(result)
log.Print(result.TAP(i + 1))
}
return nil
}
func main() {
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
sfvFiles := flag.Args()
var exitCode int
var results sfv.ResultSummary
if tap {
if len(sfvFiles) != 1 {
log.Fatal("-tap can only be used with one SFV file")
}
err := tapSFVFile(sfvFiles[0], &results)
if err != nil {
log.Print(err)
exitCode = 1
}
} else {
for _, file := range sfvFiles {
err := checkSFVFile(file, &results)
if err != nil {
log.Print(err)
exitCode = 1
}
}
}
if err := results.Summary(); err != nil {
log.Printf("%s: %s\n", os.Args[0], err)
exitCode = 1
}
os.Exit(exitCode)
}