utils

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOutputFormats = []OutputFormat{OUTPUT_JSON, OUTPUT_TEXT, OUTPUT_YAML}

Functions

func AddOutputFlag

func AddOutputFlag(fs *pflag.FlagSet, outputVar *OutputFormat, defaultOutput OutputFormat, validOutputs ...OutputFormat)

AddOutputFlag adds a --output/-o flag to the given FlagSet, binding the result to the given string variable.

func Fatal

func Fatal(code int, format string, values ...any)

Fatal prints the given error message to stderr and then exits with the given exit code.

func FilterSlice

func FilterSlice[X any](data []X, predicate func(X) bool) []X

FilterSlice takes a slice and filters it using the given predicate function. Does not modify the original slice, but values are not deep-copied, so changes to the values might influence the original slice, depending on the type.

func GetCurrentApiserverHost

func GetCurrentApiserverHost(kcfg *clientcmdapi.Config) (string, error)

GetCurrentApiserverHost returns the current apiserver host from the given kubeconfig.

func IsRequirementError

func IsRequirementError(err error) bool

func MarshalKubeconfig

func MarshalKubeconfig(kcfg *clientcmdapi.Config) ([]byte, error)

MarshalKubeconfig returns the yaml representation of the given kubeconfig.

func NaturalLanguageJoin

func NaturalLanguageJoin(data []string, separator string, oxfordComma bool) string

NaturalLanguageJoin works like strings.Join, but it separates the last element via ' and ' instead of comma. For exactly two elements, the result is '<elem> and <elem>'.

func ParseKubeconfig

func ParseKubeconfig(data []byte) (*clientcmdapi.Config, error)

ParseKubeconfig parses the given data as a kubeconfig and returns the parsed config.

func ParseKubeconfigFromFile

func ParseKubeconfigFromFile(path string) (*clientcmdapi.Config, error)

ParseKubeconfigFromFile reads the given file and parses it as kubeconfig.

func ParseKubeconfigFromFileWithClient

func ParseKubeconfigFromFileWithClient(path string) (*clientcmdapi.Config, client.Client, error)

ParseKubeconfigFromFileWithClient works like ParseKubeconfigFromFile, but it returns a client.Client in addition to the parsed kubeconfig.

func ParseKubeconfigWithClient

func ParseKubeconfigWithClient(data []byte) (*clientcmdapi.Config, client.Client, error)

ParseKubeconfigWithClient works like ParseKubeconfig, but it returns a client.Client in addition to the parsed kubeconfig.

func Project

func Project[X any, Y any](data []X, project func(X) Y) []Y

Project takes a list and converts/projects each element of the list to another type using the given project function.

func PromptForConfirmation

func PromptForConfirmation(prompt string, newline bool) bool

PromptForConfirmation prompts the user for confirmation.

func StringPadding

func StringPadding(s string, l int) string

StringPadding returns the given string filled with spaces to the given length. If the string already has the required or a greater length, it is returned unmodified.

func UnknownOutputFatal

func UnknownOutputFatal(output OutputFormat)

UnknownOutputFatal prints an error message that the specified output format is not known and then exits with status 1.

func ValidateOutputFormat

func ValidateOutputFormat(output OutputFormat, validOutputs ...OutputFormat)

ValidateOutputFormat verifies that a valid output format was specified and exits with error otherwise.

Types

type OutputFormat

type OutputFormat string
const (
	OUTPUT_TEXT OutputFormat = "text"
	OUTPUT_YAML OutputFormat = "yaml"
	OUTPUT_JSON OutputFormat = "json"
)

func (*OutputFormat) Set

func (o *OutputFormat) Set(val string) error

Set implements pflag.Value

func (*OutputFormat) String

func (o *OutputFormat) String() string

String implements pflag.Value

func (*OutputFormat) Type

func (o *OutputFormat) Type() string

Type implements pflag.Value

type Requirement

type Requirement struct {
	Satisfy func() error
	// contains filtered or unexported fields
}

Requirement represents a requirement, consisting of a 'Satisfy' method and a boolean to check whether it has been called.

func (*Requirement) IsSatisfied

func (r *Requirement) IsSatisfied() bool

IsSatisfied returns whether the requirement is satisfied (= its 'Satisfy' method has been called before)

type RequirementError

type RequirementError struct {
	// Requirement is the name of the requirement which caused the error
	Requirement string
	// Error is the error
	Err error
}

func NewRequirementError

func NewRequirementError(req string, err error) RequirementError

func (RequirementError) Error

func (re RequirementError) Error() string

type Requirements

type Requirements map[string]*Requirement

Requirements is a helper struct for object methods which require other methods to have been called before.

func NewRequirements

func NewRequirements() Requirements

NewRequirements returns a new Requirements object.

func (Requirements) HasRequirement

func (rs Requirements) HasRequirement(id string) bool

HasRequirement returns whether a requirement with the given ID has been registered.

func (Requirements) IsSatisfied

func (rs Requirements) IsSatisfied(id string) bool

IsSatisfied returns true if a requirement with the given ID has been registered and satisfied.

func (Requirements) Register

func (rs Requirements) Register(id string, satisfy func() error)

Register registers a new requirement. It takes an identifier and a function which, when called, will satisfy the requirement.

func (Requirements) Require

func (rs Requirements) Require(ids ...string) error

Require checks if all referenced requirements have been satisfied. It returns an error if any of the given IDs doesn't match a requirement which has been registered before. For all requirements, it is checked whether the requirement is already satisfied.

If not, its 'Satisfy' method is called.
If 'Satisfy' returns without errors, the requirement is marked as satisfied,
otherwise the error is returned without checking further requirements.

func (Requirements) SetSatisfied

func (rs Requirements) SetSatisfied(id string, satisfied bool) error

SetSatisfied allows to manually satisfy (or 'unsatisfy') a requirement.

type Table

type Table[T any] struct {
	// contains filtered or unexported fields
}

Table is a helper struct to format output as table.

func NewOutputTable

func NewOutputTable[T any]() *Table[T]

NewOutputTable returns a new Table for constructing output formatted as a table.

func (*Table[T]) String

func (t *Table[T]) String() string

func (*Table[T]) WithColumn

func (t *Table[T]) WithColumn(name string, getValue func(obj T) string) *Table[T]

WithColumn adds a new column with the given name and getter function to the table.

func (*Table[T]) WithData

func (t *Table[T]) WithData(obj ...T) *Table[T]

WithData adds a one or more new rows to the table.

type WriteBuffer

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

func NewWriteBuffer

func NewWriteBuffer() *WriteBuffer

func (*WriteBuffer) Data

func (w *WriteBuffer) Data() []byte

func (*WriteBuffer) Flush

func (w *WriteBuffer) Flush(target io.Writer, replacements ...string) error

Flush writes the buffer to the target writer and resets it. If replacements are provided, all occurrences of the first element in each pair are replaced with the second element. In case of an uneven number of replacements, the last replacement is ignored.

func (*WriteBuffer) FlushToString

func (w *WriteBuffer) FlushToString() string

func (*WriteBuffer) Write

func (w *WriteBuffer) Write(p []byte) (n int, err error)

Write implements io.Writer.

Jump to

Keyboard shortcuts

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