utils

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2021 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidKeyLength = errors.New("invalid key length")
)
View Source
var (
	ErrMergedContextCanceled = errors.New("merged context canceled")
)

Functions

func CloseFileAndRename added in v1.1.0

func CloseFileAndRename(fileDescriptor *os.File, sourceFilePath string, targetFilePath string) error

CloseFileAndRename closes the file descriptor and renames the file.

func CreateTempFile added in v1.1.0

func CreateTempFile(filePath string) (*os.File, string, error)

CreateTempFile creates a file descriptor with _tmp as file extension.

func DirectoryEmpty added in v1.1.0

func DirectoryEmpty(dirPath string) (bool, error)

DirectoryEmpty returns whether the given directory is empty.

func EstimateRemainingTime added in v0.5.0

func EstimateRemainingTime(timeStart time.Time, current int64, total int64) (percentage float64, remaining time.Duration)

EstimateRemainingTime estimates the remaining time for a running operation and returns the finished percentage.

func FolderSize added in v1.0.5

func FolderSize(target string) (int64, error)

FolderSize returns the size of a folder.

func LoadEd25519PrivateKeysFromEnvironment added in v1.0.0

func LoadEd25519PrivateKeysFromEnvironment(name string) ([]ed25519.PrivateKey, error)

LoadEd25519PrivateKeysFromEnvironment loads ed25519 private keys from the given environment variable.

func LoadStringFromEnvironment added in v1.0.0

func LoadStringFromEnvironment(name string) (string, error)

LoadStringFromEnvironment loads a string from the given environment variable.

func MergeContexts added in v1.1.0

func MergeContexts(ctxPrimary context.Context, ctxSecondary context.Context) (context.Context, context.CancelFunc)

MergeContexts creates a new mergedContext based on two contexts.

func ParseEd25519PrivateKeyFromString added in v1.0.0

func ParseEd25519PrivateKeyFromString(key string) (ed25519.PrivateKey, error)

func ParseEd25519PublicKeyFromString added in v1.0.0

func ParseEd25519PublicKeyFromString(key string) (ed25519.PublicKey, error)

func RandomInsecure

func RandomInsecure(min int, max int) int

RandomInsecure returns a random int in the range of min to max. the result is not cryptographically secure. RandomInsecure is inclusive max value.

func ReadFromFile added in v1.0.0

func ReadFromFile(filename string, data interface{}) error

ReadFromFile reads structured binary data from the file named by filename to data. A successful call returns err == nil, not err == EOF. ReadFromFile uses binary.Read to decode data. Data must be a pointer to a fixed-size value or a slice of fixed-size values.

func ReadJSONFromFile added in v1.0.0

func ReadJSONFromFile(filename string, data interface{}) error

ReadJSONFromFile reads JSON data from the file named by filename to data. ReadJSONFromFile uses json.Unmarshal to decode data. Data must be a pointer to a fixed-size value or a slice of fixed-size values.

func ReadTOMLFromFile added in v1.0.5

func ReadTOMLFromFile(filename string, data interface{}) error

ReadTOMLFromFile reads TOML data from the file named by filename to data. ReadTOMLFromFile uses toml.Unmarshal to decode data. Data must be a pointer to a fixed-size value or a slice of fixed-size values.

func ReturnErrIfCtxDone added in v1.0.0

func ReturnErrIfCtxDone(ctx context.Context, err error) error

ReturnErrIfCtxDone returns the given error if the provided context is done.

func Uint32Diff added in v1.0.5

func Uint32Diff(newCount uint32, oldCount uint32) uint32

Uint32Diff returns the difference between newCount and oldCount and catches overflows

func WaitForChannelClosed added in v1.0.0

func WaitForChannelClosed(ctx context.Context, ch chan struct{}) error

WaitForChannelClosed waits until the channel is closed or the context is done. If the context was done, the event should be manually deregistered afterwards to clean up memory.

func WriteJSONToFile added in v1.0.0

func WriteJSONToFile(filename string, data interface{}, perm os.FileMode) (err error)

WriteJSONToFile writes the JSON representation of data to a file named by filename. If the file does not exist, WriteJSONToFile creates it with permissions perm (before umask); otherwise WriteJSONToFile truncates it before writing, without changing permissions. WriteJSONToFile uses json.MarshalIndent to encode data. Data must be a pointer to a fixed-size value or a slice of fixed-size values.

func WriteTOMLToFile added in v1.0.5

func WriteTOMLToFile(filename string, data interface{}, perm os.FileMode, header ...string) (err error)

WriteTOMLToFile writes the TOML representation of data to a file named by filename. If the file does not exist, WriteTOMLToFile creates it with permissions perm (before umask); otherwise WriteTOMLToFile truncates it before writing, without changing permissions. WriteTOMLToFile uses toml.Marshal to encode data. Data must be a pointer to a fixed-size value or a slice of fixed-size values. An additional header can be passed.

func WriteToFile added in v1.0.0

func WriteToFile(filename string, data interface{}, perm os.FileMode) (err error)

WriteToFile writes the binary representation of data to a file named by filename. If the file does not exist, WriteFile creates it with permissions perm (before umask); otherwise WriteFile truncates it before writing, without changing permissions. WriteToFile uses binary.Write to encode data. Data must be a pointer to a fixed-size value or a slice of fixed-size values.

Types

type SyncEvent added in v1.0.0

type SyncEvent struct {
	syncutils.RWMutex
	// contains filtered or unexported fields
}

func NewSyncEvent added in v1.0.0

func NewSyncEvent() *SyncEvent

func (*SyncEvent) DeregisterEvent added in v1.0.0

func (se *SyncEvent) DeregisterEvent(key interface{})

DeregisterEvent removes a registered event to free the memory if not used.

func (*SyncEvent) RegisterEvent added in v1.0.0

func (se *SyncEvent) RegisterEvent(key interface{}) chan struct{}

RegisterEvent creates a unique channel for the key which can be used to signal global events.

func (*SyncEvent) Trigger added in v1.0.0

func (se *SyncEvent) Trigger(key interface{})

type TimeHeap

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

TimeHeap implements a heap sorted by time, where older elements are popped during AveragePerSecond call.

func NewTimeHeap

func NewTimeHeap() *TimeHeap

NewTimeHeap creates a new TimeHeap object.

func (*TimeHeap) Add

func (h *TimeHeap) Add(count uint64)

Add a new entry to the container with a count for the average calculation.

func (*TimeHeap) AveragePerSecond added in v1.0.5

func (h *TimeHeap) AveragePerSecond(timeBefore time.Duration) float32

AveragePerSecond calculates the average per second of all entries in the given duration. older elements are removed from the container.

func (TimeHeap) Len

func (h TimeHeap) Len() int

func (TimeHeap) Less

func (h TimeHeap) Less(i, j int) bool

func (*TimeHeap) Pop

func (h *TimeHeap) Pop() interface{}

func (*TimeHeap) Push

func (h *TimeHeap) Push(x interface{})

func (TimeHeap) Swap

func (h TimeHeap) Swap(i, j int)

type WrappedLogger added in v1.1.0

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

WrappedLogger is a wrapper to call logging functions in case a logger was passed.

func NewWrappedLogger added in v1.1.0

func NewWrappedLogger(logger *logger.Logger) *WrappedLogger

NewWrappedLogger creates a new WrappedLogger.

func (*WrappedLogger) LogDebug added in v1.1.0

func (l *WrappedLogger) LogDebug(args ...interface{})

LogDebug uses fmt.Sprint to construct and log a message.

func (*WrappedLogger) LogDebugf added in v1.1.0

func (l *WrappedLogger) LogDebugf(template string, args ...interface{})

LogDebugf uses fmt.Sprintf to log a templated message.

func (*WrappedLogger) LogError added in v1.1.0

func (l *WrappedLogger) LogError(args ...interface{})

LogError uses fmt.Sprint to construct and log a message.

func (*WrappedLogger) LogErrorf added in v1.1.0

func (l *WrappedLogger) LogErrorf(template string, args ...interface{})

LogErrorf uses fmt.Sprintf to log a templated message.

func (*WrappedLogger) LogFatal added in v1.1.0

func (l *WrappedLogger) LogFatal(args ...interface{})

LogFatal uses fmt.Sprint to construct and log a message, then calls os.Exit.

func (*WrappedLogger) LogFatalf added in v1.1.0

func (l *WrappedLogger) LogFatalf(template string, args ...interface{})

LogFatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.

func (*WrappedLogger) LogInfo added in v1.1.0

func (l *WrappedLogger) LogInfo(args ...interface{})

LogInfo uses fmt.Sprint to construct and log a message.

func (*WrappedLogger) LogInfof added in v1.1.0

func (l *WrappedLogger) LogInfof(template string, args ...interface{})

LogInfof uses fmt.Sprintf to log a templated message.

func (*WrappedLogger) LogPanic added in v1.1.0

func (l *WrappedLogger) LogPanic(args ...interface{})

LogPanic uses fmt.Sprint to construct and log a message, then panics.

func (*WrappedLogger) LogPanicf added in v1.1.0

func (l *WrappedLogger) LogPanicf(template string, args ...interface{})

LogPanicf uses fmt.Sprintf to log a templated message, then panics.

func (*WrappedLogger) LogWarn added in v1.1.0

func (l *WrappedLogger) LogWarn(args ...interface{})

LogWarn uses fmt.Sprint to construct and log a message.

func (*WrappedLogger) LogWarnf added in v1.1.0

func (l *WrappedLogger) LogWarnf(template string, args ...interface{})

LogWarnf uses fmt.Sprintf to log a templated message.

func (*WrappedLogger) Logger added in v1.1.0

func (l *WrappedLogger) Logger() *logger.Logger

Logger return the underlying logger.

func (*WrappedLogger) LoggerNamed added in v1.1.0

func (l *WrappedLogger) LoggerNamed(name string) *logger.Logger

LoggerNamed adds a sub-scope to the logger's name. See Logger.Named for details.

Jump to

Keyboard shortcuts

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