mirror of
https://github.com/docker/cli.git
synced 2026-08-28 02:24:15 -05:00
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>
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package hooks
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
const (
|
|
hookTemplateCommandName = `{{command}}`
|
|
hookTemplateFlagValue = `{{flagValue %q}}`
|
|
hookTemplateArg = `{{argValue %d}}`
|
|
)
|
|
|
|
// TemplateReplaceSubcommandName returns a hook template string
|
|
// that will be replaced by the CLI subcommand being executed
|
|
//
|
|
// Example:
|
|
//
|
|
// Response{
|
|
// Type: NextSteps,
|
|
// Template: "you ran the subcommand: " + TemplateReplaceSubcommandName(),
|
|
// }
|
|
//
|
|
// When being executed after the command:
|
|
//
|
|
// docker run --name "my-container" alpine
|
|
//
|
|
// It results in the message:
|
|
//
|
|
// you ran the subcommand: run
|
|
func TemplateReplaceSubcommandName() string {
|
|
return hookTemplateCommandName
|
|
}
|
|
|
|
// TemplateReplaceFlagValue returns a hook template string that will be
|
|
// replaced with the flags value when printed by the CLI.
|
|
//
|
|
// Example:
|
|
//
|
|
// Response{
|
|
// Type: NextSteps,
|
|
// Template: "you ran a container named: " + TemplateReplaceFlagValue("name"),
|
|
// }
|
|
//
|
|
// when executed after the command:
|
|
//
|
|
// docker run --name "my-container" alpine
|
|
//
|
|
// it results in the message:
|
|
//
|
|
// you ran a container named: my-container
|
|
func TemplateReplaceFlagValue(flag string) string {
|
|
return fmt.Sprintf(hookTemplateFlagValue, flag)
|
|
}
|
|
|
|
// TemplateReplaceArg 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.
|
|
//
|
|
// Example:
|
|
//
|
|
// Response{
|
|
// Type: NextSteps,
|
|
// Template: "run this image with `docker run " + TemplateReplaceArg(0) + "`",
|
|
// }
|
|
//
|
|
// when being executed after the command:
|
|
//
|
|
// docker pull alpine
|
|
//
|
|
// It results in the message:
|
|
//
|
|
// Run this image with `docker run alpine`
|
|
func TemplateReplaceArg(i int) string {
|
|
return fmt.Sprintf(hookTemplateArg, i)
|
|
}
|