pluginx

package
v1.6.0-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2023 License: MIT Imports: 20 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecuteCommandWithEnvs

func ExecuteCommandWithEnvs(ctx context.Context, rawCmd string, envs map[string]string) (string, error)

ExecuteCommandWithEnvs is a simple wrapper around exec.CommandContext to simplify running a given command. Deprecated: Use ExecuteCommand(ctx, rawCmd, ExecuteCommandEnvs(envs)) instead.

func MergeExecutorConfigs

func MergeExecutorConfigs(in []*executor.Config, dest any) error

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

func MergeExecutorConfigsWithDefaults(defaults any, in []*executor.Config, dest any) error

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

func MergeSourceConfigs(in []*source.Config, dest any) error

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

func MergeSourceConfigsWithDefaults(defaults any, in []*source.Config, dest any) error

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

func ParseCommand(pluginName, command string, destination any) error

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 PersistKubeConfig(_ context.Context, kc []byte) (string, func(context.Context) error, error)

func ValidateKubeConfigProvided added in v1.0.1

func ValidateKubeConfigProvided(pluginName string, kubeconfig []byte) error

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 ExecuteClearColorCodes added in v1.4.0

func ExecuteClearColorCodes() ExecuteCommandMutation

ExecuteClearColorCodes is a function that enables removing color codes.

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.

func ExecuteCommandStdin added in v1.3.0

func ExecuteCommandStdin(in io.Reader) ExecuteCommandMutation

ExecuteCommandStdin is a function that sets the stdin of the command.

func ExecuteCommandWorkingDir added in v1.3.0

func ExecuteCommandWorkingDir(dir string) ExecuteCommandMutation

ExecuteCommandWorkingDir is a function that sets the working directory of the command.

type ExecuteCommandOptions added in v1.2.0

type ExecuteCommandOptions struct {
	Envs            map[string]string
	DependencyDir   string
	WorkDir         string
	Stdin           io.Reader
	ClearColorCodes bool
}

ExecuteCommandOptions represents the options for executing a command.

type ExecuteCommandOutput added in v1.2.0

type ExecuteCommandOutput struct {
	Stdout   string
	Stderr   string
	ExitCode int
}

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.

func (ExecuteCommandOutput) CombinedOutput added in v1.3.0

func (out ExecuteCommandOutput) CombinedOutput() string

CombinedOutput return combined stdout and stderr.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL