-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
179 lines (161 loc) · 3.1 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// Version gjo version info
type Version struct {
Program string `json:"program"`
Description string `json:"description"`
Author string `json:"author"`
Repo string `json:"repo"`
Version string `json:"version"`
}
var (
array = flag.Bool("a", false, "creates an array of words")
pretty = flag.Bool("p", false, "pretty-prints")
version = flag.Bool("v", false, "show version")
stdin io.Reader = os.Stdin
stdout io.Writer = os.Stdout
stderr io.Writer = os.Stderr
)
func isRawString(s string) bool {
if strings.HasPrefix(s, "{") && strings.HasSuffix(s, "}") {
return true
}
if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
return true
}
if strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"") {
return true
}
return false
}
func parseValue(s string) interface{} {
if s == "" {
return ""
}
if isRawString(s) {
return json.RawMessage(s)
}
if s == "true" {
return true
}
if s == "false" {
return false
}
if s == "null" {
return nil
}
f, err := strconv.ParseFloat(s, 64)
if err == nil {
return f
}
return s
}
func readFile(fname string) (interface{}, error) {
f, err := os.Open(fname)
if err != nil {
return nil, err
}
var v interface{}
err = json.NewDecoder(f).Decode(&v)
if err != nil {
return nil, err
}
return v, nil
}
func doArray(args []string) (interface{}, error) {
jsons := []interface{}{}
for _, value := range args {
jsons = append(jsons, parseValue(value))
}
return jsons, nil
}
func isKeyFile(s string) bool {
pos := strings.IndexRune(s, ':')
return pos > 0 && pos == len(s)-1
}
func doObject(args []string) (interface{}, error) {
jsons := make(map[string]interface{}, len(args))
for _, arg := range args {
kv := strings.SplitN(arg, "=", 2)
s := ""
if len(kv) > 0 {
s = kv[0]
}
if len(kv) != 2 {
return nil, fmt.Errorf("Argument %q is not k=v", s)
}
if isKeyFile(kv[0]) {
// For argument a:=b, read value from file "b".
v, err := readFile(kv[1])
if err != nil {
return v, err
}
key := kv[0][:len(kv[0])-1]
jsons[key] = v
} else {
jsons[kv[0]] = parseValue(kv[1])
}
}
return jsons, nil
}
func doVersion() error {
enc := json.NewEncoder(stdout)
if *pretty {
enc.SetIndent("", " ")
}
return enc.Encode(&Version{
Program: "gjo",
Description: "This is inspired by jpmens/jo",
Author: "skanehira",
Repo: "https://github.com/skanehira/gjo",
Version: "1.0.3",
})
}
func run() int {
flag.Parse()
if *version {
err := doVersion()
if err != nil {
fmt.Fprintln(stderr, err)
return 1
}
return 0
}
args := flag.Args()
if len(args) == 0 {
flag.Usage()
return 2
}
var value interface{}
var err error
if *array {
value, err = doArray(args)
} else {
value, err = doObject(args)
}
if err != nil {
fmt.Fprintln(stderr, err)
return 1
}
enc := json.NewEncoder(stdout)
if *pretty {
enc.SetIndent("", " ")
}
err = enc.Encode(value)
if err != nil {
fmt.Fprintln(stderr, err)
return 1
}
return 0
}
func main() {
os.Exit(run())
}