forked from y13i/j2y
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (59 loc) · 1.32 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
package main
import (
"fmt"
"github.com/urfave/cli"
"github.com/y13i/j2y/lib"
"golang.org/x/crypto/ssh/terminal"
"os"
)
func main() {
app := cli.NewApp()
app.Name = "j2y"
app.Usage = "convert JSON to YAML"
app.Version = "0.0.8"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "output, o",
Value: "STDOUT",
Usage: "path of output destination file path",
},
cli.BoolFlag{
Name: "eval, e",
Usage: "treat argument as raw data instead of file path",
},
cli.BoolFlag{
Name: "reverse, r",
Usage: "convert YAML to JSON",
},
cli.BoolFlag{
Name: "minify, m",
Usage: "output JSON in single line format",
},
}
app.Action = func(command *cli.Context) error {
var source string
var outputBytes []byte
if terminal.IsTerminal(0) {
if command.Bool("eval") {
source = "ARGV"
} else {
if command.Args().First() == "" {
fmt.Println("`j2y --help` to view usage.")
os.Exit(1)
}
source = "FILE"
}
} else {
source = "STDIN"
}
inputBytes := j2yLib.GetInputBytes(source, command.Args().First())
if command.Bool("reverse") {
outputBytes = j2yLib.YamlToJson(inputBytes, command.Bool("minify"))
} else {
outputBytes = j2yLib.JsonToYaml(inputBytes)
}
j2yLib.Output(outputBytes, command.String("output"))
return nil
}
app.Run(os.Args)
}