From 06b0d8e09e5b66684aff8f7ff4893784c8ddfa3c Mon Sep 17 00:00:00 2001 From: tidwall Date: Mon, 26 Nov 2018 16:42:35 -0700 Subject: [PATCH] Allow for formatted json --- cmd/tile38-cli/main.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/tile38-cli/main.go b/cmd/tile38-cli/main.go index d6daf1da..1d13d141 100644 --- a/cmd/tile38-cli/main.go +++ b/cmd/tile38-cli/main.go @@ -133,6 +133,10 @@ func refusedErrorString(addr string) string { var groupsM = make(map[string][]string) +func jsonOK(msg []byte) bool { + return gjson.GetBytes(msg, "ok").Bool() +} + func main() { if !parseArgs() { return @@ -335,13 +339,14 @@ func main() { output = "resp" } case "output json": - if strings.HasPrefix(string(msg), `{"ok":true`) { + if jsonOK(msg) { output = "json" } } mustOutput := true - if oneCommand == "" && !strings.HasPrefix(string(msg), `{"ok":true`) { + + if oneCommand == "" && !jsonOK(msg) { var cerr connError if err := json.Unmarshal(msg, &cerr); err == nil { fmt.Fprintln(os.Stderr, "(error) "+cerr.Err) @@ -361,6 +366,7 @@ func main() { } fmt.Fprintln(os.Stdout, string(msg)) } else { + msg = bytes.TrimSpace(msg) if raw { fmt.Fprintln(os.Stdout, string(msg)) } else {