CLI: Serialisable output formats

The following is a bugbear of mine, the fact that we don’t always honour the format directive the user asked for when running the CLI. I’ve started on a long but slow road to attempt to clean this up, with the addition of a IsSerial method on a CLI context. The IsSerial denotes if the output directive format is serialisable, with the idea that we no longer need to check magic strings. If the users desired output format is json or yaml then checking IsSerial should return true. If Juju gets a new machine format that is serialisable then we hopefully won’t need to check everywhere for the magic string.

Although simple, it’s actually none trivial for one person to fix every location where this occurs. So if you do see a place in the CLI and you have a cmd.Context, then I would be grateful if you check for that instead of the individual names out the formatting output.

Example:

https://github.com/juju/juju/blob/e39409b62f28212cb8c2532e6a095498ca6b1dcb/cmd/juju/cloud/list.go#L193-L203


In the future I hope that we can rely on the IsSerial method to determine how we output to the user, so:

  1. When outputting a result from a command, if there are no results, output the default empty value (for json the example would be {}) rather than an error message.
  2. Refrain from giving an interactive prompt to the user if they request a serialisable format directive.
  3. Attempt (but not always possible) to not log out to stderr if the user requests a serialisable format directive.
  4. Don’t log out to stdout unless it’s in the result type of the format directive.

I’m sure there are other ideals we want to add above, but I think it’s a start.

Let me know if you think I’m right or wrong :wink:

2 Likes