common

package
v0.0.0-...-ebe7275 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: BSD-2-Clause-Patent Imports: 24 Imported by: 48

README

Common

Common utilities for file access, collection manipulation (including some functional helper methods), unit tests and mocks among others. This package will be imported from multiple other packages under its parent directory and to avoid cyclical import dependencies files in this package should not import from peer packages unless absolutely necessary.

Documentation

Index

Constants

View Source
const (
	ControlLogLevelTrace  = ControlLogLevel(logging.LogLevelTrace)
	ControlLogLevelDebug  = ControlLogLevel(logging.LogLevelDebug)
	ControlLogLevelInfo   = ControlLogLevel(logging.LogLevelInfo)
	ControlLogLevelNotice = ControlLogLevel(logging.LogLevelNotice)
	ControlLogLevelError  = ControlLogLevel(logging.LogLevelError)

	DefaultControlLogLevel = ControlLogLevel(logging.DefaultLogLevel)
)

TODO(mjmac): Evaluate whether or not this layer of indirection adds any value.

View Source
const DisableProxyScrubEnv = "DAOS_DISABLE_PROXY_SCRUB"

DisableProxyScrubEnv disabled removal of proxy variables from the process environment.

View Source
const UtilLogDepth = 4

UtilLogDepth signifies stack depth, set calldepth on calls to logger so log message context refers to caller not callee.

Variables

This section is empty.

Functions

func All

func All(ss []string, f func(string) bool) bool

All returns true if all strings returns true from f.

func Any

func Any(ss []string, f func(string) bool) bool

Any returns true if any of the strings in the slice returns true from f.

func AppendFile

func AppendFile(path string) (*os.File, error)

AppendFile appends to existing or creates new file with default options

func AppendToPathEnv

func AppendToPathEnv(pathEnv string, toAdd ...string) string

AppendToPathEnv appends entries to the supplied colon-separated string if non-existent.

func BoolAsInt

func BoolAsInt(b bool) int

BoolAsInt converts a bool to an int.

func CheckDupeProcess

func CheckDupeProcess() error

CheckDupeProcess checks to see if another process with the same name as ours is running.

func CmpTCPAddr

func CmpTCPAddr(a, b *net.TCPAddr) bool

CmpTCPAddr compares two *net.TCPAddr instances and returns true if they are equivalent, false otherwise.

func ConcatErrors

func ConcatErrors(scanErrors []error, err error) error

ConcatErrors builds single error from error slice.

func CpDir

func CpDir(source string, dest string) error

Copy the Directory from source to destination.

func CpFile

func CpFile(src, dst string) error

CpFile copies a file from src to dst.

func DedupeStringSlice

func DedupeStringSlice(in []string) []string

DedupeStringSlice is responsible for returning a slice based on the input with any duplicates removed.

func DeleteKeyValue

func DeleteKeyValue(keyPairs []string, name string) ([]string, error)

DeleteKeyValue removes an existing key and returns new slice of key-value pairs.

func ExpBackoff

func ExpBackoff(base time.Duration, cur, limit uint64) time.Duration

ExpBackoff implements an exponential backoff algorithm, where the parameters are used to multiplicatively slow down the rate of retries of some operation, up to a maximum backoff limit.

func ExpBackoffWithJitter

func ExpBackoffWithJitter(base, jitter time.Duration, cur, limit uint64) time.Duration

ExpBackoffWithJitter is like ExpBackoff but allows for a custom amount of jitter to be specified.

func Filter

func Filter(ss []string, f func(string) bool) (nss []string)

Filter returns new slice with only strings that return true from f.

func FilterStringMatches

func FilterStringMatches(filterStr, actualStr string) bool

FilterStringMatches checks whether a filter string matches the actual string in a case-insensitive way. If the filter string is empty, a match is assumed.

func FindBinary

func FindBinary(binName string) (string, error)

FindBinary attempts to locate the named binary by checking $PATH first. If the binary is not found in $PATH, look in the directory containing the running processes binary as well as the working directory.

func FindKeyValue

func FindKeyValue(keyPairs []string, name string) (string, error)

FindKeyValue will return value from supplied name key if found in input slice of key-pairs (e.g. environment). ErrNotExist error returned if key cannot be found.

func FolderCompress

func FolderCompress(src string, buf io.Writer) error

Archive and create the *tar.gz of the given folder.

func FormatTime

func FormatTime(t time.Time) string

FormatTime returns ISO8601 formatted representation of timestamp with microsecond resolution.

func GetAdjacentPath

func GetAdjacentPath(inPath string) (string, error)

GetAdjacentPath retrieves path relative to the binary used to launch the currently running process.

func GetConsent

func GetConsent(log logging.Logger) bool

GetConsent scans stdin for yes/no

func GetExitStatus

func GetExitStatus(err error) error

GetExitStatus ensures that a monitored process always returns an error of some sort when it exits so that we can respond appropriately.

func GetFilePaths

func GetFilePaths(dir string, ext string) ([]string, error)

GetFilePaths return full file paths in given directory with matching file extensions

func GetProcName

func GetProcName(pid int) (string, error)

GetProcName returns the name of the process with the given pid.

func GetProcPids

func GetProcPids(procName string) ([]int, error)

GetProcPids returns a list of pids for the given process name.

func GetWorkingPath

func GetWorkingPath(inPath string) (string, error)

GetWorkingPath retrieves path relative to the current working directory when invoking the current process.

func HasPort

func HasPort(addr string) bool

HasPort checks if addr specifies a port. This only works with IPv4 addresses at the moment.

func HasPrefixPath

func HasPrefixPath(base, sub string) (bool, error)

HasPrefixPath reports whether sub parameter path is a prefix of the base parameter one.

func Includes

func Includes(ss []string, target string) bool

Includes returns true if string target in slice.

func Index

func Index(ss []string, target string) int

Index returns first index of target string, -1 if no match

func InterfaceIsNil

func InterfaceIsNil(i interface{}) bool

InterfaceIsNil returns true if the interface itself or its underlying value is nil.

func IsAlphabetic

func IsAlphabetic(s string) bool

IsAlphabetic checks of a string just contains alphabetic characters.

func IsHidden

func IsHidden(filename string) bool

Check if file or directory that starts with . which is hidden

func IsLocalAddr

func IsLocalAddr(testAddr *net.TCPAddr) bool

IsLocalAddr returns true if the supplied net.TCPAddr matches one of the local IP addresses, false otherwise.

func LocalhostCtrlAddr

func LocalhostCtrlAddr() *net.TCPAddr

LocalhostCtrlAddr returns a *net.TCPAddr representing the default control address on localhost.

func Map

func Map(ss []string, f func(string) string) (nss []string)

Map returns new slice with f applied to each string in original.

func MergeKeyValues

func MergeKeyValues(curVars []string, newVars []string) (merged []string)

MergeKeyValues merges and deduplicates two slices of key-value pairs. Conflicts are resolved by taking the value from the second list.

func NormalizePath

func NormalizePath(p string) (np string, err error)

Normalize the input path with removing redundant separators, up-level reference, changing relative path to absolute one, etc.

func ParseHostList

func ParseHostList(in []string, defaultPort int) (out []string, err error)

ParseHostList validates and deduplicates the given list of host strings. Any hosts missing a port will have one added according to the defaultPort parameter.

func ParseNumberList

func ParseNumberList(stringList string, output interface{}) error

ParseNumberList converts a comma-separated string of numbers to a slice.

func ParseTime

func ParseTime(ts string) (time.Time, error)

ParseTime returns a time.Time object from ISO8601 or RFC3339 timestamp strings.

func PercentageString

func PercentageString(part, total uint64) string

PercentageString returns string representation of percentage given nominator and denominator unsigned integers.

func Pluralise

func Pluralise(s string, n int) string

Pluralise appends "s" to input string unless n==1.

func PrintStructs

func PrintStructs(name string, i interface{})

PrintStructs dumps friendly YAML representation of structs to stdout proceeded with "name" identifier.

func ResolvePath

func ResolvePath(inPath string, defaultPath string) (outPath string, err error)

ResolvePath simply returns an absolute path, appends input path to current working directory if input path not empty otherwise appends default path to location of running binary (adjacent). Use case is specific to config files.

func Run

func Run(cmd string) error

Run executes command in os and builds useful error message.

func ScrubEnvironment

func ScrubEnvironment(blocklist []string)

ScrubEnvironment modifies the environment variables set for this process and any children which inherit its environment by unsetting any variables supplied in the blocklist.

func ScrubEnvironmentExcept

func ScrubEnvironmentExcept(allowlist []string)

ScrubEnvironmentExcept modifies the environment variables set for this process and any children which inherit its environment by unsetting any variables that are not supplied in the allowlist.

func ScrubProxyVariables

func ScrubProxyVariables()

ScrubProxyVariables removes proxy variables from the process environment.

func SplitPort

func SplitPort(addrPattern string, defaultPort int) (string, string, error)

SplitPort separates port from host in address and can apply default port if address doesn't contain one.

func StringSliceHasDuplicates

func StringSliceHasDuplicates(slice []string) bool

StringSliceHasDuplicates checks whether there are duplicate strings in the slice. Comparisons are case insensitive. If duplicates found, return true.

func StructsToString

func StructsToString(i interface{}) (lines string, err error)

StructsToString returns yaml representation (as a list of strings) of any interface but avoids fields/lines prefixed with xxx_ such as added by protobuf boilerplate.

func SyncDir

func SyncDir(path string) (err error)

SyncDir flushes all prior modifications to a directory. This is required if one modifies a directory (e.g., by creating a new file in it) and needs to wait for this modification to become persistent.

func TokenizeCommaSeparatedString

func TokenizeCommaSeparatedString(str string) []string

TokenizeCommaSeparatedString splits the input string on comma boundaries, and discards any empty strings in the result.

func TruncFile

func TruncFile(path string) (*os.File, error)

TruncFile overrides existing or creates new file with default options

func UpdateKeyValue

func UpdateKeyValue(keyPairs []string, name, newValue string) ([]string, error)

UpdateKeyValue updates a value for existing key and returns new slice of key-value pairs.

func WriteFileAtomic

func WriteFileAtomic(path string, data []byte, perm os.FileMode) error

WriteFileAtomic mimics ioutil.WriteFile, but it makes sure the file is either successfully written persistently or untouched.

func WriteSlice

func WriteSlice(path string, slice []string) (err error)

WriteSlice writes string slice to specified file, overwriting and creating if non-existent.

func WriteString

func WriteString(path string, s string) error

WriteString writes string to specified file, wrapper around WriteSlice.

Types

type ControlLogLevel

type ControlLogLevel logging.LogLevel

ControlLogLevel is a type that specifies log levels

func (ControlLogLevel) MarshalYAML

func (c ControlLogLevel) MarshalYAML() (interface{}, error)

func (ControlLogLevel) String

func (c ControlLogLevel) String() string

func (*ControlLogLevel) UnmarshalYAML

func (c *ControlLogLevel) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements yaml.Unmarshaler on ControlLogMask struct

type ExitStatus

type ExitStatus string

ExitStatus implements the error interface and is used to indicate external process exit conditions.

const NormalExit ExitStatus = "process exited with 0"

NormalExit indicates that the process exited without error.

func (ExitStatus) Error

func (es ExitStatus) Error() string

type GetMemInfoFn

type GetMemInfoFn func() (*MemInfo, error)

type MemInfo

type MemInfo struct {
	HugepagesTotal  int `json:"hugepages_total" hash:"ignore"`
	HugepagesFree   int `json:"hugepages_free" hash:"ignore"`
	HugepagesRsvd   int `json:"hugepages_reserved" hash:"ignore"`
	HugepagesSurp   int `json:"hugepages_surplus" hash:"ignore"`
	HugepageSizeKiB int `json:"hugepage_size_kb"`
	MemTotalKiB     int `json:"mem_total_kb"`
	MemFreeKiB      int `json:"mem_free_kb" hash:"ignore"`
	MemAvailableKiB int `json:"mem_available_kb" hash:"ignore"`
}

MemInfo contains information about system hugepages.

func GetMemInfo

func GetMemInfo() (*MemInfo, error)

GetMemInfo reads /proc/meminfo and returns information about system hugepages and memory (RAM).

func (*MemInfo) HugepagesFreeMB

func (mi *MemInfo) HugepagesFreeMB() int

func (*MemInfo) HugepagesTotalMB

func (mi *MemInfo) HugepagesTotalMB() int

func (*MemInfo) Summary

func (mi *MemInfo) Summary() string

type StringSet

type StringSet map[string]struct{}

StringSet is a non-duplicated set of strings.

func NewStringSet

func NewStringSet(values ...string) StringSet

NewStringSet creates a StringSet and initializes it with zero or more strings.

func (StringSet) Add

func (s StringSet) Add(values ...string)

Add adds zero or more strings to the StringSet.

func (StringSet) AddUnique

func (s StringSet) AddUnique(values ...string) error

AddUnique adds zero or more strings to the StringSet, and returns an error if any already exist.

func (StringSet) Has

func (s StringSet) Has(val string) bool

Has checks if the passed string is in the StringSet.

func (StringSet) MarshalYAML

func (s StringSet) MarshalYAML() (interface{}, error)

MarshalYAML converts from the StringSet to a slice that can be marshaled into YAML.

func (StringSet) String

func (s StringSet) String() string

func (StringSet) ToSlice

func (s StringSet) ToSlice() []string

ToSlice returns the strings in the set as a sorted slice.

func (*StringSet) UnmarshalYAML

func (s *StringSet) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML converts from the YAML string slice to a StringSet.

Directories

Path Synopsis
chk
ctl
srv

Jump to

Keyboard shortcuts

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