utils

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2023 License: GPL-3.0 Imports: 29 Imported by: 23

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	JsonObjectStartDelim = json.Delim('{')
	JsonObjectEndDelim   = json.Delim('}')
	JsonArrayStartDelim  = json.Delim('[')
	JsonArrayEndDelim    = json.Delim(']')
)

Functions

func ByteCountIEC

func ByteCountIEC(b int64) string

func ByteCountSI

func ByteCountSI(b int64) string

func Copy

func Copy(source interface{}) interface{}

Creates an exact duplicae of struct behind the given interface

in: source - data structure to create a copy of out: a copy of the given source data structure

func CopyFiles

func CopyFiles(src, dst string, BUFFERSIZE int64) error

Copies a source file to the given destination file path. The function uses a buffer that optimizes the copy operation.

func CopyStrPtr

func CopyStrPtr(s *string) *string

func DirCompare

func DirCompare(dirRoot1, dirRoot2 string) (bool, error)

DirCompare compares the digests of all files in the given directories and returns whether the directory contents match or not

func DirReverseLookup

func DirReverseLookup(name, homePath string) string

Searches from the given path for a directory of the given name. If the directory is found in the user's home directory then it is returned. If not found the the search continues up the directory tree. If directory is not found then it will be created at the given path.

func ExtractMatches

func ExtractMatches(buffer []byte, patterns map[string]*regexp.Regexp) map[string][][]string

func FormatFullName

func FormatFullName(first, middle, family string) string

Return a formated full name

func FormatMessage

func FormatMessage(
	indent, width int,
	indentFirst, capitalize bool,
	format string, args ...interface{},
) string

func FormatMultilineString

func FormatMultilineString(
	input string,
	indent, width int,
	padFirstIndent bool,
	splitEven bool,
) (string, bool)

func GetItemsWithMatchAtPath

func GetItemsWithMatchAtPath(keyPath, keyValueMatch string, valueMapArray []interface{}) ([]interface{}, error)

Returns the map from an array of maps with the key matching the given regex.

in: keyPath - key path where path separator is '/' in: valueMapArray - nested map of maps to lookup key in

func GetValueAtPath

func GetValueAtPath(keyPath string, valueMap interface{}) (interface{}, error)

func HashString

func HashString(s, prefix string) (string, error)

func InvokeWithTimeout

func InvokeWithTimeout(fn func(), timeout time.Duration) bool

Invoke the given function and waits for it to complete within the specified timeout. Returns false if function call times out. If timeout is 0 then call will wait indefinitely until function call completes.

func JoinListAsSentence

func JoinListAsSentence(format string, list []string, quoteListItems bool) string

func LinkPaths

func LinkPaths(linkSrc, linkDest string) error

Creates a symbolic link to the given src path. If windows then hard copy is done as some tools are unable to follow windows links

func LockTimeout

func LockTimeout(mx *sync.Mutex, timeout time.Duration) bool

Waits for the lock for the specified max timeout. Returns false if waiting timed out.

func MD5DirAll

func MD5DirAll(dirRoot string) (map[string][md5.Size]byte, error)

MD5All reads all the files in the file tree rooted at root and returns a map from file path to the MD5 sum of the file's contents. If the directory walk fails or any read operation fails, MD5All returns an error. In that case, MD5All does not wait for inflight read operations to complete.

func MustGetValueAtPath

func MustGetValueAtPath(keyPath string, valueMap interface{}) interface{}

Returns the value at the given path within a nested map instance

in: keyPath - key path where path separator is '/' in: valueMap - nested map of maps to lookup key in out: the value at the given path

func NewChunkReadSeeker

func NewChunkReadSeeker(source io.ReaderAt, offset, chunkSize int64) io.ReadSeeker

func NewChunkWriteSeeker

func NewChunkWriteSeeker(dest io.WriterAt, offset, chunkSize int64) io.WriteSeeker

func PtrToStr

func PtrToStr(s string) *string

func RandomString

func RandomString(n int) string

func ReadJSONDelimiter

func ReadJSONDelimiter(decoder *json.Decoder, delimiter json.Delim) (json.Token, error)

Reads a specified JSON delimiter from a json stream decoder. If the expected delimiter is not read then an error is returned.

in: decoder - an instance of a json decoder stream reader in: delimiter - the delimiter expected to be read as the next token out: the json token read

func RepeatString

func RepeatString(s string, n int, out io.Writer)

func SetValueAtPath

func SetValueAtPath(keyPath string, value interface{}, valueMap interface{}) error

Sets a value at the given path within a nested map instance

in: keyPath - key path where path separator is '/' in: value - value to set in: valueMap - nested map of maps to lookup key in

func SortValueMap

func SortValueMap(keyPath string, valueMapArray interface{}) error

Sorts the given array of maps using the given key path as the sort key

in: keyPath - key path where path separator is '/' in: valueMapArray - an array of maps to be sorted

func SplitString

func SplitString(input string, indent, width int) []string

func Unzip

func Unzip(zippedData []byte, dest string) ([]string, error)

Unzip will decompress a zipped content, moving all files and folders within the zip content (parameter 1) to an output directory (parameter 2).

func UnzipStream

func UnzipStream(reader io.ReaderAt, size int64, dest string) ([]string, error)

func WaitTimeout

func WaitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool

Waits for the waitgroup for the specified max timeout. Returns false if waiting timed out.

Types

type ChunkReadSeeker

type ChunkReadSeeker struct {
	// contains filtered or unexported fields
}

Chunk reader seeker

func (*ChunkReadSeeker) Read

func (r *ChunkReadSeeker) Read(data []byte) (int, error)

func (*ChunkReadSeeker) Seek

func (s *ChunkReadSeeker) Seek(offset int64, whence int) (int64, error)

type ChunkWriteSeeker

type ChunkWriteSeeker struct {
	// contains filtered or unexported fields
}

Chunk write

func (*ChunkWriteSeeker) Seek

func (r *ChunkWriteSeeker) Seek(offset int64, whence int) (int64, error)

func (*ChunkWriteSeeker) Write

func (r *ChunkWriteSeeker) Write(data []byte) (int, error)

type ExecTimer

type ExecTimer struct {
	// contains filtered or unexported fields
}

An execution timer will execute a call back after a specified period of time. The call back can inform the timer to resume to trigger at the same interval by returning 0 or update the interval by returning the next interval period in milliseconds. When the timer first starts it invokes the call back to determin the trigger interval.

func NewExecTimer

func NewExecTimer(
	ctx context.Context,
	callback func() (time.Duration, error),
	stopOnError bool,
) *ExecTimer

func (*ExecTimer) Start

func (t *ExecTimer) Start(timeout time.Duration) error

func (*ExecTimer) Stop

func (t *ExecTimer) Stop() error

type SSHClient

type SSHClient struct {
	// contains filtered or unexported fields
}

func SSHDial

func SSHDial(
	network, address string,
	config *ssh.ClientConfig,
) (*SSHClient, error)

dial starts a client connection to the given SSH server.

func SSHDialWithKey

func SSHDialWithKey(
	address, user string,
	key []byte,
) (*SSHClient, error)

starts a client connection to the given SSH server with key authmethod.

func SSHDialWithKeyWithPassphrase

func SSHDialWithKeyWithPassphrase(
	address, user string,
	key []byte, passphrase string,
) (*SSHClient, error)

same as DialWithKey but with a passphrase to decrypt the private key

func SSHDialWithPasswd

func SSHDialWithPasswd(
	address, user, passwd string,
) (*SSHClient, error)

starts a client connection to the given SSH server with passwd authmethod.

func (*SSHClient) Close

func (c *SSHClient) Close() error

func (*SSHClient) Cmd

func (c *SSHClient) Cmd(cmd string) *remoteScript

run a command on client

func (*SSHClient) Script

func (c *SSHClient) Script(script string) *remoteScript

run a script on a client

func (*SSHClient) ScriptFile

func (c *SSHClient) ScriptFile(fname string) *remoteScript

run the given script file on a client

func (*SSHClient) Shell

func (c *SSHClient) Shell() *remoteShell

create a noninteractive shell on client

func (*SSHClient) Terminal

func (c *SSHClient) Terminal(config *SSHTerminalConfig) *remoteShell

create an interactive shell on client

type SSHTerminalConfig

type SSHTerminalConfig struct {
	Term   string
	Height int
	Weight int
	Modes  ssh.TerminalModes
}

type TaskDispatcher

type TaskDispatcher struct {
	// contains filtered or unexported fields
}

func NewTaskDispatcher

func NewTaskDispatcher(bufferSize int, putTimeout time.Duration) *TaskDispatcher

func (*TaskDispatcher) RunTask

func (td *TaskDispatcher) RunTask(
	name string,
	taskFn func(inData interface{}) (outData interface{}, err error),
) *task

func (*TaskDispatcher) Start

func (td *TaskDispatcher) Start(numWorkers int)

func (*TaskDispatcher) Stop

func (td *TaskDispatcher) Stop()

Jump to

Keyboard shortcuts

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