validation

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRemediable

func IsRemediable(err error) bool

IsRemediable checks if an error has a remediation.

func NewRemediableErr

func NewRemediableErr(err, remediation string) error

NewRemediableErr returns a new Remediable error.

func Remediation

func Remediation(err error) string

Remediation returns the Remediation message for an error if it has it. Otherwise it returns an empty string.

func Unwrap

func Unwrap(err error) []error

Unwrap unfolds and flattens errors if err implements Unwrap []error. If it doesn't implement it, it just returns a slice with one single error.

func WithRemediation

func WithRemediation(err error, remediation string) error

WithRemediation makes an error Remediable.

Types

type ChannelReader

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

ChannelReader implements LineReader for a channel of strings source.

func NewChannelReader

func NewChannelReader(in <-chan string, name string) ChannelReader

NewChannelReader returns a new ChannelReader.

func (ChannelReader) Line

func (c ChannelReader) Line() (string, bool)

Line reads a line from the channel. Returns false if there is no more lines to read (in buffered channels) or the channel is closed.

func (ChannelReader) Name

func (c ChannelReader) Name() string

type Colorer

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

func (Colorer) Black

func (c Colorer) Black(m string) string

func (Colorer) Blue

func (c Colorer) Blue(m string) string

func (Colorer) Bold

func (c Colorer) Bold(m string) string

func (Colorer) Cyan

func (c Colorer) Cyan(m string) string

func (Colorer) Green

func (c Colorer) Green(m string) string

func (Colorer) Grey

func (c Colorer) Grey(m string) string

func (Colorer) Magenta

func (c Colorer) Magenta(m string) string

func (Colorer) Red

func (c Colorer) Red(m string) string

func (Colorer) Underline

func (c Colorer) Underline(m string) string

func (Colorer) Yellow

func (c Colorer) Yellow(m string) string

type FileCapture

type FileCapture struct {
	*os.File
	// contains filtered or unexported fields
}

FileCapture forwards the content of a file to a channel, reading line by line.

func NewFileCapture

func NewFileCapture(out chan<- string) *FileCapture

NewFileCapture returns a new FileCapture.

func (*FileCapture) Close

func (r *FileCapture) Close() error

Close closes all internal resources and stops routines.

func (*FileCapture) Init

func (r *FileCapture) Init() error

Init initializes the FileCapture. The caller is responsible for calling Close after Init succeeds once they are done with the FileCapture. If not, FileCapture might leak resources.

type Informer

type Informer interface {
	Starting(ctx context.Context, name, message string)
	Done(ctx context.Context, name string, err error)
}

type LineReader

type LineReader interface {
	// Line reads a line from the source. It returns false for the second argument
	// if there is no more lines to read.
	Line() (string, bool)
	// Name is name of the lines source.
	Name() string
}

LineReader is an interface that allows to read lines from some source.

type NoOpInformer

type NoOpInformer struct{}

func (NoOpInformer) Done

func (NoOpInformer) Done(ctx context.Context, name string, err error)

func (NoOpInformer) Starting

func (NoOpInformer) Starting(ctx context.Context, name, message string)

type Printer

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

Informer is an informer that prints the validation steps to stdout.

func NewPrinter

func NewPrinter(opts ...PrinterOpt) Printer

func (Printer) Done

func (p Printer) Done(ctx context.Context, name string, err error)

Done prints the result of a validation. If a validation fails, it will print the error, external output if any and the error remediation if the error is Remediable.

func (Printer) Starting

func (p Printer) Starting(ctx context.Context, name, message string)

Starting prints the starting message of a validation.

type PrinterOpt

type PrinterOpt func(*Printer)

PrinterOpt allows to configure the Printer.

func WithExternalLogs

func WithExternalLogs(in LineReader) PrinterOpt

WithExternalLogs allows to configure an external source of logs that will get included in the output when a validation fails. This is useful for correctly displaying the output of external processes that are invoked during the validation. You can just capture their output and let the printer handle its display.

func WithNoColor

func WithNoColor() PrinterOpt

WithNoColor disables color output.

func WithOutWriter

func WithOutWriter(out io.Writer) PrinterOpt

WithOutWriter allows to configure the output destination.

type PrinterWithStdCapture

type PrinterWithStdCapture struct {
	Printer
	FileCapture
}

PrinterWithStdCapture is a convenience struct that combines a Printer and a FileCapture, allowing to capture a os.File. Useful for capturing stderr or stdout and displaying it through the Printer.

func NewPrinterWithStdCapture

func NewPrinterWithStdCapture(stdName string, noColor bool) *PrinterWithStdCapture

NewPrinterWithStdCapture returns a new PrinterWithStdCapture.

type Remediable

type Remediable interface {
	Remediation() string
}

Remediable is an error that provides a possible remediation.

type Runner

type Runner[O Validatable[O]] struct {
	// contains filtered or unexported fields
}

Runner allows to compose and run validations.

func NewRunner

func NewRunner[O Validatable[O]](informer Informer, opts ...RunnerOpt) *Runner[O]

NewRunner constructs a new Runner.

func (*Runner[O]) Register

func (r *Runner[O]) Register(validations ...Validation[O])

Register adds validations to the Runner.

func (*Runner[O]) Sequentially

func (r *Runner[O]) Sequentially(ctx context.Context, obj O) error

Sequentially runs all validations one after the other and waits until they all finish, aggregating the errors if present. obj must not be modified. If it is, this indicates a programming error and the method will panic.

func (*Runner[O]) UntilError

func (r *Runner[O]) UntilError(validations ...Validation[O]) Validation[O]

type RunnerConfig

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

RunnerConfig holds the configuration for the Runner.

type RunnerOpt

type RunnerOpt func(*RunnerConfig)

RunnerOpt allows to configure the Runner.

func WithSkipValidations

func WithSkipValidations(namesToSkip ...string) RunnerOpt

WithSkipValidation configures the runner to skip the validations with the given names.

type Validatable

type Validatable[O any] interface {
	DeepCopy() O
}

Validatable is anything that can be validated.

type Validate

type Validate[O Validatable[O]] func(ctx context.Context, informer Informer, obj O) error

Validate is the logic for a validation of a type O.

func UntilError

func UntilError[O Validatable[O]](validates ...Validate[O]) Validate[O]

UntilError returns a composed validate that runs all validations until one fails.

type Validation

type Validation[O Validatable[O]] struct {
	Name     string
	Validate Validate[O]
}

Validation validates the system for a type O.

func New

func New[O Validatable[O]](name string, validate Validate[O]) Validation[O]

Jump to

Keyboard shortcuts

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