-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (83 loc) · 1.76 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
// Command json2yaml converts JSON documents to YAML.
//
// If command is called by yaml2json name, then it converts from YAML to JSON.
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
func main() {
flag.Parse()
log.SetFlags(0)
if err := run(flag.Arg(0)); err != nil {
log.Fatal(err)
}
}
func run(name string) error {
input, output := os.Stdin, os.Stdout
if name != "" && name != "-" {
f, err := os.Open(name)
if err != nil {
return err
}
defer f.Close()
input = f
}
if reverseMode {
return yaml2json(input, output)
}
return json2yaml(input, output)
}
func json2yaml(input io.Reader, output io.Writer) error {
dec := json.NewDecoder(input)
enc := yaml.NewEncoder(output)
defer enc.Close()
for {
var doc interface{}
if err := dec.Decode(&doc); err != nil {
if err == io.EOF {
return enc.Close()
}
return err
}
if err := enc.Encode(doc); err != nil {
return err
}
}
}
func yaml2json(input io.Reader, output io.Writer) error {
dec := yaml.NewDecoder(input)
enc := json.NewEncoder(output)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
for {
var doc interface{}
if err := dec.Decode(&doc); err != nil {
if err == io.EOF {
return nil
}
return err
}
if err := enc.Encode(doc); err != nil {
return err
}
}
}
var reverseMode = len(os.Args) != 0 && filepath.Base(os.Args[0]) == "yaml2json"
func init() {
flag.Usage = func() {
if reverseMode {
fmt.Fprintln(flag.CommandLine.Output(), "Usage: yaml2json < input.yaml > output.json\n"+
" or: yaml2json input.yaml > output.json")
} else {
fmt.Fprintln(flag.CommandLine.Output(), "Usage: json2yaml < input.json > output.yaml\n"+
" or: json2yaml input.json > output.yaml")
}
}
}