Documentation ¶
Overview ¶
Package utils provides general purpose utilities for the various other packages. DEVELOPER NOTE: This package should ideally NOT depend on any other internal package
Index ¶
- Variables
- func CheckForExecutables(exeMap map[string]string) error
- func CheckRunningUserNotRoot() error
- func ContextWithOverrideTimeout(ctx context.Context, timeout time.Duration) context.Context
- func ContextWithVerbose(ctx context.Context) context.Context
- func GetArgsFromTemplate(s string) ([]string, error)
- func GetOverrideTimeoutFromContext(ctx context.Context) (time.Duration, error)
- func GetProperTimeoutFromContext(ctx context.Context, defaultDuration string) (time.Duration, error)
- func GetVerboseFromContext(ctx context.Context) (bool, error)
- func IsSliceSubSlice[C comparable](sliceOne []C, sliceTwo []C) bool
- func MergeCmdArgs(fs *pflag.FlagSet, extraArgs []string) ([]string, error)
- func TemplateToCommand(templ *template.Template, cmdArgs any) ([]string, error)
- type TemplateArgsError
- type TemplateExecuteError
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func CheckForExecutables ¶
CheckForExecutables takes a map of executables of the form {"name_of_executable": "whatever"} and checks if each executable is in $PATH. If the location of the executable in $PATH is already present in the map, and can be found on the filesystem it will move to the next executable. If not, it will save the location in the map. If an executable cannot be found, CheckForExecutables returns an error.
func CheckRunningUserNotRoot ¶
func CheckRunningUserNotRoot() error
CheckRunningUserNotRoot checks that the current user running any of the executables is not the system root user
func ContextWithOverrideTimeout ¶
ContextWithOverrideTimeout takes a parent context and returns a new context with an override timeout set
func ContextWithVerbose ¶
ContextWithVerbose wraps a context with a verbose=true value
func GetArgsFromTemplate ¶
GetArgsFromTemplate takes a template string and breaks it into a slice of args
func GetOverrideTimeoutFromContext ¶
GetOverrideTimeoutFromContext returns the override timeout value from a context if it's been stored and type-checks it. If either of these operations fail, it returns an error
func GetProperTimeoutFromContext ¶
func GetProperTimeoutFromContext(ctx context.Context, defaultDuration string) (time.Duration, error)
GetProperTimeoutFromContext takes a context and a default duration, and tries to see if the context is holding an overrideTimeout key. If it finds an overrideTimeout key, it returns the corresponding time.Duration. Otherwise, it will parse the defaultDuration string and return a time.Duration from that, if possible.
func GetVerboseFromContext ¶
GetVerboseFromContext returns the type-checked value of the verbose contextKey from a context if it has been stored. If there was an issue retrieving the value from the verbose contextKey in the context, an error is returned
func IsSliceSubSlice ¶
func IsSliceSubSlice[C comparable](sliceOne []C, sliceTwo []C) bool
IsSliceSubSlice verifies every element within sliceOne is contained within sliceTwo. Ordering does not matter.
func MergeCmdArgs ¶
MergeCmdArgs is meant to take a FlagSet that defines defaults and flags for a particular command, for example ping, and merges the extraArgs with the defaults. So for example:
fs := pflag.NewFlagSet("ping flags", pflag.ContinueOnError) fs.StringP("pingFlagW", "W", "5", "") fs.StringP("pingFlagc", "c", "1", "") mergedArgs := MergeCmdArgs(fs, []string{}) // returns ([]string{"-W, "5", "-c", "1"}, nil) mergedArgs := MergeCmdArgs(fs, []string{"-4"}) // returns ([]string{"-W, "5", "-c", "1", "-4"}, nil) mergedArgs := MergeCmdArgs(fs, []string{"-W", "6"}) // returns ([]string{"-W, "6", "-c", "1"}, nil) mergedArgs := MergeCmdArgs(fs, []string{"-W", "6", "-4"}) // returns ([]string{"-W, "6", "-c", "1", "-4"}, nil)
Notice in this example that dummy longhand names are given for the flags, because the pflag library does not yet support using shorthand flags without longhand flags.
func TemplateToCommand ¶
TemplateToCommand takes a *template.Template and a struct, cmdArgs, and executes the template with those args. Keep in mind that this means that TemplateToCommand expects that the struct cmdArgs's fields should be exported and match up to the fields the template expects. TemplateToCommand returns the finalized template string split into a []string.
Types ¶
type TemplateArgsError ¶
type TemplateArgsError struct {
// contains filtered or unexported fields
}
func (*TemplateArgsError) Error ¶
func (t *TemplateArgsError) Error() string
type TemplateExecuteError ¶
type TemplateExecuteError struct {
// contains filtered or unexported fields
}
func (*TemplateExecuteError) Error ¶
func (t *TemplateExecuteError) Error() string