utils

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2023 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BYTE = 1.0 << (10 * iota)
	KIBIBYTE
	MEBIBYTE
	GIBIBYTE
)

Variables

This section is empty.

Functions

func AddLogContextValue

func AddLogContextValue(ctx context.Context, key string, value interface{}) context.Context

AddLogContextValue adds a key value pair to the logging context. If none is defined it will be added.

func BytesString

func BytesString(bytes uint64, accuracy int) string

BytesString converts bytes into a human-readable string. This function is inspired by https://www.reddit.com/r/golang/comments/8micn7/review_bytes_to_human_readable_format/

func CTFComponentArchiveFilename

func CTFComponentArchiveFilename(name, version string) string

CTFComponentArchiveFilename returns the name of the component archive file in the ctf.

func CleanMarkdownUsageFunc

func CleanMarkdownUsageFunc(cmd *cobra.Command)

CleanMarkdownUsageFunc removes Markdown tags from the long usage of the command. With this func it is possible to generate the Markdown docs but still have readable commandline help func. Note: currently only "<pre>" tags are removed.

func ExtractTarToFs

func ExtractTarToFs(fs vfs.FileSystem, in io.Reader) error

ExtractTarToFs writes a tar stream to a filesystem.

func FormatKey

func FormatKey(k string) string

func FormatList

func FormatList(def string, elems ...KeyInfo) string

func FormatMap

func FormatMap[T DescriptionProvider](def string, elems map[string]T) string

func GetFileType

func GetFileType(fs vfs.FileSystem, path string) (string, error)

GetFileType returns the mimetype of a file.

func GetOptionFlag

func GetOptionFlag(list ...bool) bool

GetOptionFlag returns the flag value used to set a bool option based on optionally specified explicit value(s). The default value is to enable the option (true).

func Gzip

func Gzip(data []byte, compressionLevel int) ([]byte, error)

Gzip applies gzip compression to an arbitrary byte slice.

func IgnoreError

func IgnoreError(_ error)

func IndentLines

func IndentLines(orig string, gap string, skipfirst ...bool) string

func JoinIndentLines

func JoinIndentLines(orig []string, gap string, skipfirst ...bool) string

func LoggerWithContextContextValues

func LoggerWithContextContextValues(ctx context.Context, delegate logr.Logger) logr.Logger

LoggerWithContextContextValues creates a new context logger that delegates the actual requests to the delegate but injects the context log values.

func Must

func Must[T any](o T, err error) T

Must expects a result to be provided without error.

func Optional

func Optional[T any](list ...T) T

Optional returns the first optional non-zero element given as variadic argument, if given, or the zero element as default.

func OptionalDefaulted

func OptionalDefaulted[T any](def T, list ...T) T

OptionalDefaulted returns the first optional non-nil element given as variadic argument, or the given default element. For value types a given zero argument is excepted, also.

func OptionalDefaultedBool

func OptionalDefaultedBool(def bool, list ...bool) bool

OptionalDefaultedBool checks all args for true. If no true is given the given default is returned.

func ParseURL

func ParseURL(urlToParse string) (*url.URL, error)

func PrintPrettyYaml

func PrintPrettyYaml(obj interface{}, enabled bool)

PrintPrettyYaml prints the given objects as yaml if enabled.

func RandomString

func RandomString(n int) string

RandomString creates a new random string with the given length.

func RawJSON

func RawJSON(value interface{}) (*json.RawMessage, error)

RawJSON converts an arbitrary value to json.RawMessage.

func SafeConvert

func SafeConvert(bytes []byte) string

SafeConvert converts a byte slice to string. If the byte slice is nil, an empty string is returned.

func StringMapKeys

func StringMapKeys[E any](m map[string]E) []string

func WriteFileToTARArchive

func WriteFileToTARArchive(filename string, contentReader io.Reader, archiveWriter *tar.Writer) error

WriteFileToTARArchive writes a new file with name=filename and content=contentReader to archiveWriter.

Types

type DescriptionProvider

type DescriptionProvider interface {
	GetDescription() string
}

type Finalizer

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

Finalizer gathers finalization functions and calls them by calling the Finalize method(s). Add and Finalize may be called in any sequence and number. Finalize just calls the aggregated functions between its last and the actual call. This way it can be used together with defer to clean up stuff when leaving a function and combine it with controlled intermediate cleanup needed, for example as part of a loop block.

func (*Finalizer) Close

func (f *Finalizer) Close(c io.Closer) *Finalizer

Close will finalize the given object by calling its Close function when the finalizer is finalized.

func (*Finalizer) Finalize

func (f *Finalizer) Finalize() error

Finalize calls all finalizations in the reverse order of their registration.

func (*Finalizer) FinalizeWithErrorPropagation

func (f *Finalizer) FinalizeWithErrorPropagation(efferr *error)

FinalizeWithErrorPropagation calls all finalizations in the reverse order of their registration and propagates a potential error to the given error variable incorporating an already existing error. This is especially intended to be used in a deferred mode to adapt the error code of a function to incorporate finalization errors.

func (*Finalizer) FinalizeWithErrorPropagationf

func (f *Finalizer) FinalizeWithErrorPropagationf(efferr *error, msg string, args ...interface{})

FinalizeWithErrorPropagationf calls all finalizations in the reverse order of their registration and propagates a potential error to the given error variable incorporating an already existing error. This is especially intended to be used in a deferred mode to adapt the error code of a function to incorporate finalization errors. The final error will be wrapped by the given common context.

func (*Finalizer) Include

func (f *Finalizer) Include(fi *Finalizer) *Finalizer

Include includes the finalization of a given finalizer.

func (*Finalizer) Length

func (f *Finalizer) Length() int

func (*Finalizer) Lock

func (f *Finalizer) Lock(locker sync.Locker) *Finalizer

Lock locks a given Locker and unlocks it again during finalization.

func (*Finalizer) Nested

func (f *Finalizer) Nested() *Finalizer

Nested returns a linked finalizer usable in a nested block, which can be separately finalized. It is intended for sequential use, for example in a for loop. Successive calls will provide the same finalizer. The nested finalizer SHOULD be finalized at the end of its scope before it is requested, again, for the next nested usage.

func (*Finalizer) New

func (f *Finalizer) New() *Finalizer

New return a new finalizer included in the actual one.

func (*Finalizer) With

func (f *Finalizer) With(fi func() error) *Finalizer

func (*Finalizer) WithVoid

func (f *Finalizer) WithVoid(fi func()) *Finalizer

WithVoid registers a simple function to be called on finalization.

type KeyInfo

type KeyInfo interface {
	DescriptionProvider
	GetKey() string
}

type LogContextValues

type LogContextValues map[string]interface{}

LogContextValues describes the context values.

func ContextWithLogContextValues

func ContextWithLogContextValues(parent context.Context) (context.Context, *LogContextValues)

func LogContextValuesFromContext

func LogContextValuesFromContext(ctx context.Context) *LogContextValues

LogContextValuesFromContext returns the context values of a ctx. If nothing is defined nil is returned.

type StringSet

type StringSet map[string]struct{}

func (StringSet) Add

func (s StringSet) Add(a string) bool

func (StringSet) Contains

func (s StringSet) Contains(a string) bool

func (StringSet) Remove

func (s StringSet) Remove(a string) bool

type StringSlice

type StringSlice []string

func (*StringSlice) Add

func (l *StringSlice) Add(list ...string)

func (StringSlice) Contains

func (l StringSlice) Contains(s string) bool

func (*StringSlice) Delete

func (l *StringSlice) Delete(i int)

func (StringSlice) Sort

func (l StringSlice) Sort()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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