util

package
v0.0.22 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: BSD-3-Clause Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyBoolTrue

func AnyBoolTrue(values ...bool) bool

AnyBoolTrue returns True if any of the provided bool values is True.

func CreateCertificateAuthority

func CreateCertificateAuthority() *x509.Certificate

CreateCertificateAuthority generates a certificate authority x509.Certificate.

func CreateClientCertificate

func CreateClientCertificate(name string) *x509.Certificate

CreateClientCertificate generates a client x509.Certificate with the common name provided.

func GetEnvBoolOrDefault

func GetEnvBoolOrDefault(k string, d bool) bool

GetEnvBoolOrDefault returns true if the environment variable is set, or the default d if it is unset.

func GetEnvFloat64OrDefault

func GetEnvFloat64OrDefault(k string, d float64) float64

GetEnvFloat64OrDefault returns the value of the environment variable k as a float *or* the default d if casting fails or the environment variable is not set.

func GetEnvIntOrDefault

func GetEnvIntOrDefault(k string, d int) int

GetEnvIntOrDefault returns the value of the environment variable k as an int *or* the default d if casting fails or the environment variable is not set.

func GetEnvStrOrDefault

func GetEnvStrOrDefault(k, d string) string

GetEnvStrOrDefault returns the value of the environment variable k as a string *or* the default d if casting fails or the environment variable is not set.

func GitHubGroupAndRepoFromURL added in v0.0.17

func GitHubGroupAndRepoFromURL(path string) (group, repo string)

GitHubGroupAndRepoFromURL attempts to return the GitHub group/user and repository from a GitHub URL.

func GitHubNormalToRawLink(path string) string

GitHubNormalToRawLink try to convert a "normal" github link to a raw link.

func HashBytes

func HashBytes(b []byte) string

HashBytes accepts a bytes object and returns a string sha256 hash representing that object.

func HashObject added in v0.0.17

func HashObject(o any) ([]byte, string, error)

HashObject accepts any object, dumps it to json then sends it to HashBytes.

func HashObjectYAML added in v0.0.17

func HashObjectYAML(o any) ([]byte, string, error)

HashObjectYAML accepts any object, dumps it to yaml then sends it to HashBytes.

func Indent

func Indent(s string, n int) string

Indent indents all lines of a given string n spaces.

func IsURL added in v0.0.17

func IsURL(path string) bool

IsURL returns true if the given path string starts with http or https.

func MustCreateDirectory

func MustCreateDirectory(directory string, permissions fs.FileMode)

MustCreateDirectory creates a directory at path `directory` with provided permissions, it panics if an error is encountered.

func MustFileExists

func MustFileExists(f string) bool

MustFileExists returns true if a given file exists, otherwise false, it panics if any error is encountered.

func MustGeneratePrivateKey

func MustGeneratePrivateKey(keySize int) *rsa.PrivateKey

MustGeneratePrivateKey generates a rsa private key of keySize or panics.

func MustSetupWithManager

func MustSetupWithManager(setup func(mgr ctrlruntime.Manager) error, mgr ctrlruntime.Manager)

MustSetupWithManager simply panics if registering a controller to the manager fails.

func Panic

func Panic(msg string)

Panic tries to panic "nicely" but will send a second sigint to really kill the process if the first does not succeed within a second, and if for some reason that one does not kill it, we try a sigkill, and finally if that still didn't work, we will simply panic outright.

func RandomString

func RandomString(length int) string

RandomString returns a string of random alphabet characters `length` long.

func RegexStringSubMatchToMap

func RegexStringSubMatchToMap(p *regexp.Regexp, s string) map[string]string

RegexStringSubMatchToMap accepts a regexp pattern and a string and returns a mapping of named capture groups to their found value. Obviously this only works with named capture groups.

func SignalHandledContext

func SignalHandledContext(
	logf func(f string, a ...interface{}),
) (context.Context, context.CancelFunc)

SignalHandledContext returns a context that will be canceled if a SIGINT or SIGTERM is received.

func StringSliceContainsAll added in v0.0.17

func StringSliceContainsAll(ss, vals []string) bool

StringSliceContainsAll returns true if all values in vals are in string slice ss, otherwise false.

func StringSliceDifference

func StringSliceDifference(a, b []string) []string

StringSliceDifference returns the difference between a and b string slices. Items that exist in slice "b" but are missing in slice "a" will be returned.

func StringSliceEqual added in v0.0.17

func StringSliceEqual(a, b []string) bool

StringSliceEqual returns true if the string slices provided are equal, otherwise false.

func ToPointer added in v0.0.17

func ToPointer[T any](t T) *T

ToPointer accepts an object T and returns a pointer to it.

func UnifiedDiff added in v0.0.17

func UnifiedDiff(a, b any) (string, error)

UnifiedDiff accepts a and b as any, dumps them to json then returns a unified diff from difflibgo.

func WriteHTTPContentsFromPath added in v0.0.17

func WriteHTTPContentsFromPath(
	ctx context.Context,
	path string,
	w io.Writer,
	headerData map[string]string,
) error

WriteHTTPContentsFromPath writes content at the http path `path` into the writer w.

Types

type CertData

type CertData struct {
	TLS []byte
	CRT []byte
	Key []byte
}

CertData is a struct that holds certificate information.

func GenerateCertificateData

func GenerateCertificateData(certBytes, caBytes []byte, key *rsa.PrivateKey) (*CertData, error)

GenerateCertificateData generates certificate data (ready to be written to disk) from the given cert/ca bytes and key.

func (*CertData) Write

func (c *CertData) Write(directory string) error

Write dumps the tls.crt, ca.crt, and tls.key data to disk in the given directory.

type ObjectDiffer added in v0.0.17

type ObjectDiffer[T any] struct {
	// Current objects by endpoint name
	Current map[string]T
	// Missing objects by endpoint name
	Missing []string
	// Extra objects that should be pruned
	Extra []T
}

ObjectDiffer holds objets of type T -- used for comparing current, missing, and extraneous objects in the cluster.

func (*ObjectDiffer[T]) CurrentObjectNames added in v0.0.17

func (d *ObjectDiffer[T]) CurrentObjectNames() []string

CurrentObjectNames returns a slice of the names of the current objects.

func (*ObjectDiffer[T]) SetExtra added in v0.0.17

func (d *ObjectDiffer[T]) SetExtra(allExpectedNames []string)

SetExtra sets the extra objects based on the slice of all expected object names and the current objects -- `Current` must be set prior to calling this or things will be weird.

func (*ObjectDiffer[T]) SetMissing added in v0.0.17

func (d *ObjectDiffer[T]) SetMissing(allExpectedNames []string)

SetMissing sets the missing objects based on the slice of all expected object names.

type StringSet

type StringSet interface {
	Add(s string)
	Extend(sSlice []string)
	Remove(s string)
	Contains(s string) bool
	Len() int
	Items() []string
}

StringSet defines a simple string set interface.

func NewStringSet

func NewStringSet() StringSet

NewStringSet returns an object satisfying the StringSet interface.

func NewStringSetWithValues

func NewStringSetWithValues(vals ...string) StringSet

NewStringSetWithValues returns an object satisfying the StringSet interface with the given values pre-loaded to the set.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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