Documentation ¶
Overview ¶
Package util contains utility functions depended on by many other packages.
Index ¶
- Constants
- Variables
- func CalculateHMACHash(secret []byte, body []byte) (string, error)
- func CheckURL(str string) error
- func CleanForPath(name string) string
- func CleanName(name string) string
- func CoalesceString(in ...string) string
- func CoalesceStrings(inArray []string, inStrs ...string) string
- func ConsistentFilepath(parts ...string) string
- func DeepCopy(src, copy interface{}) error
- func EscapeJQLReservedChars(in string) string
- func ExpandValues(input interface{}, expansions *Expansions) error
- func GetIntValue(r *http.Request, valueKey string, defaultValue int) (int, error)
- func GetSHA256Hash(s string) string
- func HasAllowedImageAsPrefix(str string, imageList []string) bool
- func IndexWhiteSpace(s string) int
- func IsExpandable(param string) bool
- func IsFieldPtr(v reflect.Value) bool
- func IsFieldUndefined(v reflect.Value) bool
- func IsFiniteNumericFloat(f float64) bool
- func Min(a ...int) int
- func NewEvergreenWebhookLogger() (send.Sender, error)
- func NewWebhookMessage(raw EvergreenWebhook) message.Composer
- func PowerShellQuotedString(s string) string
- func RecursivelySetUndefinedFields(structToSet, structToDefaultFrom reflect.Value)
- func RespErrorf(resp *http.Response, format string, args ...interface{}) error
- func TimeIsWithinLastDay(timestamp time.Time) bool
- func TryParseFloat(s string) (float64, error)
- func UnmarshalYAMLStrictWithFallback(in []byte, out interface{}) error
- func UnmarshalYAMLWithFallback(in []byte, out interface{}) error
- func WriteToTempFile(data string) (string, error)
- type CachedDurationValue
- type CachedDurationValueRefresher
- type CachedIntValue
- type CachedIntValueRefresher
- type CappedWriter
- type DurationStats
- type EvergreenWebhook
- type Expansions
- func (exp *Expansions) Exists(expansion string) bool
- func (exp *Expansions) ExpandString(toExpand string) (string, error)
- func (exp *Expansions) Get(expansion string) string
- func (exp *Expansions) Map() map[string]string
- func (exp *Expansions) Put(expansion string, value string)
- func (exp *Expansions) Remove(expansion string)
- func (exp *Expansions) Update(newItems map[string]string)
- func (exp *Expansions) UpdateFromYaml(filename string) ([]string, error)
- type KeyValuePair
- type KeyValuePairSlice
Constants ¶
const UnmarshalStrictError = "error unmarshalling yaml strict"
Variables ¶
var ErrBufferFull = errors.New("buffer full")
ErrBufferFull indicates that a CappedWriter's bytes.Buffer has MaxBytes bytes.
Functions ¶
func CalculateHMACHash ¶
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 CleanForPath ¶
func CoalesceString ¶
func CoalesceStrings ¶
func ConsistentFilepath ¶
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 EscapeJQLReservedChars ¶
see https://confluence.atlassian.com/jiracoreserver073/search-syntax-for-text-fields-861257223.html
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 GetIntValue ¶
GetIntValue returns a form value as an integer
func GetSHA256Hash ¶
GetSHA256Hash returns the SHA256 hash of the given string.
func HasAllowedImageAsPrefix ¶
HasAllowedImageAsPrefix returns true if the given string has one of the allowed image prefixes
func IndexWhiteSpace ¶
IndexWhiteSpace returns the first index of white space in the given string. Returns -1 if no white space exists.
func IsExpandable ¶
IsExpandable returns true if the passed in string contains an expandable parameter
func IsFieldPtr ¶
IsFieldPtr returns a boolean indicating whether the reflected value is a pointer.
func IsFieldUndefined ¶
IsFieldUndefined is an adaptation of IsZero https://golang.org/src/reflect/value.go?s=34297:34325#L1090
func IsFiniteNumericFloat ¶
IsFiniteNumericFloat takes a float64 and checks that it is not +inf, -inf, or NaN
func NewWebhookMessage ¶
func NewWebhookMessage(raw EvergreenWebhook) message.Composer
func PowerShellQuotedString ¶
PowerShellQuotedString returns a string PowerShell which, when interpreted by PowerShell, all quotation marks in s are treated as literal quotation marks.
func RecursivelySetUndefinedFields ¶
RecursivelySetUndefinedFields sets all fields that are not set in structToSet to the value of the corresponding field in structToDefaultFrom.
func RespErrorf ¶
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 TimeIsWithinLastDay ¶
TimeIsWithinLastDay indicates if the timestamp occurred within the last day.
func TryParseFloat ¶
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 ¶
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 ¶
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 ¶
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 (*CachedDurationValue) Get ¶
func (v *CachedDurationValue) Get() (DurationStats, bool)
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 ¶
func (v *CachedDurationValue) SetRefresher(r CachedDurationValueRefresher) error
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 (*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 ¶
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 ¶
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.
type EvergreenWebhook ¶
type Expansions ¶
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)
ExpandString applies 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) ([]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). It returns the updated keys from the Yaml.
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