utils

package
v0.0.0-...-877fc11 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: Apache-2.0 Imports: 32 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 AsBool

func AsBool(b *bool, def ...bool) bool

func BoolP

func BoolP[T ~bool](b T) *bool

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 DefaultedFileSystem

func DefaultedFileSystem(def vfs.FileSystem, fss ...vfs.FileSystem) vfs.FileSystem

func FileSystem

func FileSystem(fss ...vfs.FileSystem) vfs.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 GetFileExtensionFromUrl

func GetFileExtensionFromUrl(url string) (string, error)

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 IsNil

func IsNil(o interface{}) bool

func JoinIndentLines

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

func Key

func Key(keyProvider interface{}) (string, error)

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 MapKeys

func MapKeys[K comparable, E any](m map[K]E) []K

func Must

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

Must expect 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 arg is given the given default is returned.

func ParseDeltaTime

func ParseDeltaTime(s string, past bool) (time.Time, error)

ParseDeltaTime parses a time diff relative to the actual time and returns the resulting time.

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 ReadFile

func ReadFile(in string, fss ...vfs.FileSystem) ([]byte, error)

func ResolveData

func ResolveData(in string, fss ...vfs.FileSystem) ([]byte, error)

func ResolvePath

func ResolvePath(path string) (string, error)

ResolvePath handles the ~ notation for the home directory.

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 Sort

func Sort[K Comparable[K]](a []K)

func SortedMapKeys

func SortedMapKeys[K ComparableMapKey[K], E any](m map[K]E) []K

func SplitLocator

func SplitLocator(locator string) (string, string, string)

func StringMapKeys

func StringMapKeys[K ~string, E any](m map[K]E) []K

func Unwrap

func Unwrap(o interface{}) interface{}

func UnwrappingCall

func UnwrappingCall[R any, I any](o interface{}, f func(I) R) R

func UnwrappingCast

func UnwrappingCast[I interface{}](o interface{}) I

func ValidateObject

func ValidateObject(o interface{}) error

ValidateObject checks whether an object is in error state. If yes, an appropriate error is returned.

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 Comparable

type Comparable[K any] interface {
	Compare(o K) int
}

type ComparableMapKey

type ComparableMapKey[K any] interface {
	Comparable[K]
	comparable
}

type DescriptionProvider

type DescriptionProvider interface {
	GetDescription() string
}

type KeyInfo

type KeyInfo interface {
	DescriptionProvider
	GetKey() string
}

type KeyProvider

type KeyProvider interface {
	Key() (string, error)
}

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()

type Unwrappable

type Unwrappable interface {
	Unwrap() interface{}
}

type Validatable

type Validatable interface {
	Validate() error
}

Validatable is an optional interface for DataAccess implementations or any other object, which might reach an error state. The error can then be queried with the method Validatable.Validate. This is used to support objects with access methods not returning an error. If the object is not valid, those methods return an unknown/default state, but the object should be queryable for its state.

Directories

Path Synopsis
Package blobaccess provides the basic set of the most commonly needed BlobAccess implementations.
Package blobaccess provides the basic set of the most commonly needed BlobAccess implementations.
bpi
flagsets/flagsetscheme
Package flagsetscheme provides a runtime.TypeScheme with support for command line option sets for the described object types.
Package flagsetscheme provides a runtime.TypeScheme with support for command line option sets for the described object types.
Package refmgmt provides a simple wrapper, which can be used to map a closable object type into an interface supporting reference counting and supporting a Dup() method.
Package refmgmt provides a simple wrapper, which can be used to map a closable object type into an interface supporting reference counting and supporting a Dup() method.
finalized
Package finalized provided a view management for a backend object, which is based on Go Garbage Collection and runtime finalizers.
Package finalized provided a view management for a backend object, which is based on Go Garbage Collection and runtime finalizers.
resource
Package resource provides support to implement closeable backing resources featuring multiple separately closeable references.
Package resource provides support to implement closeable backing resources featuring multiple separately closeable references.
Package registrations provides a hierarchical namespace for denoting any kind of handlers to be registered on some target.
Package registrations provides a hierarchical namespace for denoting any kind of handlers to be registered on some target.

Jump to

Keyboard shortcuts

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