-
Notifications
You must be signed in to change notification settings - Fork 11
/
json5.go
58 lines (52 loc) · 1.01 KB
/
json5.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/yosuke-furukawa/json5/encoding/json5"
)
func main() {
procArgs()
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: json5 \n")
fmt.Fprintf(os.Stderr, "Compiles JSON5 file into sibling JSON.\n")
flag.PrintDefaults()
os.Exit(2)
}
func procArgs() {
flag.Usage = usage
json5Path := flag.String("c", "", "path/to/file.json5, or blank for stdin")
outputPath := flag.String("o", "", "path/to/file.json, or blank for stdout")
flag.Parse()
var file *os.File
var err error
if *json5Path == "" {
file = os.Stdin
} else {
file, err = os.Open(*json5Path)
if err != nil {
fmt.Println(err)
usage()
}
}
var data interface{}
dec := json5.NewDecoder(file)
err = dec.Decode(&data)
if err != nil {
fmt.Println(err)
usage()
}
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Println(err)
usage()
}
if *outputPath == "" {
fmt.Println(string(b))
} else {
ioutil.WriteFile(*outputPath, b, 0644)
}
}