Skip to content

Commit 79093ee

Browse files
dvdksnclaude
andcommitted
cli: render hints from errors that carry them
Hooks the new internal/hint package into the CLI and plugin error renderers. After printing the error itself, walks the chain for any error implementing hint.Hinter and prints its hint on its own line. Preserves the underlying cause when cobra flag errors are converted to cli.StatusError so hint metadata can still be discovered. t call sites that don't carry a hint. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: David Karlsson <35727626+dvdksn@users.noreply.github.com>
1 parent 1d4c20a commit 79093ee

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

cli-plugins/plugin/plugin.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"io"
89
"os"
910
"sync"
1011

@@ -14,6 +15,7 @@ import (
1415
"github.com/docker/cli/cli/command"
1516
"github.com/docker/cli/cli/connhelper"
1617
"github.com/docker/cli/cli/debug"
18+
"github.com/docker/cli/internal/hint"
1719
"github.com/moby/moby/client"
1820
"github.com/spf13/cobra"
1921
"go.opentelemetry.io/otel"
@@ -104,14 +106,21 @@ func Run(makeCmd func(command.Cli) *cobra.Command, meta metadata.Metadata, ops .
104106
if stErr.StatusCode == 0 { // FIXME(thaJeztah): this should never be used with a zero status-code. Check if we do this anywhere.
105107
stErr.StatusCode = 1
106108
}
107-
_, _ = fmt.Fprintln(dockerCLI.Err(), stErr)
109+
printError(dockerCLI.Err(), stErr)
108110
os.Exit(stErr.StatusCode)
109111
}
110-
_, _ = fmt.Fprintln(dockerCLI.Err(), err)
112+
printError(dockerCLI.Err(), err)
111113
os.Exit(1)
112114
}
113115
}
114116

117+
func printError(out io.Writer, err error) {
118+
_, _ = fmt.Fprintln(out, err)
119+
if h := hint.Of(err); h != "" {
120+
_, _ = fmt.Fprintln(out, "\n"+h)
121+
}
122+
}
123+
115124
func withPluginClientConn(name string) command.CLIOption {
116125
return func(cli *command.DockerCli) error {
117126
cmd := "docker"

cli/cobra.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func FlagErrorFunc(cmd *cobra.Command, err error) error {
8282
}
8383

8484
return StatusError{
85+
Cause: err,
8586
Status: fmt.Sprintf("%s\n\nUsage: %s\n\nRun '%s --help' for more information", err, cmd.UseLine(), cmd.CommandPath()),
8687
StatusCode: 125,
8788
}

cmd/docker/docker.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
cliflags "github.com/docker/cli/cli/flags"
2525
"github.com/docker/cli/cli/version"
2626
platformsignals "github.com/docker/cli/cmd/docker/internal/signals"
27+
"github.com/docker/cli/internal/hint"
2728
"github.com/moby/moby/client/pkg/versions"
2829
"github.com/sirupsen/logrus"
2930
"github.com/spf13/cobra"
@@ -48,6 +49,9 @@ func main() {
4849
if err != nil && !errdefs.IsCanceled(err) {
4950
if err.Error() != "" {
5051
_, _ = fmt.Fprintln(os.Stderr, err)
52+
if h := hint.Of(err); h != "" {
53+
_, _ = fmt.Fprintln(os.Stderr, "\n"+h)
54+
}
5155
}
5256
os.Exit(getExitCode(err))
5357
}

0 commit comments

Comments
 (0)