Documentation
¶
Overview ¶
Package pluginx provides helper functions to simplify working it Botkube plugins. The 'x' in the package name stand for 'extension'. It is added to reduce possible conflicts with core plugin package named 'plugin'.
Example (ExecuteCommand) ¶
out, err := ExecuteCommand(context.Background(), `echo "hakuna matata"`)
if err != nil {
panic(err)
}
fmt.Println(out.Stdout)
Output: hakuna matata
Example (ExecuteCommandWithEnv) ¶
out, err := ExecuteCommand(context.Background(), `sh -c "echo ${CUSTOM_ENV}"`, ExecuteCommandEnvs(map[string]string{
"CUSTOM_ENV": "magic-value",
}))
if err != nil {
panic(err)
}
fmt.Println(out.Stdout)
Output: magic-value
Example (ParseCommand) ¶
type (
CommitCmd struct {
All bool `arg:"-a"`
Message string `arg:"-m"`
}
Commands struct {
Commit *CommitCmd `arg:"subcommand:commit"`
Quiet bool `arg:"-q"` // this flag is global to all subcommands
}
)
rawCommand := "./example commit -m 'hakuna matata' -a -q"
var cmd Commands
err := ParseCommand("./example", rawCommand, &cmd)
if err != nil {
panic(err)
}
switch {
case cmd.Commit != nil:
fmt.Println(heredoc.Docf(`
global quiet flag: %v
commit message: %v
commit all flag: %v
`, cmd.Commit.All, cmd.Commit.Message, cmd.Quiet))
}
Output: global quiet flag: true commit message: hakuna matata commit all flag: true
Index ¶
- func ExecuteCommandWithEnvs(ctx context.Context, rawCmd string, envs map[string]string) (string, error)deprecated
- func MergeExecutorConfigs(in []*executor.Config, dest any) error
- func MergeExecutorConfigsWithDefaults(defaults any, in []*executor.Config, dest any) error
- func MergeSourceConfigs(in []*source.Config, dest any) error
- func MergeSourceConfigsWithDefaults(defaults any, in []*source.Config, dest any) error
- func ParseCommand(pluginName, command string, destination any) error
- func PersistKubeConfig(_ context.Context, kc []byte) (string, func(context.Context) error, error)
- func ValidateKubeConfigProvided(pluginName string, kubeconfig []byte) error
- type ExecuteCommandMutation
- type ExecuteCommandOptions
- type ExecuteCommandOutput
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExecuteCommandWithEnvs
deprecated
func MergeExecutorConfigs ¶
MergeExecutorConfigs merges input configuration into a given destination. Rules: - Destination MUST be a pointer to a struct. - if `yaml:"omitempty"` tag is not specified, then empty fields are take into account, and resets previous value. - Merging strategy can be found here https://github.com/knadh/koanf#merge-behavior.
func MergeExecutorConfigsWithDefaults ¶
MergeExecutorConfigsWithDefaults merges input configuration into a given destination. Rules: - Destination MUST be a pointer to a struct. - Default MUST be a Go object with the `yaml` tag. - if `yaml:"omitempty"` tag is not specified, then empty fields are take into account, and resets previous value. - Merging strategy can be found here https://github.com/knadh/koanf#merge-behavior.
func MergeSourceConfigs ¶
MergeSourceConfigs merges input configuration into a given destination. Rules: - Destination MUST be a pointer to a struct. - if `yaml:"omitempty"` tag is not specified, then empty fields are take into account, and resets previous value. - Merging strategy can be found here https://github.com/knadh/koanf#merge-behavior.
func MergeSourceConfigsWithDefaults ¶
MergeSourceConfigsWithDefaults merges input configuration into a given destination. Rules: - Destination MUST be a pointer to a struct. - Default MUST be a Go object with the `yaml` tag. - if `yaml:"omitempty"` tag is not specified, then empty fields are take into account, and resets previous value. - Merging strategy can be found here https://github.com/knadh/koanf#merge-behavior.
func ParseCommand ¶
ParseCommand processes a given command string and stores the result in a given destination. Destination MUST be a pointer to a struct.
If `-h,--help` flag was specified, arg.ErrHelp is returned and command might be not fully parsed.
To support flags, positional arguments, and subcommands add dedicated `arg:` tag. To learn more, visit github.com/alexflint/go-arg
func PersistKubeConfig ¶ added in v1.0.0
func ValidateKubeConfigProvided ¶ added in v1.0.1
ValidateKubeConfigProvided returns an error if a given kubeconfig is empty or nil.
Types ¶
type ExecuteCommandMutation ¶ added in v1.2.0
type ExecuteCommandMutation func(*ExecuteCommandOptions)
ExecuteCommandMutation is a function type that can be used to modify ExecuteCommandOptions.
func ExecuteCommandDependencyDir ¶ added in v1.2.0
func ExecuteCommandDependencyDir(dir string) ExecuteCommandMutation
ExecuteCommandDependencyDir is a function that sets the dependency directory.
func ExecuteCommandEnvs ¶ added in v1.2.0
func ExecuteCommandEnvs(envs map[string]string) ExecuteCommandMutation
ExecuteCommandEnvs is a function that sets the environment variables.
type ExecuteCommandOptions ¶ added in v1.2.0
ExecuteCommandOptions represents the options for executing a command.
type ExecuteCommandOutput ¶ added in v1.2.0
ExecuteCommandOutput holds ExecuteCommand output.
func ExecuteCommand ¶
func ExecuteCommand(ctx context.Context, rawCmd string, mutators ...ExecuteCommandMutation) (ExecuteCommandOutput, error)
ExecuteCommand is a simple wrapper around exec.CommandContext to simplify running a given command.