|
| 1 | +// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16: |
| 2 | +//go:build go1.24 |
| 3 | + |
| 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. |
| 27 | +package hooks |
| 28 | + |
| 29 | +// ResponseType is the type of response from the plugin. |
| 30 | +type ResponseType int |
| 31 | + |
| 32 | +const ( |
| 33 | + NextSteps ResponseType = 0 |
| 34 | +) |
| 35 | + |
| 36 | +// Request is the type representing the information |
| 37 | +// that plugins declaring support for hooks get passed when |
| 38 | +// being invoked following a CLI command execution. |
| 39 | +type Request struct { |
| 40 | + // RootCmd is a string representing the matching hook configuration |
| 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"` |
| 62 | +} |
| 63 | + |
| 64 | +// Response represents a plugin hook response. Plugins |
| 65 | +// declaring support for CLI hooks need to print a JSON |
| 66 | +// representation of this type when their hook subcommand |
| 67 | +// is invoked. |
| 68 | +type Response struct { |
| 69 | + Type ResponseType `json:"Type"` |
| 70 | + Template string `json:"Template,omitzero"` |
| 71 | +} |
| 72 | + |
| 73 | +// HookType is the type of response from the plugin. |
| 74 | +// |
| 75 | +// Deprecated: use [ResponseType] instead. |
| 76 | +// |
| 77 | +//go:fix inline |
| 78 | +type HookType = ResponseType |
| 79 | + |
| 80 | +// HookMessage represents a plugin hook response. |
| 81 | +// |
| 82 | +// Deprecated: use [Response] instead. |
| 83 | +// |
| 84 | +//go:fix inline |
| 85 | +type HookMessage = Response |
0 commit comments