mantl / consul-cli

Command line interface to Consul HTTP API
Apache License 2.0
251 stars 67 forks source link

Terminate all JSON output with a newline #24

Closed msabramo closed 8 years ago

msabramo commented 8 years ago

I think it would be nice if the output always ended with a newline. It's weird when it doesn't because the next prompt starts on the same line as the last line of output and that just doesn't look great. Also it's nice to have the output end with a newline because then you can redirect to a file and have that file adhere to POSIX standards - e.g.:

Fixes: GH-23

Before

marca@marca-mac2:~/go/src/github.com/CiscoCloud/consul-cli$ consul-cli catalog services
{
  "aptly": [
    "develop"
  ],
  "consul": [],
  "vault": [
    "develop",
    "vault"
  ]
}marca@marca-mac2:~/go/src/github.com/CiscoCloud/consul-cli$

marca@marca-mac2:~/go/src/github.com/CiscoCloud/consul-cli$ echo "*** BEGIN services ***"; consul-cli catalog services; echo "*** END services ***"
*** BEGIN services ***
{
  "aptly": [
    "develop"
  ],
  "consul": [],
  "vault": [
    "develop",
    "vault"
  ]
}*** END services ***

Note how in the first example, the prompt appears on the same line as the last closing brace and in the second example, the *** END services *** appears immediately after the last closing brace.

After

marca@marca-mac2:~/go/src/github.com/CiscoCloud/consul-cli$ bin/consul-cli catalog services
{
  "aptly": [
    "develop"
  ],
  "consul": [],
  "vault": [
    "develop",
    "vault"
  ]
}
marca@marca-mac2:~/go/src/github.com/CiscoCloud/consul-cli$

marca@marca-mac2:~/go/src/github.com/CiscoCloud/consul-cli$ echo "*** BEGIN services ***"; bin/consul-cli catalog services; echo "*** END services ***"
*** BEGIN services ***
{
  "aptly": [
    "develop"
  ],
  "consul": [],
  "vault": [
    "develop",
    "vault"
  ]
}
*** END services ***
msabramo commented 8 years ago

Just curious. Any feedback on this?

langston-barrett commented 8 years ago

A better way to do this would probably be

jsonStr := string(jsonRaw)
if strings.HasSuffix(jsonStr, "\n") {
    fmt.Fprintf(c.Out, jsonStr)
} else {
    fmt.Fprintf(c.Out, jsonStr + "\n")
}

so that we never add unnecessary newlines.

msabramo commented 8 years ago

@siddharthist: Great idea! I just updated it, as you suggested.