Skip to content

Commit 4a1b2ef

Browse files
committed
cli-plugins/hooks: update godoc
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 4142d40 commit 4a1b2ef

File tree

3 files changed

+81
-23
lines changed

3 files changed

+81
-23
lines changed

cli-plugins/hooks/hook_types.go

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
22
//go:build go1.24
33

4+
// Package hooks defines the contract between the Docker CLI and CLI plugin hook
5+
// implementations.
6+
//
7+
// # Audience
8+
//
9+
// This package is intended to be imported by CLI plugin implementations that
10+
// implement a "hooks" subcommand, and by the Docker CLI when invoking those
11+
// hooks.
12+
//
13+
// # Contract and wire format
14+
//
15+
// Hook inputs (see [Request]) are serialized as JSON and passed to the plugin hook
16+
// subcommand (currently as a command-line argument). Hook outputs are emitted by
17+
// the plugin as JSON (see [Response]).
18+
//
19+
// # Stability
20+
//
21+
// The types that represent the hook contract ([Request], [Response] and related
22+
// constants) are considered part of Docker CLI's public Go API.
23+
// Fields and values may be extended in a backwards-compatible way (for example,
24+
// adding new fields), but existing fields and their meaning should remain stable.
25+
// Plugins should ignore unknown fields and unknown hook types to remain
26+
// forwards-compatible.
427
package hooks
528

629
// ResponseType is the type of response from the plugin.
@@ -15,12 +38,27 @@ const (
1538
// being invoked following a CLI command execution.
1639
type Request struct {
1740
// RootCmd is a string representing the matching hook configuration
18-
// which is currently being invoked. If a hook for `docker context` is
19-
// configured and the user executes `docker context ls`, the plugin will
20-
// be invoked with `context`.
21-
RootCmd string `json:"RootCmd,omitzero"`
22-
Flags map[string]string `json:"Flags,omitzero"`
23-
CommandError string `json:"CommandError,omitzero"`
41+
// which is currently being invoked. If a hook for "docker context"
42+
// is configured and the user executes "docker context ls", the plugin
43+
// is invoked with "context".
44+
RootCmd string `json:"RootCmd,omitzero"`
45+
46+
// Flags contains flags that were set on the command for which the
47+
// hook was invoked. It uses flag names as key, with leading hyphens
48+
// removed ("--flag" and "-flag" are included as "flag" and "f").
49+
//
50+
// Flag values are not included and are set to an empty string,
51+
// except for boolean flags known to the CLI itself, for which
52+
// the value is either "true", or "false".
53+
//
54+
// Plugins can use this information to adjust their [Response]
55+
// based on whether the command triggering the hook was invoked
56+
// with.
57+
Flags map[string]string `json:"Flags,omitzero"`
58+
59+
// CommandError is a string containing the error output (if any)
60+
// of the command for which the hook was invoked.
61+
CommandError string `json:"CommandError,omitzero"`
2462
}
2563

2664
// Response represents a plugin hook response. Plugins

cli-plugins/hooks/hook_utils.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,61 @@ const (
1515
//
1616
// Example:
1717
//
18-
// "you ran the subcommand: " + TemplateReplaceSubcommandName()
18+
// Response{
19+
// Type: NextSteps,
20+
// Template: "you ran the subcommand: " + TemplateReplaceSubcommandName(),
21+
// }
1922
//
20-
// when being executed after the command:
21-
// `docker run --name "my-container" alpine`
22-
// will result in the message:
23-
// `you ran the subcommand: run`
23+
// When being executed after the command:
24+
//
25+
// docker run --name "my-container" alpine
26+
//
27+
// It results in the message:
28+
//
29+
// you ran the subcommand: run
2430
func TemplateReplaceSubcommandName() string {
2531
return hookTemplateCommandName
2632
}
2733

28-
// TemplateReplaceFlagValue returns a hook template string
29-
// that will be replaced by the flags value.
34+
// TemplateReplaceFlagValue returns a hook template string that will be
35+
// replaced with the flags value when printed by the CLI.
3036
//
3137
// Example:
3238
//
33-
// "you ran a container named: " + TemplateReplaceFlagValue("name")
39+
// Response{
40+
// Type: NextSteps,
41+
// Template: "you ran a container named: " + TemplateReplaceFlagValue("name"),
42+
// }
3443
//
35-
// when being executed after the command:
36-
// `docker run --name "my-container" alpine`
37-
// will result in the message:
38-
// `you ran a container named: my-container`
44+
// when executed after the command:
45+
//
46+
// docker run --name "my-container" alpine
47+
//
48+
// it results in the message:
49+
//
50+
// you ran a container named: my-container
3951
func TemplateReplaceFlagValue(flag string) string {
4052
return fmt.Sprintf(hookTemplateFlagValue, flag)
4153
}
4254

4355
// TemplateReplaceArg takes an index i and returns a hook
4456
// template string that the CLI will replace the template with
45-
// the ith argument, after processing the passed flags.
57+
// the ith argument after processing the passed flags.
4658
//
4759
// Example:
4860
//
49-
// "run this image with `docker run " + TemplateReplaceArg(0) + "`"
61+
// Response{
62+
// Type: NextSteps,
63+
// Template: "run this image with `docker run " + TemplateReplaceArg(0) + "`",
64+
// }
5065
//
5166
// when being executed after the command:
52-
// `docker pull alpine`
53-
// will result in the message:
54-
// "Run this image with `docker run alpine`"
67+
//
68+
// docker pull alpine
69+
//
70+
// It results in the message:
71+
//
72+
// Run this image with `docker run alpine`
5573
func TemplateReplaceArg(i int) string {
5674
return fmt.Sprintf(hookTemplateArg, i)
5775
}

cli-plugins/hooks/printer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/morikuni/aec"
88
)
99

10+
// PrintNextSteps renders list of [NextSteps] messages and writes them
11+
// to out. It is a no-op if messages is empty.
1012
func PrintNextSteps(out io.Writer, messages []string) {
1113
if len(messages) == 0 {
1214
return

0 commit comments

Comments
 (0)