/
json5.go
53 lines (48 loc) · 924 Bytes
/
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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/yosuke-furukawa/json5/encoding/json5"
"io/ioutil"
"os"
)
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")
outputPath := flag.String("o", "", "path/to/file.json")
flag.Parse()
if *json5Path == "" {
usage()
}
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))
}
ioutil.WriteFile(*outputPath, b, 0644)
}