util

package
v0.0.0-...-a6c244b Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 28 Imported by: 261

Documentation

Overview

Package util contains utility functions depended on by many other packages.

Index

Constants

View Source
const UnmarshalStrictError = "error unmarshalling yaml strict"

Variables

View Source
var ErrBufferFull = errors.New("buffer full")

ErrBufferFull indicates that a CappedWriter's bytes.Buffer has MaxBytes bytes.

Functions

func CalculateHMACHash

func CalculateHMACHash(secret []byte, body []byte) (string, error)

CalculateHMACHash calculates a sha256 HMAC has of the body with the given secret. The body must NOT be modified after calculating this hash. The string result will be prefixed with "sha256=", followed by the HMAC hash. When validating this hash, use a constant-time compare to avoid vulnerability to timing attacks.

func CheckURL

func CheckURL(str string) error

CheckURL returns errors if str is not in the form of an expected URL

func CleanForPath

func CleanForPath(name string) string

func CleanName

func CleanName(name string) string

CleanName returns a name with spaces and dashes replaced with safe underscores

func CoalesceString

func CoalesceString(in ...string) string

func CoalesceStrings

func CoalesceStrings(inArray []string, inStrs ...string) string

func ConsistentFilepath

func ConsistentFilepath(parts ...string) string

ConsistentFilepath returns a filepath that always uses forward slashes ('/') rather than platform-dependent file path separators.

func DeepCopy

func DeepCopy(src, copy interface{}) error

DeepCopy makes a deep copy of the src value into the copy params It uses json marshalling to do so, so the src and copy params must be json encodable and decodable. It only works with public fields.

func ExpandValues

func ExpandValues(input interface{}, expansions *Expansions) error

Taking in the input and expansions map, apply the expansions to any appropriate fields in the input. The input must be a pointer to a struct so that the underlying struct can be modified.

func GetBoolValue

func GetBoolValue(r *http.Request, valueKey string, defaultValue bool) (bool, error)

GetBoolValue returns a form value as an integer

func GetIntValue

func GetIntValue(r *http.Request, valueKey string, defaultValue int) (int, error)

GetIntValue returns a form value as an integer

func GetStringArrayValue

func GetStringArrayValue(r *http.Request, valueKey string, defaultValue []string) []string

GetStringArrayValue returns a form value as a string array

func HasAllowedImageAsPrefix

func HasAllowedImageAsPrefix(str string, imageList []string) bool

HasAllowedImageAsPrefix returns true if the given string has one of the allowed image prefixes

func IndexWhiteSpace

func IndexWhiteSpace(s string) int

IndexWhiteSpace returns the first index of white space in the given string. Returns -1 if no white space exists.

func IsExpandable

func IsExpandable(param string) bool

IsExpandable returns true if the passed in string contains an expandable parameter

func IsFieldPtr

func IsFieldPtr(v reflect.Value) bool

IsFieldPtr returns a boolean indicating whether the reflected value is a pointer.

func IsFieldUndefined

func IsFieldUndefined(v reflect.Value) bool

IsFieldUndefined is an adaptation of IsZero https://golang.org/src/reflect/value.go?s=34297:34325#L1090

func IsFiniteNumericFloat

func IsFiniteNumericFloat(f float64) bool

IsFiniteNumericFloat takes a float64 and checks that it is not +inf, -inf, or NaN

func Min

func Min(a ...int) int

min function for ints

func NewEvergreenWebhookLogger

func NewEvergreenWebhookLogger() (send.Sender, error)

func NewWebhookMessage

func NewWebhookMessage(raw EvergreenWebhook) message.Composer

func PowerShellQuotedString

func PowerShellQuotedString(s string) string

PowerShellQuotedString returns a string PowerShell which, when interpreted by PowerShell, all quotation marks in s are treated as literal quotation marks.

func RecursivelySetUndefinedFields

func RecursivelySetUndefinedFields(structToSet, structToDefaultFrom reflect.Value)

RecursivelySetUndefinedFields sets all fields that are not set in structToSet to the value of the corresponding field in structToDefaultFrom.

func RespErrorf

func RespErrorf(resp *http.Response, format string, args ...interface{}) error

RespErrorf attempts to read a gimlet.ErrorResponse from the response body JSON. If successful, it returns the gimlet.ErrorResponse wrapped with the HTTP status code and the formatted error message. Otherwise, it returns an error message with the HTTP status and raw response body.

func TryParseFloat

func TryParseFloat(s string) (float64, error)

TryParseFloat takes an input string and validates that it is a valid finite floating point number. The number is returned if valid, NaN if not

func UnmarshalYAMLStrictWithFallback

func UnmarshalYAMLStrictWithFallback(in []byte, out interface{}) error

UnmarshalYAMLStrictWithFallback attempts to use yaml v3 to unmarshal strict, but on failure attempts yaml v2. If this succeeds then we can assume in is outdated yaml and requires v2, otherwise we only return the error relevant to the current yaml version. This should only be used for cases where we expect v3 to fail for legacy cases.

func UnmarshalYAMLWithFallback

func UnmarshalYAMLWithFallback(in []byte, out interface{}) error

UnmarshalYAMLWithFallback attempts to use yaml v3 to unmarshal, but on failure attempts yaml v2. If this succeeds then we can assume in is outdated yaml and requires v2, otherwise we only return the error relevant to the current yaml version. This should only be used for cases where we expect v3 to fail for legacy cases.

func WriteToTempFile

func WriteToTempFile(data string) (string, error)

WriteToTempFile writes the given string to a temporary file and returns the path to the file.

Types

type CachedDurationValue

type CachedDurationValue struct {
	Value       time.Duration `bson:"value"`
	StdDev      time.Duration `bson:"std_dev"`
	TTL         time.Duration `bson:"ttl"`
	CollectedAt time.Time     `bson:"collected_at"`
	// contains filtered or unexported fields
}

CachedDurationValue represents a calculated int value saved in a database with a expiration time. When the data is not expired, the value is returned directly by the Get() method, otherwise, a refresh function is called, to update the value.

func NewCachedDurationValue

func NewCachedDurationValue(start, ttl time.Duration, refresh CachedDurationValueRefresher) *CachedDurationValue

NewCachedDurationValue constructs a CachedDurationValue object.

func (*CachedDurationValue) Get

Get returns the value, refreshing it when its stale. The "ok" value tells the caller that the value needs to be persisted and may have changed since the last time Get was called.

func (*CachedDurationValue) SetRefresher

SetRefresher sets CachedDurationValueRefresher for the object which is needed when reading CachedDurationValue objects out of the database.

It is not permissible to set the refresher to either nil or a value when it is *not* nil.

func (*CachedDurationValue) String

func (v *CachedDurationValue) String() string

String implements fmt.Stringer reporting how stale the value is in the stale case.

type CachedDurationValueRefresher

type CachedDurationValueRefresher func(DurationStats) (DurationStats, bool)

CachedDurationValueRefresher provides a mechanism for CachedDurationValues to update their values when the current cached value expires. Implementations are responsible for logging errors, as needed.

type CachedIntValue

type CachedIntValue struct {
	Value       int           `bson:"value"`
	TTL         time.Duration `bson:"ttl"`
	CollectedAt time.Time     `bson:"collected_at"`
	// contains filtered or unexported fields
}

CachedIntValue represents a calculated int value saved in a database with a expiration time. When the data is not expired, the value is returned directly by the Get() method, otherwise, a refresh function is called, to update the value.

func NewCachedIntValue

func NewCachedIntValue(start int, ttl time.Duration, refresh CachedIntValueRefresher) *CachedIntValue

NewCachedIntValue constructs a CachedIntValue object.

func (*CachedIntValue) Get

func (v *CachedIntValue) Get() (int, bool)

Get returns the value, refreshing it when its stale. The "ok" value reports errors with the refresh process and alerts callers that the value might be stale.

func (*CachedIntValue) SetRefresher

func (v *CachedIntValue) SetRefresher(r CachedIntValueRefresher) error

SetRefresher sets CachedIntValueRefresher for the object which is needed when reading CachedIntValue objects out of the database.

It is not permissible to set the refresher to either nil or a value when it is *not* nil.

func (*CachedIntValue) String

func (v *CachedIntValue) String() string

String implements fmt.Stringer reporting how stale the value is in the stale case.

type CachedIntValueRefresher

type CachedIntValueRefresher func(int) (int, bool)

CachedIntValueRefresher provides a mechanism for CachedIntValues to update their values when the current cached value expires. Implementations are responsible for logging errors, as needed.

type CappedWriter

type CappedWriter struct {
	Buffer   *bytes.Buffer
	MaxBytes int
}

CappedWriter implements a buffer that stores up to MaxBytes bytes. Returns ErrBufferFull on overflowing writes

func NewCappedWriter

func NewCappedWriter(size int) *CappedWriter

NewCappedWriter is a convenience constructor to create a CappedWriter with no contents and the given size.

func NewMBCappedWriter

func NewMBCappedWriter() *CappedWriter

NewMBCappedWriter is the same as NewCappedWriter but sets a default size of 1MB.

func (*CappedWriter) Close

func (cw *CappedWriter) Close() error

Close is a noop method so that you can use CappedWriter as an io.WriteCloser.

func (*CappedWriter) IsFull

func (cw *CappedWriter) IsFull() bool

IsFull indicates whether the buffer is full.

func (*CappedWriter) String

func (cw *CappedWriter) String() string

String return the contents of the buffer as a string.

func (*CappedWriter) Write

func (cw *CappedWriter) Write(in []byte) (int, error)

Write writes to the buffer. An error is returned if the buffer is full.

type DurationStats

type DurationStats struct {
	Average time.Duration
	StdDev  time.Duration
}

type EvergreenWebhook

type EvergreenWebhook struct {
	NotificationID string      `bson:"notification_id"`
	URL            string      `bson:"url"`
	Secret         []byte      `bson:"secret"`
	Body           []byte      `bson:"body"`
	Headers        http.Header `bson:"headers"`
	Retries        int         `bson:"retries"`
	MinDelayMS     int         `bson:"min_delay_ms"`
	TimeoutMS      int         `bson:"timeout_ms"`
}

type Expansions

type Expansions map[string]string

Wrapper for an expansions map, with some utility functions.

func NewExpansions

func NewExpansions(initMap map[string]string) *Expansions

Return a new Expansions object with all of the specified expansions present.

func (*Expansions) Exists

func (exp *Expansions) Exists(expansion string) bool

Check if a value is present in the expansions.

func (*Expansions) ExpandString

func (exp *Expansions) ExpandString(toExpand string) (string, error)

Apply the expansions to a single string. Return the expanded string, or an error if the input string is malformed.

func (*Expansions) Get

func (exp *Expansions) Get(expansion string) string

Get a single value from the expansions. Return the value, or the empty string if the value is not present.

func (*Expansions) Map

func (exp *Expansions) Map() map[string]string

func (*Expansions) Put

func (exp *Expansions) Put(expansion string, value string)

Set a single value in the expansions.

func (*Expansions) Remove

func (exp *Expansions) Remove(expansion string)

Remove deletes a value from the expansions.

func (*Expansions) Update

func (exp *Expansions) Update(newItems map[string]string)

Update all of the specified keys in the expansions to point to the specified values.

func (*Expansions) UpdateFromYaml

func (exp *Expansions) UpdateFromYaml(filename string) error

Read a map of keys/values from the given file, and update the expansions to include them (overwriting any duplicates with the new value).

type KeyValuePair

type KeyValuePair struct {
	Key   string      `bson:"key" json:"key" yaml:"key"`
	Value interface{} `bson:"value" json:"value" yaml:"value"`
}

type KeyValuePairSlice

type KeyValuePairSlice []KeyValuePair

func MakeKeyValuePair

func MakeKeyValuePair(in map[string]string) KeyValuePairSlice

func MakeNestedKeyValuePair

func MakeNestedKeyValuePair(in map[string]map[string]string) KeyValuePairSlice

func (KeyValuePairSlice) Map

func (in KeyValuePairSlice) Map() (map[string]string, error)

func (KeyValuePairSlice) NestedMap

func (in KeyValuePairSlice) NestedMap() (map[string]map[string]string, error)

Jump to

Keyboard shortcuts

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