DockerCLI/cli/command/network
Sebastiaan van Stijn bca2090061
cli: make cli.StatusError slightly prettier
This error didn't do a great job at formatting. If a StatusError was
produced without a Status message, it would print a very non-informative
error, with information missing.

Let's update the output:

- If a status-message is provided; print just that (after all the
  status code is something that can be found from the shell, e.g.
  through `echo $?` in Bash).
- If no status-message is provided: print a message more similar to
  Go's `exec.ExecError`, which uses `os.rocessState.String()` (see [1]).

Before this patch, an error without custom status would print:

    Status: , Code: 2

After this patch:

    exit status 2

In situations where a custom error-message is provided, the error-message
is print as-is, whereas before this patch, the message got combined with
the `Status:` and `Code:`, which resulted in some odd output.

Before this patch:

    docker volume --no-such-flag
    Status: unknown flag: --no-such-flag
    See 'docker volume --help'.

    Usage:  docker volume COMMAND

    Manage volumes

    Commands:
      create      Create a volume
      inspect     Display detailed information on one or more volumes
      ls          List volumes
      prune       Remove unused local volumes
      rm          Remove one or more volumes
      update      Update a volume (cluster volumes only)

    Run 'docker volume COMMAND --help' for more information on a command.
    , Code: 125

With this patch, the error is shown as-is;

    docker volume --no-such-flag
    unknown flag: --no-such-flag
    See 'docker volume --help'.

    Usage:  docker volume COMMAND

    Manage volumes

    Commands:
      create      Create a volume
      inspect     Display detailed information on one or more volumes
      ls          List volumes
      prune       Remove unused local volumes
      rm          Remove one or more volumes
      update      Update a volume (cluster volumes only)

    Run 'docker volume COMMAND --help' for more information on a command.

While the exit-code is no longer printed, it's still properly handled;

    echo $?
    125

[1]: 82c14346d8/src/os/exec_posix.go (L107-L135)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-07-04 22:08:18 +02:00
..
testdata formatter: reduce minimum width for columns in table-view 2020-08-31 16:31:15 +02:00
client_test.go vendor: github.com/docker/docker 59996a493cfc (v27.0.0-dev) 2024-06-07 16:11:03 +02:00
cmd.go Adopt Cobra completion v2 to support completion by CLI plugins 2022-05-12 12:59:10 +02:00
connect.go cli/command/network: some cleanup and pass smaller interfaces 2024-07-03 17:40:35 +02:00
connect_test.go test spring-cleaning 2024-07-04 01:35:12 +02:00
create.go cli/command/network: rewrite consolidateIpam to take an option-struct 2024-07-04 01:39:27 +02:00
create_test.go test spring-cleaning 2024-07-04 01:35:12 +02:00
disconnect.go replace uses of deprecated API types 2024-07-04 15:22:18 +02:00
disconnect_test.go test spring-cleaning 2024-07-04 01:35:12 +02:00
formatter.go vendor: github.com/docker/docker c6aaabc9fc82 (master / v27.0.0-dev) 2024-06-05 16:29:55 +02:00
formatter_test.go Update go:build comments to go1.21 2024-06-18 12:17:13 +02:00
inspect.go cli/command/network: some cleanup and pass smaller interfaces 2024-07-03 17:40:35 +02:00
list.go vendor: github.com/docker/docker cd3804655a25 (master / v27.0.0-dev) 2024-06-04 09:17:20 +02:00
list_test.go test spring-cleaning 2024-07-04 01:35:12 +02:00
prune.go cli/command/network: NewPruneCommand: explicitly ignore error 2024-07-04 01:39:28 +02:00
prune_test.go test spring-cleaning 2024-07-04 01:35:12 +02:00
remove.go cli/command/network: some cleanup and pass smaller interfaces 2024-07-03 17:40:35 +02:00
remove_test.go cli: make cli.StatusError slightly prettier 2024-07-04 22:08:18 +02:00