From 7a7a9c8e01e14a83f732a0509dd9128d271d7d2d Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Fri, 16 Aug 2024 13:20:28 +0300 Subject: [PATCH] commands: add debug as persistent flag Allows using `--debug` to enable debug logging under any subcommand. Currently it needed to be set as `docker --debug buildx` meaning only way to enable debug in standalone mode was to set env variable instead and updating existing commands to add `--debug` was cumbersome. Signed-off-by: Tonis Tiigi --- commands/root.go | 17 +++++++++-------- docs/reference/buildx.md | 1 + docs/reference/buildx_bake.md | 1 + docs/reference/buildx_build.md | 1 + docs/reference/buildx_create.md | 1 + docs/reference/buildx_debug.md | 1 + docs/reference/buildx_debug_build.md | 1 + docs/reference/buildx_dial-stdio.md | 11 ++++++----- docs/reference/buildx_du.md | 1 + docs/reference/buildx_imagetools.md | 1 + docs/reference/buildx_imagetools_create.md | 1 + docs/reference/buildx_imagetools_inspect.md | 1 + docs/reference/buildx_inspect.md | 1 + docs/reference/buildx_ls.md | 7 ++++--- docs/reference/buildx_prune.md | 1 + docs/reference/buildx_rm.md | 1 + docs/reference/buildx_stop.md | 1 + docs/reference/buildx_use.md | 1 + docs/reference/buildx_version.md | 6 ++++++ 19 files changed, 40 insertions(+), 16 deletions(-) diff --git a/commands/root.go b/commands/root.go index 8c0d8627427..360b40ab884 100644 --- a/commands/root.go +++ b/commands/root.go @@ -21,6 +21,7 @@ import ( ) func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Command { + var opt rootOptions cmd := &cobra.Command{ Short: "Docker Buildx", Long: `Extended build capabilities with BuildKit`, @@ -32,6 +33,10 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman HiddenDefaultCmd: true, }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if opt.debug { + debug.Enable() + } + cmd.SetContext(appcontext.Context()) if !isPlugin { return nil @@ -47,11 +52,6 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman cmd.TraverseChildren = true cmd.DisableFlagsInUseLine = true cli.DisableFlagsInUseLine(cmd) - - // DEBUG=1 should perform the same as --debug at the docker root level - if debug.IsEnabled() { - debug.Enable() - } } logrus.SetFormatter(&logutil.Formatter{}) @@ -68,16 +68,16 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman cmd.SetHelpTemplate(cmd.HelpTemplate() + "\nExperimental commands and flags are hidden. Set BUILDX_EXPERIMENTAL=1 to show them.\n") } - addCommands(cmd, dockerCli) + addCommands(cmd, &opt, dockerCli) return cmd } type rootOptions struct { builder string + debug bool } -func addCommands(cmd *cobra.Command, dockerCli command.Cli) { - opts := &rootOptions{} +func addCommands(cmd *cobra.Command, opts *rootOptions, dockerCli command.Cli) { rootFlags(opts, cmd.PersistentFlags()) cmd.AddCommand( @@ -112,4 +112,5 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) { func rootFlags(options *rootOptions, flags *pflag.FlagSet) { flags.StringVar(&options.builder, "builder", os.Getenv("BUILDX_BUILDER"), "Override the configured builder instance") + flags.BoolVarP(&options.debug, "debug", "D", debug.IsEnabled(), "Enable debug logging") } diff --git a/docs/reference/buildx.md b/docs/reference/buildx.md index 2dd0b74177a..783a136ec4a 100644 --- a/docs/reference/buildx.md +++ b/docs/reference/buildx.md @@ -32,6 +32,7 @@ Extended build capabilities with BuildKit | Name | Type | Default | Description | |:------------------------|:---------|:--------|:-----------------------------------------| | [`--builder`](#builder) | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | diff --git a/docs/reference/buildx_bake.md b/docs/reference/buildx_bake.md index 2d21b5d3fb2..61b57ae5493 100644 --- a/docs/reference/buildx_bake.md +++ b/docs/reference/buildx_bake.md @@ -18,6 +18,7 @@ Build from a file | [`--builder`](#builder) | `string` | | Override the configured builder instance | | [`--call`](#call) | `string` | `build` | Set method for evaluating build (`check`, `outline`, `targets`) | | [`--check`](#check) | `bool` | | Shorthand for `--call=check` | +| `-D`, `--debug` | `bool` | | Enable debug logging | | [`-f`](#file), [`--file`](#file) | `stringArray` | | Build definition file | | `--load` | `bool` | | Shorthand for `--set=*.output=type=docker` | | [`--metadata-file`](#metadata-file) | `string` | | Write build result metadata to a file | diff --git a/docs/reference/buildx_build.md b/docs/reference/buildx_build.md index f0c98e4b7cd..9fe9b2d2cd6 100644 --- a/docs/reference/buildx_build.md +++ b/docs/reference/buildx_build.md @@ -27,6 +27,7 @@ Start a build | [`--call`](#call) | `string` | `build` | Set method for evaluating build (`check`, `outline`, `targets`) | | [`--cgroup-parent`](#cgroup-parent) | `string` | | Set the parent cgroup for the `RUN` instructions during build | | [`--check`](#check) | `bool` | | Shorthand for `--call=check` | +| `-D`, `--debug` | `bool` | | Enable debug logging | | `--detach` | `bool` | | Detach buildx server (supported only on linux) (EXPERIMENTAL) | | [`-f`](#file), [`--file`](#file) | `string` | | Name of the Dockerfile (default: `PATH/Dockerfile`) | | `--iidfile` | `string` | | Write the image ID to a file | diff --git a/docs/reference/buildx_create.md b/docs/reference/buildx_create.md index 554e2db7915..76d1776052d 100644 --- a/docs/reference/buildx_create.md +++ b/docs/reference/buildx_create.md @@ -15,6 +15,7 @@ Create a new builder instance | `--bootstrap` | `bool` | | Boot builder after creation | | [`--buildkitd-config`](#buildkitd-config) | `string` | | BuildKit daemon config file | | [`--buildkitd-flags`](#buildkitd-flags) | `string` | | BuildKit daemon flags | +| `-D`, `--debug` | `bool` | | Enable debug logging | | [`--driver`](#driver) | `string` | | Driver to use (available: `docker-container`, `kubernetes`, `remote`) | | [`--driver-opt`](#driver-opt) | `stringArray` | | Options for the driver | | [`--leave`](#leave) | `bool` | | Remove a node from builder instead of changing it | diff --git a/docs/reference/buildx_debug.md b/docs/reference/buildx_debug.md index a07385c9f8a..6f72b5ccec1 100644 --- a/docs/reference/buildx_debug.md +++ b/docs/reference/buildx_debug.md @@ -15,6 +15,7 @@ Start debugger (EXPERIMENTAL) | Name | Type | Default | Description | |:------------------|:---------|:--------|:--------------------------------------------------------------------------------------------------------------------| | `--builder` | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | | `--detach` | `bool` | `true` | Detach buildx server for the monitor (supported only on linux) (EXPERIMENTAL) | | `--invoke` | `string` | | Launch a monitor with executing specified command (EXPERIMENTAL) | | `--on` | `string` | `error` | When to launch the monitor ([always, error]) (EXPERIMENTAL) | diff --git a/docs/reference/buildx_debug_build.md b/docs/reference/buildx_debug_build.md index a7dd373a174..be0499c2d23 100644 --- a/docs/reference/buildx_debug_build.md +++ b/docs/reference/buildx_debug_build.md @@ -23,6 +23,7 @@ Start a build | `--call` | `string` | `build` | Set method for evaluating build (`check`, `outline`, `targets`) | | `--cgroup-parent` | `string` | | Set the parent cgroup for the `RUN` instructions during build | | `--check` | `bool` | | Shorthand for `--call=check` | +| `-D`, `--debug` | `bool` | | Enable debug logging | | `--detach` | `bool` | | Detach buildx server (supported only on linux) (EXPERIMENTAL) | | `-f`, `--file` | `string` | | Name of the Dockerfile (default: `PATH/Dockerfile`) | | `--iidfile` | `string` | | Write the image ID to a file | diff --git a/docs/reference/buildx_dial-stdio.md b/docs/reference/buildx_dial-stdio.md index d78f8b309f1..7fdc84ec0fe 100644 --- a/docs/reference/buildx_dial-stdio.md +++ b/docs/reference/buildx_dial-stdio.md @@ -5,11 +5,12 @@ Proxy current stdio streams to builder instance ### Options -| Name | Type | Default | Description | -|:-------------|:---------|:--------|:----------------------------------------------------------------------------------------------------| -| `--builder` | `string` | | Override the configured builder instance | -| `--platform` | `string` | | Target platform: this is used for node selection | -| `--progress` | `string` | `quiet` | Set type of progress output (`auto`, `plain`, `tty`, `rawjson`). Use plain to show container output | +| Name | Type | Default | Description | +|:----------------|:---------|:--------|:----------------------------------------------------------------------------------------------------| +| `--builder` | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | +| `--platform` | `string` | | Target platform: this is used for node selection | +| `--progress` | `string` | `quiet` | Set type of progress output (`auto`, `plain`, `tty`, `rawjson`). Use plain to show container output | diff --git a/docs/reference/buildx_du.md b/docs/reference/buildx_du.md index 3ac8d9504d5..684f717a2e0 100644 --- a/docs/reference/buildx_du.md +++ b/docs/reference/buildx_du.md @@ -12,6 +12,7 @@ Disk usage | Name | Type | Default | Description | |:------------------------|:---------|:--------|:-----------------------------------------| | [`--builder`](#builder) | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | | `--filter` | `filter` | | Provide filter values | | [`--verbose`](#verbose) | `bool` | | Provide a more verbose output | diff --git a/docs/reference/buildx_imagetools.md b/docs/reference/buildx_imagetools.md index 6e4348b5c2a..42e191ddf21 100644 --- a/docs/reference/buildx_imagetools.md +++ b/docs/reference/buildx_imagetools.md @@ -20,6 +20,7 @@ Commands to work on images in registry | Name | Type | Default | Description | |:------------------------|:---------|:--------|:-----------------------------------------| | [`--builder`](#builder) | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | diff --git a/docs/reference/buildx_imagetools_create.md b/docs/reference/buildx_imagetools_create.md index 7fb1159b0c5..fffff68276f 100644 --- a/docs/reference/buildx_imagetools_create.md +++ b/docs/reference/buildx_imagetools_create.md @@ -14,6 +14,7 @@ Create a new image based on source images | [`--annotation`](#annotation) | `stringArray` | | Add annotation to the image | | [`--append`](#append) | `bool` | | Append to existing manifest | | [`--builder`](#builder) | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | | [`--dry-run`](#dry-run) | `bool` | | Show final image instead of pushing | | [`-f`](#file), [`--file`](#file) | `stringArray` | | Read source descriptor from file | | `--prefer-index` | `bool` | `true` | When only a single source is specified, prefer outputting an image index or manifest list instead of performing a carbon copy | diff --git a/docs/reference/buildx_imagetools_inspect.md b/docs/reference/buildx_imagetools_inspect.md index 69a396b60bf..ed98215b143 100644 --- a/docs/reference/buildx_imagetools_inspect.md +++ b/docs/reference/buildx_imagetools_inspect.md @@ -12,6 +12,7 @@ Show details of an image in the registry | Name | Type | Default | Description | |:------------------------|:---------|:----------------|:----------------------------------------------| | [`--builder`](#builder) | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | | [`--format`](#format) | `string` | `{{.Manifest}}` | Format the output using the given Go template | | [`--raw`](#raw) | `bool` | | Show original, unformatted JSON manifest | diff --git a/docs/reference/buildx_inspect.md b/docs/reference/buildx_inspect.md index 066b9881b36..8cd9481377b 100644 --- a/docs/reference/buildx_inspect.md +++ b/docs/reference/buildx_inspect.md @@ -13,6 +13,7 @@ Inspect current builder instance |:----------------------------|:---------|:--------|:--------------------------------------------| | [`--bootstrap`](#bootstrap) | `bool` | | Ensure builder has booted before inspecting | | [`--builder`](#builder) | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | diff --git a/docs/reference/buildx_ls.md b/docs/reference/buildx_ls.md index 988d1144930..dba478c3063 100644 --- a/docs/reference/buildx_ls.md +++ b/docs/reference/buildx_ls.md @@ -9,9 +9,10 @@ List builder instances ### Options -| Name | Type | Default | Description | -|:----------------------|:---------|:--------|:------------------| -| [`--format`](#format) | `string` | `table` | Format the output | +| Name | Type | Default | Description | +|:----------------------|:---------|:--------|:---------------------| +| `-D`, `--debug` | `bool` | | Enable debug logging | +| [`--format`](#format) | `string` | `table` | Format the output | diff --git a/docs/reference/buildx_prune.md b/docs/reference/buildx_prune.md index df739831ec3..f8d4537567c 100644 --- a/docs/reference/buildx_prune.md +++ b/docs/reference/buildx_prune.md @@ -13,6 +13,7 @@ Remove build cache |:------------------------|:---------|:--------|:------------------------------------------| | `-a`, `--all` | `bool` | | Include internal/frontend images | | [`--builder`](#builder) | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | | `--filter` | `filter` | | Provide filter values (e.g., `until=24h`) | | `-f`, `--force` | `bool` | | Do not prompt for confirmation | | `--keep-storage` | `bytes` | `0` | Amount of disk space to keep for cache | diff --git a/docs/reference/buildx_rm.md b/docs/reference/buildx_rm.md index a9e7cca2991..bcb856e7bdf 100644 --- a/docs/reference/buildx_rm.md +++ b/docs/reference/buildx_rm.md @@ -13,6 +13,7 @@ Remove one or more builder instances |:------------------------------------|:---------|:--------|:-----------------------------------------| | [`--all-inactive`](#all-inactive) | `bool` | | Remove all inactive builders | | [`--builder`](#builder) | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | | [`-f`](#force), [`--force`](#force) | `bool` | | Do not prompt for confirmation | | [`--keep-daemon`](#keep-daemon) | `bool` | | Keep the BuildKit daemon running | | [`--keep-state`](#keep-state) | `bool` | | Keep BuildKit state | diff --git a/docs/reference/buildx_stop.md b/docs/reference/buildx_stop.md index 19aa8fbb952..dcf0430a9f3 100644 --- a/docs/reference/buildx_stop.md +++ b/docs/reference/buildx_stop.md @@ -12,6 +12,7 @@ Stop builder instance | Name | Type | Default | Description | |:------------------------|:---------|:--------|:-----------------------------------------| | [`--builder`](#builder) | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | diff --git a/docs/reference/buildx_use.md b/docs/reference/buildx_use.md index c52cde32a7a..6cd1652f704 100644 --- a/docs/reference/buildx_use.md +++ b/docs/reference/buildx_use.md @@ -12,6 +12,7 @@ Set the current builder instance | Name | Type | Default | Description | |:------------------------|:---------|:--------|:-------------------------------------------| | [`--builder`](#builder) | `string` | | Override the configured builder instance | +| `-D`, `--debug` | `bool` | | Enable debug logging | | `--default` | `bool` | | Set builder as default for current context | | `--global` | `bool` | | Builder persists context changes | diff --git a/docs/reference/buildx_version.md b/docs/reference/buildx_version.md index 83dde2c8ca9..e186812f12a 100644 --- a/docs/reference/buildx_version.md +++ b/docs/reference/buildx_version.md @@ -7,6 +7,12 @@ docker buildx version Show buildx version information +### Options + +| Name | Type | Default | Description | +|:----------------|:-------|:--------|:---------------------| +| `-D`, `--debug` | `bool` | | Enable debug logging | +