util

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorInvalidSumsFile = fmt.Errorf("invalid sums file")
	ErrorChecksumError   = fmt.Errorf("could not checksum file")
)
View Source
var (
	// ErrSchemaUnknown indicates the schema could not be found
	ErrSchemaUnknown = errors.New("unknown schema")
	// ErrSchemaValidationFailed indicates that the validator failed to perform validation, perhaps due to invalid schema
	ErrSchemaValidationFailed = errors.New("validation failed")
)
View Source
var ConstantBackOffForTests = backoff.Policy{
	Millis: []int{25},
}

Functions

func Base64IfNotPrintable added in v0.23.0

func Base64IfNotPrintable(val []byte) string

Base64IfNotPrintable returns a string value that's either the given value or base64 encoded if not IsPrintable()

func BuildInfo added in v0.24.0

func BuildInfo() *build.Info

BuildInfo retrieves build information

func DumpJSONIndent added in v0.22.0

func DumpJSONIndent(data any) error

DumpJSONIndent dumps data to stdout as indented JSON

func DumpJSONIndentedFormatted added in v0.26.2

func DumpJSONIndentedFormatted(data []byte, indent int) error

func DumpMapStrings

func DumpMapStrings(data map[string]string, leftPad int)

DumpMapStrings shows k: v of a map[string]string left padded by int, the k will be right aligned and value left aligned

func Ed25519KeyPair added in v0.26.2

func Ed25519KeyPair() (ed25519.PublicKey, ed25519.PrivateKey, error)

func Ed25519KeyPairFromSeed added in v0.26.2

func Ed25519KeyPairFromSeed(seed []byte) (ed25519.PublicKey, ed25519.PrivateKey, error)

func Ed25519KeyPairFromSeedFile added in v0.26.2

func Ed25519KeyPairFromSeedFile(f string) (ed25519.PublicKey, ed25519.PrivateKey, error)

func Ed25519KeyPairToFile added in v0.26.2

func Ed25519KeyPairToFile(f string) (ed25519.PublicKey, ed25519.PrivateKey, error)

func Ed25519Sign added in v0.26.2

func Ed25519Sign(pk ed25519.PrivateKey, msg []byte) ([]byte, error)

func Ed25519SignWithSeedFile added in v0.26.2

func Ed25519SignWithSeedFile(f string, msg []byte) ([]byte, error)

func Ed25519Verify added in v0.27.0

func Ed25519Verify(pk ed25519.PublicKey, msg []byte, sig []byte) (bool, error)

func ExecuteTemplateFile added in v0.27.0

func ExecuteTemplateFile(file string, data any, funcs template.FuncMap) ([]byte, error)

func ExpandPath

func ExpandPath(p string) (string, error)

ExpandPath expands a path that starts in ~ to the users homedir

func FileExist

func FileExist(path string) bool

FileExist checks if a file exist on disk

func FileHasSha256Sum added in v0.25.0

func FileHasSha256Sum(path string, sum string) (bool, string, error)

FileHasSha256Sum verifies that path has the checksum provided, sum should be hex encoded

func FileIsDir

func FileIsDir(path string) bool

func FileIsRegular

func FileIsRegular(path string) bool

func FuncMap added in v0.23.0

func FuncMap(f map[string]any) template.FuncMap

func GovernorSubject added in v0.24.0

func GovernorSubject(name string, collective string) string

GovernorSubject the subject to use for choria managed Governors within a collective

func HasPrefix added in v0.26.0

func HasPrefix(s string, prefixes ...string) bool

HasPrefix checks if s has any one of prefixes

func HomeDir

func HomeDir() (string, error)

HomeDir determines the home location without using the user package or requiring cgo

On Unix it needs HOME set and on windows HOMEDRIVE and HOMEDIR

func InterruptibleSleep

func InterruptibleSleep(ctx context.Context, d time.Duration) error

InterruptibleSleep sleep for the duration in a way that can be interrupted by the context. An error is returned if the context cancels the sleep

func IsEncodedEd25519Key added in v0.28.0

func IsEncodedEd25519Key(k []byte) bool

func IsEncodedEd25519KeyString added in v0.28.0

func IsEncodedEd25519KeyString(k string) bool

func IsExecutableInPath added in v0.25.0

func IsExecutableInPath(c string) bool

func IsPrintable added in v0.23.0

func IsPrintable(s string) bool

IsPrintable determines if a string is printable

func IterateStringsMap

func IterateStringsMap(data map[string]string, cb func(k string, v string))

IterateStringsMap iterates a map[string]string in key sorted order

func LongestString

func LongestString(list []string, max int) int

LongestString determines the length of the longest string in list, capped at max

func MatchAnyRegex

func MatchAnyRegex(str []byte, regex []string) bool

MatchAnyRegex checks str against a list of possible regex, if any match true is returned

func NewUTF8Table added in v0.26.0

func NewUTF8Table(hdr ...any) *xtablewriter.Table

NewUTF8Table creates a table formatted with UTF8 styling

func NewUTF8TableWithTitle added in v0.26.0

func NewUTF8TableWithTitle(title string, hdr ...any) *xtablewriter.Table

NewUTF8TableWithTitle creates a table formatted with UTF8 styling with a title set

func ParagraphPadding added in v0.22.0

func ParagraphPadding(paragraph string, padding int) string

ParagraphPadding pads paragraph with padding spaces

func ParseDuration added in v0.22.0

func ParseDuration(dstr string) (dur time.Duration, err error)

ParseDuration is an extended version of go duration parsing that also supports w,W,d,D,M,Y,y in addition to what go supports

func PromptForConfirmation added in v0.23.0

func PromptForConfirmation(format string, a ...any) (bool, error)

PromptForConfirmation asks for confirmation on the CLI

func RenderDuration added in v0.25.0

func RenderDuration(d time.Duration) string

RenderDuration create a string similar to what %v on a duration would but it supports years, months, etc. Being that days and years are influenced by leap years and such it will never be 100% accurate but for feedback on the terminal its sufficient

func Sha256ChecksumDir added in v0.28.0

func Sha256ChecksumDir(dir string) ([]byte, error)

Sha256ChecksumDir produce a file similar to those produced by sha256sum on the command line.

This function will walk the directory and checksum all files in all subdirectories

func Sha256HashBytes added in v0.25.0

func Sha256HashBytes(c []byte) (string, error)

Sha256HashBytes computes the sha256 sum of the byes c and returns the hex encoded result

func Sha256HashFile added in v0.25.0

func Sha256HashFile(path string) (string, error)

Sha256HashFile computes the sha256 sum of a hash and returns the hex encoded result

func Sha256VerifyDir added in v0.28.0

func Sha256VerifyDir(sumsFile string, dir string, log *logrus.Entry, cb FileReportHandler) (bool, error)

Sha256VerifyDir verifies the sums in sumsFile relative to dir, sumsFile should have been made using Sha256ChecksumDir

When dir is supplied the files in that dir will be verified else the path will be relative to the path of the sumsFile

func SliceGroups

func SliceGroups(input []string, size int, fn func(group []string))

SliceGroups takes a slice of words and make new chunks of given size and call the function with the sub slice. If there are not enough items in the input slice empty strings will pad the last group

func SliceVerticalGroups added in v0.23.0

func SliceVerticalGroups(input []string, size int, fn func(group []string))

SliceVerticalGroups takes a slice of words and make new chunks of given size and call the function with the sub slice. The results are ordered for vertical alignment. If there are not enough items in the input slice empty strings will pad the last group

func StrToBool

func StrToBool(s string) (bool, error)

StrToBool converts a typical mcollective boolianish string to bool

func StringInList

func StringInList(list []string, match string) bool

StringInList checks if match is in list

func StringsMapKeys

func StringsMapKeys(data map[string]string) []string

StringsMapKeys returns the keys from a map[string]string in sorted order

func UniqueID

func UniqueID() (id string)

UniqueID creates a new unique ID, usually a v4 uuid, if that fails a random string based ID is made

func UniqueStrings

func UniqueStrings(items []string, shouldSort bool) []string

func UserConfig added in v0.24.0

func UserConfig() string

UserConfig determines what is the active config file for a user

func ValidateSchemaFromFS added in v0.26.2

func ValidateSchemaFromFS(schemaPath string, data any) (errors []string, err error)

Types

type FileReportHandler added in v0.28.0

type FileReportHandler func(file string, ok bool)

Jump to

Keyboard shortcuts

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