util

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: May 14, 2020 License: MIT Imports: 11 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirExists added in v0.0.2

func DirExists(filename string) bool

DirExists checks whether path is an existing dir

func Ensure

func Ensure(err error, message string)

Ensure checks for an error. If there is one it panics, if no error it prints the message (given it is not empty) `message` should describe what is ensured by the call and verification of `err` being `nil`

func FileExists added in v0.0.2

func FileExists(filename string) bool

FileExists checks if a file exists and is not a directory before we try using it to prevent further errors.

func Filter added in v0.0.2

func Filter(selector *regexp.Regexp, slice []string) (out []string, numRemoved int)

Filter removes all matching elements from a string slice based on a regexp selector

func FilterSplit added in v0.0.2

func FilterSplit(selector *regexp.Regexp, slice []string) (out, removed []string)

FilterSplit removes all matching elements from a string slice based on a regexp selector and returns all the removed items too

func FilterStringMapByKey added in v0.0.2

func FilterStringMapByKey(selector *regexp.Regexp, m map[string]string) (filtered map[string]string)

FilterStringMapByKey takes a map where a string is the key and filters it by matching the selector

func FilterStringMapByValue added in v0.0.2

func FilterStringMapByValue(selector *regexp.Regexp, m map[string]string) (filtered map[string]string)

FilterStringMapByValue takes a map where a string is the value and filters it by matching the selector

func GetKeyByValue added in v0.0.2

func GetKeyByValue(m map[interface{}]interface{}, target interface{}) (interface{}, error)

GetKeyByValue returns the key of a certain value of a map. It returns the first instance it finds that has the given value. It returns an err if the value is not in the map

func IsFileOrDir added in v0.0.2

func IsFileOrDir(path string) bool

IsFileOrDir returns whether the passed path is an existing file or dir

func LogIfError

func LogIfError(err error)

LogIfError logs an error if it occurs, rather than repeating this structure everywhere

func MapContains added in v0.0.2

func MapContains(m map[interface{}]interface{}, target interface{}, asValue bool) bool

MapContains returns whether a map contains a certain element asValue denotes whether to check for values (if false will search for key)

func MergeStringMaps added in v0.0.2

func MergeStringMaps(maps ...map[string]string) (out map[string]string, err error)

MergeStringMaps merges the passed maps into a single map, errors on key clashes but still returns the merged map. Only final value of the clashed key remains

func PanicIfErr

func PanicIfErr(err error, customMessage string)

PanicIfErr panics in case of an error. If custom message it creates a new error based on that and returns that instead

func ReadJSONFile added in v0.0.2

func ReadJSONFile(filepath string, data interface{}) error

ReadJSONFile reads json from a file and stores it in the passed interface which should be a pointer

func ReadYAMLFile added in v0.0.2

func ReadYAMLFile(filepath string, data interface{}) error

ReadYAMLFile reads yaml from a file and stores it in the passed interface which should be a pointer

func ReturnErrOnPanic

func ReturnErrOnPanic(perr *error) func()

ReturnErrOnPanic catches panics, expects them to be of type error, then stores it in the pointer as recovery

func ReturnFirstErr

func ReturnFirstErr(errs ...error) error

ReturnFirstErr returns the first not-nil error from a list of errors. Used to prevent many copies of if err != nil { return err } when they are indepedent

func RunCommand added in v0.0.2

func RunCommand(cmd *exec.Cmd, path string, noOutput bool) string

RunCommand runs an arbitrary os/exec.Cmd command as if you were in a terminal at the given path

func SelectMatching added in v0.0.2

func SelectMatching(selector *regexp.Regexp, slice []string) (matching []string)

SelectMatching return all matching elements from a string slice based on a regexp selector

func Verify added in v0.0.2

func Verify(v Validatable, err error) error

Verify calls IsValid on the parameter and saves the first error in case of an error it returns the error in the passed variable, it ensures no if err != nil repetition in code

func WriteJSONFile added in v0.0.2

func WriteJSONFile(filepath string, data interface{}) error

WriteJSONFile writes an object as json to the specified file path

func WriteYAMLFile added in v0.0.2

func WriteYAMLFile(filepath string, data interface{}) error

WriteYAMLFile writes an object as yaml to the specified file path

Types

type Parseable added in v0.0.2

type Parseable interface {
	ParseFromFile(filepath string) error
}

Parseable is an interface to support parsing from a file

type Validatable added in v0.0.2

type Validatable interface {
	IsValid() error
}

Validatable is a struct that can be checked for validity

Jump to

Keyboard shortcuts

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