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 ¶
- func CheckForExecutables(exeMap map[string]string) error
- func CheckRunningUserNotRoot() error
- func GetArgsFromTemplate(s string) ([]string, 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 ¶
This section is empty.
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 GetArgsFromTemplate ¶
GetArgsFromTemplate takes a template string and breaks it into a slice of args
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