utils

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidKeyLength = errors.New("invalid key length")
)

Functions

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 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 ParseIPNet added in v1.0.0

func ParseIPNet(entry string) (*net.IPNet, 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)

Jump to

Keyboard shortcuts

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