{"modulePath":"github.com/DOCKER/cli","version":"v29.10.1+incompatible","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package hooks\n\nPackage hooks defines the contract between the Docker CLI and CLI plugin hook implementations.\n\n### Audience {#hdr-Audience}\n\nThis package is intended to be imported by CLI plugin implementations that implement a \"hooks\" subcommand, and by the Docker CLI when invoking those hooks.\n\n### Contract and wire format {#hdr-Contract_and_wire_format}\n\nHook inputs (see [Request](#Request)) are serialized as JSON and passed to the plugin hook subcommand (currently as a command-line argument). Hook outputs are emitted by the plugin as JSON (see [Response](#Response)).\n\n### Stability {#hdr-Stability}\n\nThe types that represent the hook contract ([Request](#Request), [Response](#Response) and related constants) are considered part of Docker CLI's public Go API. Fields and values may be extended in a backwards-compatible way (for example, adding new fields), but existing fields and their meaning should remain stable. Plugins should ignore unknown fields and unknown hook types to remain forwards-compatible.\n\n## Variables\n\n```go\nvar ErrHookTemplateParse = errors.New(\"failed to parse hook template\")\n```\n\n## Functions\n\n```go\nfunc ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error)\n```\n\n```go\nfunc PrintMessages(out io.Writer, messages []Response, evaluate func(Response) []string)\n```\nPrintMessages groups messages by type and writes them to out. Generic messages appear first, without a header or indentation, followed by next steps under a shared \"What's next:\" header. Order within each type is preserved. Messages with no lines or an unknown type produce no output.\n\n```go\nfunc PrintNextSteps(out io.Writer, messages []string)\n```\nPrintNextSteps renders list of [NextSteps](#NextSteps) messages and writes them to out. It is a no-op if messages is empty.\n\nDeprecated: use [PrintMessages](#PrintMessages) instead.\n\n```go\nfunc TemplateReplaceArg(i int) string\n```\nTemplateReplaceArg takes an index i and returns a hook template string that the CLI will replace the template with the ith argument after processing the passed flags.\n\nExample:\n\n\tResponse{\n\t\tType:     NextSteps,\n\t\tTemplate: \"run this image with `docker run \" + TemplateReplaceArg(0) + \"`\",\n\t}\n\nwhen being executed after the command:\n\n\tdocker pull alpine\n\nIt results in the message:\n\n\tRun this image with `docker run alpine`\n\n```go\nfunc TemplateReplaceFlagValue(flag string) string\n```\nTemplateReplaceFlagValue returns a hook template string that will be replaced with the flags value when printed by the CLI.\n\nExample:\n\n\tResponse{\n\t\tType:     NextSteps,\n\t\tTemplate: \"you ran a container named: \" + TemplateReplaceFlagValue(\"name\"),\n\t}\n\nwhen executed after the command:\n\n\tdocker run --name \"my-container\" alpine\n\nit results in the message:\n\n\tyou ran a container named: my-container\n\n```go\nfunc TemplateReplaceSubcommandName() string\n```\nTemplateReplaceSubcommandName returns a hook template string that will be replaced by the CLI subcommand being executed\n\nExample:\n\n\tResponse{\n\t\tType:     NextSteps,\n\t\tTemplate: \"you ran the subcommand: \" + TemplateReplaceSubcommandName(),\n\t}\n\nWhen being executed after the command:\n\n\tdocker run --name \"my-container\" alpine\n\nIt results in the message:\n\n\tyou ran the subcommand: run\n\n## Types\n\n```go\ntype HookMessage = Response\n```\nHookMessage represents a plugin hook response.\n\nDeprecated: use [Response](#Response) instead.\n\n```go\ntype HookType = ResponseType\n```\nHookType is the type of response from the plugin.\n\nDeprecated: use [ResponseType](#ResponseType) instead.\n\n```go\ntype Request struct {\n\t// RootCmd is a string representing the matching hook configuration\n\t// which is currently being invoked. If a hook for \"docker context\"\n\t// is configured and the user executes \"docker context ls\", the plugin\n\t// is invoked with \"context\".\n\tRootCmd string `json:\"RootCmd,omitzero\"`\n\n\t// Flags contains flags that were set on the command for which the\n\t// hook was invoked. It uses flag names as key, with leading hyphens\n\t// removed (\"--flag\" and \"-flag\" are included as \"flag\" and \"f\").\n\t//\n\t// Flag values are not included and are set to an empty string,\n\t// except for boolean flags known to the CLI itself, for which\n\t// the value is either \"true\", or \"false\".\n\t//\n\t// Plugins can use this information to adjust their [Response]\n\t// based on whether the command triggering the hook was invoked\n\t// with.\n\tFlags map[string]string `json:\"Flags,omitzero\"`\n\n\t// CommandError is a string containing the error output (if any)\n\t// of the command for which the hook was invoked.\n\tCommandError string `json:\"CommandError,omitzero\"`\n}\n```\nRequest is the type representing the information that plugins declaring support for hooks get passed when being invoked following a CLI command execution.\n\n```go\ntype Response struct {\n\tType     ResponseType `json:\"Type\"`\n\tTemplate string       `json:\"Template,omitzero\"`\n}\n```\nResponse represents a plugin hook response. Plugins declaring support for CLI hooks need to print a JSON representation of this type when their hook subcommand is invoked.\n\n```go\ntype ResponseType int\n```\nResponseType is the type of response from the plugin.\n\n```go\nconst (\n\t// NextSteps renders suggestions under a shared \"What's next:\" header.\n\tNextSteps ResponseType = 0\n\t// GenericMessage renders before NextSteps, without a header or indentation.\n\t// CLI versions that do not recognize this type ignore the response.\n\tGenericMessage ResponseType = 1\n)\n```\n\n","path":"github.com/DOCKER/cli/cli-plugins/hooks","name":"hooks","synopsis":"Package hooks defines the contract between the Docker CLI and CLI plugin hook implementations.","isRedistributable":true}
