const (
COUNTERS = 10
)
func Bail(txt string, err error)
never returns - if error != nil, print it and exit
func BooleanValue(boolvalue string) (bool, error)
return true or false if a boolean can be parsed. error if string is not something that is understood as a boolean. currently understands true/false,yes/no and on/off
func BooleanValueNoErr(boolvalue string) bool
if a valid boolean can parsed, return true, false in all other cases
func CallingFunction() string
returns a single line with the calling function immedialy preceding the function which invoked this one
func ChmodR(dir string, dirmask, filemask fs.FileMode) error
recursively chmod a dir and files and subdirectories. applies 'dirmask' to dirs and 'filemask' to files
func DebugFlag(name string) *debugFlag
func DirHash(ddir string) (string, error)
return a hash across an entire directory tree, including the contents of all files
func DirWalk(dir string, fn func(root string, relative_filename string) error) error
walk a directory tree and call function for each file (but not each dir)
func DirWalkAllFiles(dir string) ([]string, error)
returns all files relative to dir
func ErrorString(err error) string
extracts the PRIVATE and possibly SENSITIVE debug error message from a string obsolete - use errors.ErrorString(err) the reason this is so convoluted with different types, is that different versions of grpc encapsulate status details in different messages.
func FileExists(filename string) bool
I can never remember how to do this, so here's a helper: return true if file exists, false otherwise
func FindFile(name string) (string, error)
search for a file in git repo and above. error if not found
func FindFileInWorkingDir(name string) (string, error)
this will find a file or directory in the given working directory by traversing up the directory hierarchy but only upto the workingdir (not higher). the returned file is guaranteed to be within the workingdir. if it cannot be found, an error is returned.
func FlagStrings(name, help string, values map[string]string) *flag_string_t
very weird string flag thing
func GetFaultIndicatorClient() faultindicator.FaultIndicatorClient
get the faultindicator client.
func GetIPAndNet(ip_s string) (string, int, error)
splits things like so:
172.29.1.0/24 => "172.29.1.0" and 24
172.29.1.5:5000 => "172.29.1.5" and 32
2a01:4b00:ab0f:5100:5::5/64 => "2a01:4b00:ab0f:5100:5::5" and 64
func GetStack(format string, args ...interface{}) string
get stack in a human readable format
func GetStackLines() []string
get stack, line by line in a human readable format
func HexStr(buf []byte) string
return a mini hexdump as single line string
func Hexdump(prefix string, buf []byte) string
return the buffer as a hexdump with ascii. each line prefixed by 'prefix'
func HexdumpWithLen(ln int, prefix string, buf []byte) string
func HomeDir() (string, error)
get current home dir (from environment variable HOME, if that fails user.Current())
func IntArrayFlag(name string, help string) *intarrayFlag
a flag to allow for a list of uint64 and a convenient method to check if an integer is part of the flag
func IsDir(name string) (bool, error)
return true if file exists and is a directory (uses findfile)
func IsLettersOnly(txt string) bool
return true if string has letters only
func IsLinkLocal(s string) bool
return true if this is a IPv6 link local ip
func IsLoopback(s string) bool
returns true if this is an IPv4 or IPv6 loopback address
func IsOnlyChars(txt string, valid string) bool
true only if string "txt" is made up exclusively of characters in "valid"
func IsPrivateIP(ip_s string) (bool, error)
error if not a valid ip true if it is a non-routeable IP, such as link-local, loopback, or rfc 1918
func LocalTime(ctx context.Context) (time.Time, error)
func LogFault(ctx context.Context, name, desc string)
This logs a fault to the faultindicator service.
func Mac2Str(mac uint64) string
func MakeSafeFilename(name string) string
given an arbitrary string, will remove all unsafe characters. result may be safely used as a filename
func Marshal(req proto.Message) (string, error)
take a proto and convert it into a base64 string
func MarshalBytes(req proto.Message) ([]byte, error)
func MarshalYaml(src interface{}) ([]byte, error)
marshal 'src' to yaml as bytes
func MaxInt(x, y int) int
func MaxInt32(x, y int32) int32
func MaxInt64(x, y int64) int64
func MinInt(x, y int) int
func MinInt32(x, y int32) int32
func MinInt64(x, y int64) int64
func MultiLinePrefix(input, prefix string, linelen int) string
Adds a prefix to each line of a multi-line string. it hounours existing line breaks (\n) if line is longer than linelen it will add a linebreak empty trailing lines will be trimmed a single trailing \n will be retained.
func NotImpl(format string, args ...interface{})
func OpenWriteFile(filename string) (*os.File, error)
like ioutil - but with open permissions to share
func ParseIP(ip_s string) (string, uint32, int, error)
Parse an IPAddress. this may be either IPv4 or IPv6. The result is the IPaddress (perhaps normalised), the port (if any), the version or an error (if unparseable)
func ParseTime(ts string) (time.Time, error)
Parse a time in various (uk) formats and return a time.Time
func ParseTimeWithLocation(ts, tz string) (time.Time, error)
Parse a time in various (uk) formats and return a unix timestamp (in UTC)
func ParseTimestamp(ts string) (uint32, error)
Parse a time in various (uk) formats and return a unix timestamp
func ParseTimestampWithLocation(ts, tz string) (uint32, error)
Parse a time in various (uk) formats and return a unix timestamp
func PrettyNumber(number uint64) string
func PrintStack(format string, args ...interface{})
print a simpliefied stacktrace (filenames and linenumbers only)
func RandomInt(max int64) int
return random integer between 0 and n
func RandomStall(minutes int)
stall for a random amount of "upto" minutes
func RandomString(n int) string
func ReadFile(filename string) ([]byte, error)
read file (uses some magic to find it too)
func ReadProto(filename string, req proto.Message) error
read a proto from disk
func ReadYaml(filename string, target interface{}) error
reads a raml file and parses it (strict) into interface. error if unknown tags are encountered in yaml
func RecreateSafely(dirname string) error
if dir does not exist: create it and create a file ".filelayouter" if dir DOES exist, check for existence of a file ".filelayouter", if so, recreate otherwise error
func RemoveAll(dir string) error
removes dir, changes permissions if needs to
func SafelyWriteFile(filename string, content []byte) error
write a file to a temporary file, then rename it afterwards
func SetConsoleTitle(title string)
func TimeString(t time.Time) string
format a timestring
func TimestampAgeString(timestamp uint32) string
format a timestamp as 'age'
func TimestampString(timestamp uint32) string
format a timestamp
func Tribool(name, help string) *tribool_t
func Unmarshal(b64string string, req proto.Message) error
take a base64 string and convert it into the proto
func UnmarshalBytes(pdata []byte, req proto.Message) error
func UnmarshalYaml(buf []byte, target interface{}) error
interprets the bytes as yaml and decodes it (strict) into interface. error if unknown tags are encountered in yaml
func WorkingDir() string
return the "workingdir" (the one we were started in)
func WriteFile(filename string, content []byte) error
like ioutil - but with open permissions to share
func WriteFileCreateDir(filename string, content []byte) error
like ioutil - but with open permissions to share. if necessary creates directories
func WriteProto(filename string, req proto.Message) error
write a proto to disk
func WriteYaml(filename string, data interface{}) error
write 'data' as a yaml file to 'filename'
compare two arrays. find Missing elements in one or the other
type ArrayComparer struct {
// contains filtered or unexported fields
}
func CompareArray(array1, array2 interface{}, comp func(i, j int) bool) *ArrayComparer
Compare an array. It uses a custom "compare" function which must return true if two elements are equal. i refers to array1[i] and j refers to array2[j] TODO: check if we can use reflect.ValueOf(array1).Index(i) to remove the need for external comp function
func (ac *ArrayComparer) ElementsIn1ButNot2() []int
returns indices of elements that are in array 1 but not array 2
func (ac *ArrayComparer) ElementsIn2ButNot1() []int
returns indices of elements that are in array 2 but not array 1
func (ac *ArrayComparer) EqualElements() bool
returns true if both arrays contain the same elements (disregarding the order they are in)
type ByteStreamReceiver struct {
sync.Mutex
// contains filtered or unexported fields
}
func NewByteStreamReceiver(path string) *ByteStreamReceiver
func NewByteStreamReceiverWithFunction(newfile func(filename string, content []byte) error) *ByteStreamReceiver
report each file to the function. This usually happens when Close() is called, prior to that the receiver has no means of telling if the file is completed. Perhaps a flaw in the protocol?
func (bsr *ByteStreamReceiver) Close() error
func (bsr *ByteStreamReceiver) FileCount() int
how many files were retrieved?
func (bsr *ByteStreamReceiver) Files() map[string]uint64
func (bsr *ByteStreamReceiver) NewData(data StreamData) error
the result of srv.Recv()
func (bsr *ByteStreamReceiver) TotalBytesReceived() uint64
sends a bunch of bytes down a grpc stream
type ByteStreamSender struct {
// contains filtered or unexported fields
}
func NewByteStreamSender(f1 Send_new_file_type, f2 Send_data_type) *ByteStreamSender
create a new bytestream sender. (key and filename are opaque to this sender) f1 - a function that sends a message on the stream to indicate start of a new file and key f2 - a function that sends a bunch of data on the stream
once stream sender is created, call SendBytes with filename and content. it will break the file into small pieces and call f2() with small arrays suitable for sending in a packet
func (bss *ByteStreamSender) FileCount() int
how many files were sent?
func (bss *ByteStreamSender) SendBytes(key, filename string, b []byte) error
type Cell struct {
// contains filtered or unexported fields
}
func (c *Cell) String() string
type ClientConnector interface {
Connect(string) *grpc.ClientConn
}
var (
// public as implementation detail
Client_connector ClientConnector
)
type DirHasher struct {
// contains filtered or unexported fields
}
func (dh *DirHasher) AddEntry(root string, relname string) error
given some reference points will interpolate a value,
examples:
type Interpolator struct {
sync.Mutex
// contains filtered or unexported fields
}
func (ip *Interpolator) AddReferencePoint(number, value float64)
func (ip *Interpolator) AddReferencePoints(points map[float64]float64)
func (ip *Interpolator) LinearInterpolate(number float64) float64
fast and quick interpolation using linear interpolation
func (ip *Interpolator) String() string
type LockedBool struct {
sync.Mutex
// contains filtered or unexported fields
}
func (lb *LockedBool) Set(b bool)
func (lb *LockedBool) Value() bool
type LockedInt struct {
sync.Mutex
// contains filtered or unexported fields
}
func (li *LockedInt) Dec() int
returns NEW value
func (li *LockedInt) Inc() int
returns NEW value
func (li *LockedInt) Set(val int) int
returns PREVIOUS value
func (li *LockedInt) Value() int
type MiniTimeSeries struct {
sync.Mutex
// contains filtered or unexported fields
}
func NewMiniTimeSeries(keep time.Duration) *MiniTimeSeries
create a new rolling timeseries, which keeps values no longer than 'keep' parameter. (older ones will be discarded)
func (mt *MiniTimeSeries) Add(value float64)
add a value now
func (mt *MiniTimeSeries) AddWithTimestamp(ts time.Time, value float64)
add a value with timestamp (backfilling)
func (mt *MiniTimeSeries) All() map[int64]float64
get all values as a map timestamp->value
func (mt *MiniTimeSeries) Difference() float64
total difference between first and last value (latest - earliest)
func (mt *MiniTimeSeries) EarliestValue() (time.Time, float64)
func (mt *MiniTimeSeries) GC()
called automatically occassionally to delete old values
func (mt *MiniTimeSeries) LatestValue() (time.Time, float64)
type PacketReader struct {
// contains filtered or unexported fields
}
func NewPacketReader(r io.Reader, start, escape, stop byte) (*PacketReader, error)
This is part of the Packetizer toolset.
A PacketReader reads from any stream (specifically, an io.Reader). It reads from the stream until any one of the following conditions occur:
A packet is defined as the data between start and stop byte. As a consequence, Any start and stop bytes in the payload must be escaped with the escape byte
The data returned by the packet reader is guaranteed to be equal to the payload send by the PacketWriter. In other words: any escaping and wrapping and unescaping and unwrapping is handled by the reader and writer. see also NewPacketWriter
func (pr *PacketReader) Close()
closes underlying reader as well AND aborts a current read
func (pr *PacketReader) Read(buf []byte) (int, error)
this will return the number of bytes or an error. it is guaranteed to either return an error OR a non-zero number of bytes, but never both
type PacketWriter struct {
// contains filtered or unexported fields
}
func NewPacketWriter(r io.Writer, start, escape, stop byte) (*PacketWriter, error)
This is part of the Packetizer toolset.
A PacketWriter writes arbitrary data to an io.Writer. Each call to PacketWriter.Write is assumed to be one packet. The PacketWriter sends extra data to the io.Writer to allow a PacketReader to identify and reassemble each packet.
The algorithm to send any one packet can be summarised like so:
This algorithm is sufficient to send any data, 8-bit clean, across any io.Writer and reassemble it on the receiving side ot he stream. The canonical implementation to reassemble the packets is NewPacketReader.
func (pr *PacketWriter) Close() error
func (pr *PacketWriter) Write(buf []byte) (int, error)
write a packet (using buf as the payload)
type PeriodicTimer struct {
// contains filtered or unexported fields
}
func NewPeriodicTimer(secs []time.Duration, cb func(pt *PeriodicTimer, secsLapsed time.Duration) error) *PeriodicTimer
A "PeriodicTimer" executes a callback at certain intervals over a certain period of time. For example, a periodictimer, defined as NewPeriodicTimer([]uint32{20,15,5}) will run for 20 seconds and call the callback after 5 seconds, 10 seconds and 20 seconds. The callback will be retried every second if it returns an error until it returns no error callback will also be called each time "Start()" is called.
Note: this code is 'experimental' (at best). it is neither optimised for efficiency nor intended to be used for high-performance requirements. it is intented to be used for small, simple periodic tasks. a typical secs array may contain 5 entries. anything over 10 is considered large and untested.
func (pt *PeriodicTimer) LastStarted() time.Time
func (pt *PeriodicTimer) Secs() []time.Duration
func (pt *PeriodicTimer) Start()
func (pt *PeriodicTimer) Stop()
func (pt *PeriodicTimer) Wait()
wait for timer to either stop or expire
* This is a simple command line "progress reporter". * It's usage is (intentionally) very simple. * Example: * pr := utils.ProgressReporter{} * pr.SetTotal(1000) * for i:=0;i<1000;i++ { * // do something slow * DoSomethingSlow() * pr.Inc() * pr.Print() * } * The above sippet will print a rate and an ETA once a second.
type ProgressReporter struct {
RawPrint bool
Prefix string
// contains filtered or unexported fields
}
func (p *ProgressReporter) Add(a uint64)
func (p *ProgressReporter) Eta() time.Time
func (p *ProgressReporter) Inc()
func (p *ProgressReporter) Print() bool
func (p *ProgressReporter) PrintSingleLine() bool
return true if it actually printed stuff
func (p *ProgressReporter) Rate() float64
func (p *ProgressReporter) Set(a uint64)
func (p *ProgressReporter) SetTotal(total uint64)
func (p *ProgressReporter) String() string
type RateCalculator interface {
Add(a uint64)
Rate() float64
Reset()
String() string
}
func NewRateCalculator(name string) RateCalculator
type Row struct {
// contains filtered or unexported fields
}
func (r *Row) AddCell(cell *Cell)
func (r *Row) Cells() []*Cell
return all cells (considering the col<->idx mapping)
func (r *Row) Cols() int
return # of cells (considering the col<->idx mapping)
func (r *Row) GetCell(idx int) *Cell
return a cell (considering the col<->idx mapping)
type Send_data_type func(b []byte) error
type Send_new_file_type func(key, filename string) error
serialise/deserialise a bunch of variables
type Shifter struct {
// contains filtered or unexported fields
}
func NewShifter(buf []byte) *Shifter
A "Shifter" shifts bytes from a larger number into an array and unshifts it again
func (sh *Shifter) Array8() []byte
next byte is length, followed by uint8s
func (sh *Shifter) Bytes() []byte
return ALL bytes
func (sh *Shifter) Error() error
func (sh *Shifter) RemainingBytes() []byte
return remaining bytes
func (sh *Shifter) SetUint32(pos int, b uint32)
modify the underlying byte array to set a uint32 at a particular address
func (sh *Shifter) Unshift_uint16() uint32
func (sh *Shifter) Unshift_uint32() uint32
LSB first, MSB last. e.g. 0xAABBCCDD will be shifted into a byte array like so: []byte{0xDD,0xCC,0xBB,0xAA}
func (sh *Shifter) Unshift_uint64() uint64
func (sh *Shifter) Unshift_uint8() uint8
it is often useful to take the most recent period of time average, for example last minute average. however, it is useful to double-buffer this so that there is always a full sample available. that is where this struct helps. That is, it is guaranteed that the average is always calculated over at least MinAge. Periodically old average numbers are "dropped", so the average is also reflective of fresh values.
The InitialAge value may be set to start providing averages faster than MinAge upon startup.
type SlidingAverage struct {
InitialAge time.Duration // time to wait before providing the first averages (after startup)
MinAge time.Duration // minimum age before a counter is valid
MinSamples uint64 // minimum number of samples before a counter is valid
// contains filtered or unexported fields
}
func NewSlidingAverage() *SlidingAverage
func (sa *SlidingAverage) Add(counter int, a uint64)
func (sa *SlidingAverage) GetAverage(counter int) float64
func (sa *SlidingAverage) GetCounter(counter int) uint64
get a counter
func (sa *SlidingAverage) GetCounts(counter int) uint64
get number of counts
func (sa *SlidingAverage) GetRate(counter int) float64
per second added rate
the proto must be compatible with this interface
type StreamData interface {
GetFilename() string
GetData() []byte
}
type StreamToPacket struct {
// contains filtered or unexported fields
}
func NewStreamToPacket(start, esc, stop byte) (*StreamToPacket, error)
func (stp *StreamToPacket) AddByte(b byte) bool
returns true if a complete packet is in the buf
func (stp *StreamToPacket) ReadPacket() []byte
type Table struct {
// contains filtered or unexported fields
}
func (t *Table) AddBool(b bool) *Table
func (t *Table) AddFloat64(f float64) *Table
func (t *Table) AddHeader(s string)
func (t *Table) AddHeaders(s ...string)
func (t *Table) AddInt(i int) *Table
func (t *Table) AddInt64(i int64) *Table
func (t *Table) AddString(s string) *Table
func (t *Table) AddStrings(sts ...string) *Table
func (t *Table) AddTimestamp(ts uint32) *Table
func (t *Table) AddTimestampWithAge(ts uint32) *Table
func (t *Table) AddUint32(i uint32) *Table
func (t *Table) AddUint64(i uint64) *Table
func (t *Table) DisableColumn(col int)
column 0..n
func (t *Table) EnableAllColumns()
column 0..n
func (t *Table) EnableColumn(col int)
column 0..n
func (t *Table) GetMaxLen(col int) int
func (t *Table) GetPrintingRows() []*Row
gets "printing" rows. Multi-line text or text that is wrapped will create an extra row
func (t *Table) GetRowOrCreate(num int) *Row
func (t *Table) NewRow()
create a new row (writing will commence at a new row
func (t *Table) SetMaxLen(col, width int)
func (t *Table) ToCSV() string
func (t *Table) ToPrettyString() string
type TerminalDimensions struct {
// contains filtered or unexported fields
}
func TerminalSize() (*TerminalDimensions, error)
get the size of the current xterm
func (td *TerminalDimensions) Columns() int
func (td *TerminalDimensions) Rows() int
type TextFormatter struct {
// contains filtered or unexported fields
}
func (tf *TextFormatter) ToPrettyString() string
type TextPositionFinder interface {
// advance position to line after pattern found. return true if found
FindLineContaining(s string) bool
// insert line at position
AddLine(s string)
// return current content
Content() []byte
}
func NewTextPositionFinder(ct []byte) TextPositionFinder
a position finder helps finding certain positions in pieces of text. for example: find the '}' after the first occurence of line "foo" and "bar". Each call advances the position further. AddLine inserts a new line at current position.
type TimeoutLock interface {
Unlock()
Lock()
LockWithTimeout(time.Duration) bool // true if lock was acquired
}
func NewTimeoutLock(name string) TimeoutLock
create a new lock which can be used like sync.Mutex but also adds LockWithTimeout()
type Value struct {
Integer int
}