-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
45 lines (36 loc) · 908 Bytes
/
process.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
package cmd
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/spf13/pflag"
)
type processFunc func(filename *string, input io.Reader) interface{}
var (
processFlags pflag.FlagSet
compact = processFlags.BoolP("compact", "c", false, "disable pretty-printing of JSON output")
)
func processFiles(filenames []string, process processFunc) {
encoder := json.NewEncoder(os.Stdout)
if !*compact {
encoder.SetIndent("", " ")
}
encoder.SetEscapeHTML(false)
if len(os.Args) <= 1 {
encoder.Encode(process(nil, os.Stdin))
return
}
for _, filename := range filenames {
processFile(filename, process, encoder)
}
}
func processFile(filename string, process processFunc, encoder *json.Encoder) {
file, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to open file: %v\n", err)
os.Exit(2)
}
defer file.Close()
encoder.Encode(process(&filename, file))
}