forked from voxelbrain/jsonformat
/
jsonformat.go
101 lines (91 loc) · 2.29 KB
/
jsonformat.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
package main
import (
"encoding/json"
"fmt"
"github.com/voxelbrain/goptions"
"io"
"io/ioutil"
"log"
"os"
)
const (
VERSION = "0.4.0"
)
var (
options = struct {
Continue bool `goptions:"-c, --continue, description='Continue on error'"`
FormatFile string `goptions:"-r, --format-file, description='Read format from file', mutexgroup='input'"`
FormatString string `goptions:"-s, --format-string, description='Format string', mutexgroup='input'"`
Format string `goptions:"-f, --format, obligatory, description='Name for the formatter'"`
goptions.Help `goptions:"-h, --help, description='Show this help'`
}{
Format: "csv",
}
)
func main() {
err := goptions.Parse(&options)
if err != nil || (len(options.FormatFile) <= 0 && len(options.FormatString) <= 0) {
if err == nil {
err = fmt.Errorf("One of --format-file and --format-string must be specified")
}
fmt.Printf("Error: %s\n", err)
goptions.PrintHelp()
fmt.Println("Formatters:")
for name, format := range Formats {
fmt.Printf("\t\"%s\": %s\n", name, format.Description)
}
fmt.Printf("Version %s\n", VERSION)
return
}
format, ok := Formats[options.Format]
if !ok {
log.Fatalf("Unknown format %s", options.Format)
}
f, err := format.Compiler(formatString())
if err != nil {
log.Fatalf("Template invalid: %s", err)
}
dec := json.NewDecoder(os.Stdin)
logFn := NewLogFn(options.Continue)
for {
var input interface{}
err := dec.Decode(&input)
if err == io.EOF {
break
}
if err != nil {
log.Fatalf("Could not decode input: %s", err)
continue
}
err = f.Execute(os.Stdout, input)
if err != nil {
logFn("Could not apply input to template: %s", err)
continue
}
io.WriteString(os.Stdout, "\n")
}
}
type LogFn func(format string, v ...interface{})
func NewLogFn(c bool) LogFn {
if !c {
return func(format string, v ...interface{}) {
log.Fatalf(format, v...)
}
}
return func(format string, v ...interface{}) {
log.Printf(format, v...)
}
}
func formatString() string {
if len(options.FormatFile) > 0 {
d, e := ioutil.ReadFile(options.FormatFile)
if e != nil {
log.Fatalf("Could not read file \"%s\": %s", options.FormatFile, e)
}
return string(d)
}
if len(options.FormatString) > 0 {
return options.FormatString
}
panic("Invalid execution path")
}