utils

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var (
	ErrContextKeyFailedTypeCheck error = errors.New("returned value failed type-check")
	ErrContextKeyNotStored             = errors.New("no value in Context for contextKey")
)

Functions

func CheckForExecutables

func CheckForExecutables(exeMap map[string]string) error

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

func ContextWithOverrideTimeout(ctx context.Context, timeout time.Duration) context.Context

ContextWithOverrideTimeout takes a parent context and returns a new context with an override timeout set

func ContextWithVerbose

func ContextWithVerbose(ctx context.Context) context.Context

ContextWithVerbose wraps a context with a verbose=true value

func GetArgsFromTemplate

func GetArgsFromTemplate(s string) ([]string, error)

GetArgsFromTemplate takes a template string and breaks it into a slice of args

func GetOverrideTimeoutFromContext

func GetOverrideTimeoutFromContext(ctx context.Context) (time.Duration, error)

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

func GetVerboseFromContext(ctx context.Context) (bool, error)

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

func MergeCmdArgs(fs *pflag.FlagSet, extraArgs []string) ([]string, error)

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

func TemplateToCommand(templ *template.Template, cmdArgs any) ([]string, error)

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

Jump to

Keyboard shortcuts

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