Allow for formatted json

This commit is contained in:
tidwall 2018-11-26 16:42:35 -07:00
parent 8b29e98359
commit 06b0d8e09e
1 changed files with 8 additions and 2 deletions

View File

@ -133,6 +133,10 @@ func refusedErrorString(addr string) string {
var groupsM = make(map[string][]string) var groupsM = make(map[string][]string)
func jsonOK(msg []byte) bool {
return gjson.GetBytes(msg, "ok").Bool()
}
func main() { func main() {
if !parseArgs() { if !parseArgs() {
return return
@ -335,13 +339,14 @@ func main() {
output = "resp" output = "resp"
} }
case "output json": case "output json":
if strings.HasPrefix(string(msg), `{"ok":true`) { if jsonOK(msg) {
output = "json" output = "json"
} }
} }
mustOutput := true mustOutput := true
if oneCommand == "" && !strings.HasPrefix(string(msg), `{"ok":true`) {
if oneCommand == "" && !jsonOK(msg) {
var cerr connError var cerr connError
if err := json.Unmarshal(msg, &cerr); err == nil { if err := json.Unmarshal(msg, &cerr); err == nil {
fmt.Fprintln(os.Stderr, "(error) "+cerr.Err) fmt.Fprintln(os.Stderr, "(error) "+cerr.Err)
@ -361,6 +366,7 @@ func main() {
} }
fmt.Fprintln(os.Stdout, string(msg)) fmt.Fprintln(os.Stdout, string(msg))
} else { } else {
msg = bytes.TrimSpace(msg)
if raw { if raw {
fmt.Fprintln(os.Stdout, string(msg)) fmt.Fprintln(os.Stdout, string(msg))
} else { } else {