From 7f5ff6b797c558e78cd60775d56b3abe64b49af0 Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Fri, 13 Jun 2025 09:32:35 -0500 Subject: [PATCH] commands: remove debug package in commands The package just causes the entire flow to be more complicated as build has to pretend it doesn't know about debug options and the debugger has to pretend it doesn't know about the build. This abstraction has been difficult when integrating a DAP command into this same workflow so I don't think this abstraction has much of a value. Signed-off-by: Jonathan A. Sternberg --- commands/build.go | 16 +----------- commands/debug.go | 34 +++++++++++++++++++++++++ commands/debug/root.go | 46 ---------------------------------- commands/root.go | 5 +--- docs/reference/buildx_debug.md | 13 +++++----- 5 files changed, 42 insertions(+), 72 deletions(-) create mode 100644 commands/debug.go delete mode 100644 commands/debug/root.go diff --git a/commands/build.go b/commands/build.go index e1b8f86b6..f2b126c3e 100644 --- a/commands/build.go +++ b/commands/build.go @@ -20,7 +20,6 @@ import ( "github.com/containerd/console" "github.com/docker/buildx/build" "github.com/docker/buildx/builder" - "github.com/docker/buildx/commands/debug" "github.com/docker/buildx/monitor" "github.com/docker/buildx/store" "github.com/docker/buildx/store/storeutil" @@ -441,20 +440,7 @@ func runBuildWithOptions(ctx context.Context, dockerCli command.Cli, opts *Build } } -func newDebuggableBuild(dockerCli command.Cli, rootOpts *rootOptions) debug.DebuggableCmd { - return &debuggableBuild{dockerCli: dockerCli, rootOpts: rootOpts} -} - -type debuggableBuild struct { - dockerCli command.Cli - rootOpts *rootOptions -} - -func (b *debuggableBuild) NewDebugger(cfg *debug.DebugConfig) *cobra.Command { - return buildCmd(b.dockerCli, b.rootOpts, cfg) -} - -func buildCmd(dockerCli command.Cli, rootOpts *rootOptions, debugConfig *debug.DebugConfig) *cobra.Command { +func buildCmd(dockerCli command.Cli, rootOpts *rootOptions, debugConfig *debugOptions) *cobra.Command { cFlags := &commonFlags{} options := &buildOptions{} diff --git a/commands/debug.go b/commands/debug.go new file mode 100644 index 000000000..1779b47a9 --- /dev/null +++ b/commands/debug.go @@ -0,0 +1,34 @@ +package commands + +import ( + "github.com/docker/buildx/util/cobrautil" + "github.com/docker/cli/cli/command" + "github.com/spf13/cobra" +) + +type debugOptions struct { + // InvokeFlag is a flag to configure the launched debugger and the commaned executed on the debugger. + InvokeFlag string + + // OnFlag is a flag to configure the timing of launching the debugger. + OnFlag string +} + +func debugCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command { + var options debugOptions + + cmd := &cobra.Command{ + Use: "debug", + Short: "Start debugger", + } + cobrautil.MarkCommandExperimental(cmd) + + flags := cmd.Flags() + flags.StringVar(&options.InvokeFlag, "invoke", "", "Launch a monitor with executing specified command") + flags.StringVar(&options.OnFlag, "on", "error", "When to launch the monitor ([always, error])") + + cobrautil.MarkFlagsExperimental(flags, "invoke", "on") + + cmd.AddCommand(buildCmd(dockerCli, rootOpts, &options)) + return cmd +} diff --git a/commands/debug/root.go b/commands/debug/root.go deleted file mode 100644 index 8ddc0df1d..000000000 --- a/commands/debug/root.go +++ /dev/null @@ -1,46 +0,0 @@ -package debug - -import ( - "github.com/docker/buildx/util/cobrautil" - "github.com/docker/cli/cli/command" - "github.com/spf13/cobra" -) - -// DebugConfig is a user-specified configuration for the debugger. -type DebugConfig struct { - // InvokeFlag is a flag to configure the launched debugger and the commaned executed on the debugger. - InvokeFlag string - - // OnFlag is a flag to configure the timing of launching the debugger. - OnFlag string -} - -// DebuggableCmd is a command that supports debugger with recognizing the user-specified DebugConfig. -type DebuggableCmd interface { - // NewDebugger returns the new *cobra.Command with support for the debugger with recognizing DebugConfig. - NewDebugger(*DebugConfig) *cobra.Command -} - -func RootCmd(dockerCli command.Cli, children ...DebuggableCmd) *cobra.Command { - var progressMode string - var options DebugConfig - - cmd := &cobra.Command{ - Use: "debug", - Short: "Start debugger", - } - cobrautil.MarkCommandExperimental(cmd) - - flags := cmd.Flags() - flags.StringVar(&options.InvokeFlag, "invoke", "", "Launch a monitor with executing specified command") - flags.StringVar(&options.OnFlag, "on", "error", "When to launch the monitor ([always, error])") - flags.StringVar(&progressMode, "progress", "auto", `Set type of progress output ("auto", "plain", "tty", "rawjson") for the monitor. Use plain to show container output`) - - cobrautil.MarkFlagsExperimental(flags, "invoke", "on") - - for _, c := range children { - cmd.AddCommand(c.NewDebugger(&options)) - } - - return cmd -} diff --git a/commands/root.go b/commands/root.go index d42eb44b0..d34d5ed11 100644 --- a/commands/root.go +++ b/commands/root.go @@ -4,7 +4,6 @@ import ( "fmt" "os" - debugcmd "github.com/docker/buildx/commands/debug" historycmd "github.com/docker/buildx/commands/history" imagetoolscmd "github.com/docker/buildx/commands/imagetools" "github.com/docker/buildx/util/cobrautil/completion" @@ -120,9 +119,7 @@ func addCommands(cmd *cobra.Command, opts *rootOptions, dockerCli command.Cli) { historycmd.RootCmd(cmd, dockerCli, historycmd.RootOptions{Builder: &opts.builder}), ) if confutil.IsExperimental() { - cmd.AddCommand(debugcmd.RootCmd(dockerCli, - newDebuggableBuild(dockerCli, opts), - )) + cmd.AddCommand(debugCmd(dockerCli, opts)) } cmd.RegisterFlagCompletionFunc( //nolint:errcheck diff --git a/docs/reference/buildx_debug.md b/docs/reference/buildx_debug.md index afe695674..028be05cd 100644 --- a/docs/reference/buildx_debug.md +++ b/docs/reference/buildx_debug.md @@ -12,13 +12,12 @@ Start debugger (EXPERIMENTAL) ### Options -| Name | Type | Default | Description | -|:----------------|:---------|:--------|:--------------------------------------------------------------------------------------------------------------------| -| `--builder` | `string` | | Override the configured builder instance | -| `-D`, `--debug` | `bool` | | Enable debug logging | -| `--invoke` | `string` | | Launch a monitor with executing specified command (EXPERIMENTAL) | -| `--on` | `string` | `error` | When to launch the monitor ([always, error]) (EXPERIMENTAL) | -| `--progress` | `string` | `auto` | Set type of progress output (`auto`, `plain`, `tty`, `rawjson`) for the monitor. Use plain to show container output | +| Name | Type | Default | Description | +|:----------------|:---------|:--------|:-----------------------------------------------------------------| +| `--builder` | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | +| `--invoke` | `string` | | Launch a monitor with executing specified command (EXPERIMENTAL) | +| `--on` | `string` | `error` | When to launch the monitor ([always, error]) (EXPERIMENTAL) |