cli-plugins: separate hook types from manager and refactor#6859
Open
thaJeztah wants to merge 15 commits intodocker:masterfrom
Open
cli-plugins: separate hook types from manager and refactor#6859thaJeztah wants to merge 15 commits intodocker:masterfrom
thaJeztah wants to merge 15 commits intodocker:masterfrom
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Currently, the error was a plain "exit status 1"; make the error message more informative if we need it :) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Extract the code inside the loop to a closure, so that we can more easily set up debug-logging. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Separate types used by plugins from the manager code. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Rename the type to match the struct it's used for. Also; - Fix the type of the NextSteps const - Don't use iota for values; the ResponseType is used as part of the "wire" format, which means that plugins using the value can use a different version of the module code; using iota increases the risk of (accidentally) changing values, which would break the wire format. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Add labels to define the expected casing and don't serialize empty fields. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
These utilities are used by CLI-plugins; separate them from the render code, which is used by teh CLI-plugin manager. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- add basic unit-test for the template utilities
- make sure the template parsing tests test both the current
template produced by the utilities, as well as a fixture
- rewrite the printer test to use fixtures
- use blackbox testing ("hooks_test")
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- use `%q` instead of manually quoting the string - use `%d` instead of manually converting the number to a string Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Define a local type for methods to expose to the template, instead of passing the cobra.Cmd. This avoids templates depending on features exposed by Cobra that are not part of the contract, and slightly decouples the templat from the Cobra implementation. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
3214407 to
9b06ef5
Compare
This allows for slighly cleaner / more natural placeholders, as it
doesn't require the context (`.`) to be specified;
- `{{command}}` instead of `{{.Command}}` or `{{command .}}`
- `{{flagValue "my-flag"}}` instead of `{{.FlagValue "my-flag"}} or `{{flagValue . "my-flag"}}`
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
9b06ef5 to
a2448d5
Compare
vvoland
reviewed
Mar 18, 2026
Comment on lines
+40
to
+43
| if n := strings.Count(out, "\n"); n > maxMessages { | ||
| return nil, fmt.Errorf("hook template contains too many messages (%d): maximum is %d", n, maxMessages) | ||
| } | ||
| return strings.SplitN(out, "\n", maxMessages), nil |
Collaborator
There was a problem hiding this comment.
10 newlines can be 11 messages
Should we do strings.SplitN(out, "\n", maxMessages+1) and check the length of the resulting slice?
Collaborator
There was a problem hiding this comment.
We should probably have a test for that
vvoland
reviewed
Mar 18, 2026
| @@ -0,0 +1,82 @@ | |||
| // Package hooks defines the contract between the Docker CLI and CLI plugin hook | |||
Collaborator
There was a problem hiding this comment.
Suggested change
| // Package hooks defines the contract between the Docker CLI and CLI plugin hook | |
| //go:build go1.24 | |
| // Package hooks defines the contract between the Docker CLI and CLI plugin hook |
vvoland
reviewed
Mar 18, 2026
Comment on lines
+37
to
+58
| // RootCmd is a string representing the matching hook configuration | ||
| // which is currently being invoked. If a hook for "docker context" | ||
| // is configured and the user executes "docker context ls", the plugin | ||
| // is invoked with "context". | ||
| RootCmd string `json:"rootCmd,omitzero"` | ||
|
|
||
| // Flags contains flags that were set on the command for which the | ||
| // hook was invoked. It uses flag names as key, with leading hyphens | ||
| // removed ("--flag" and "-flag" are included as "flag" and "f"). | ||
| // | ||
| // Flag values are not included and are set to an empty string, | ||
| // except for boolean flags known to the CLI itself, for which | ||
| // the value is either "true", or "false". | ||
| // | ||
| // Plugins can use this information to adjust their [Response] | ||
| // based on whether the command triggering the hook was invoked | ||
| // with. | ||
| Flags map[string]string `json:"flags,omitzero"` | ||
|
|
||
| // CommandError is a string containing the error output (if any) | ||
| // of the command for which the hook was invoked. | ||
| CommandError string `json:"commandError,omitzero"` |
Collaborator
There was a problem hiding this comment.
Previously these were using the default JSON casing so (RootCmd not rootCmd).
Can we break the format here?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
cli-plugins/manager: Plugin.RunHook: improve error message
Currently, the error was a plain "exit status 1"; make the error
message more informative if we need it :)
cli-plugins/manager: simplify ctx-cancel check
cli-plugins/manager: refactor for easier debugging
Extract the code inside the loop to a closure, so that we can more
easily set up debug-logging.
cli-plugins/manager: move HookPluginData to hooks.Request
Separate types used by plugins from the manager code.
cli-plugins/hooks: rename HookMessage to Response
cli-plugins/hooks: rename HookType to ResponseType
Rename the type to match the struct it's used for. Also;
part of the "wire" format, which means that plugins using
the value can use a different version of the module code;
using iota increases the risk of (accidentally) changing
values, which would break the wire format.
cli-plugins/hooks: add JSON labels, omitzero
Add labels to define the expected casing and don't serialize
empty fields.
cli-plugins/hooks: move template utils separate from render code
These utilities are used by CLI-plugins; separate them from the render
code, which is used by teh CLI-plugin manager.
cli-plugins/hooks: update tests
template produced by the utilities, as well as a fixture
cli-plugins/hooks: slight tweaks in templates
%qinstead of manually quoting the string%dinstead of manually converting the number to a stringcli-plugins/hooks: detect if templating is needed
cli-plugins/hooks: limit maximum number of lines / messages
cli-plugins/hooks: update godoc
cli-plugins/hooks: add commandInfo type for templating
Define a local type for methods to expose to the template, instead of
passing the cobra.Cmd. This avoids templates depending on features
exposed by Cobra that are not part of the contract, and slightly
decouples the templat from the Cobra implementation.
cli-plugins/hooks: simplify templating formats
This allows for slighly cleaner / more natural placeholders, as it
doesn't require the context (
.) to be specified;{{command}}instead of{{.Command}}or{{command .}}{{flagValue "my-flag"}}instead of{{.FlagValue "my-flag"}} or{{flagValue . "my-flag"}}`cli-plugins/hooks: PrintNextSteps: slight cleanup
- Human readable description for the release notes
- A picture of a cute animal (not mandatory but encouraged)