feedback

package
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2025 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirectStreams

func DirectStreams() (io.Writer, io.Writer, error)

DirectStreams returns the underlying io.Writer to directly stream to stdout and stderr. If the selected output format is not Text, the function will error.

Using the streams returned by this function allows direct control of the output and the PrintResult function must not be used anymore

func Fatal

func Fatal(errorMsg string, exitCode ExitCode)

Fatal outputs the errorMsg and exits with status exitCode.

func FatalError

func FatalError(err error, exitCode ExitCode)

FatalError outputs the error and exits with status exitCode.

func FatalResult

func FatalResult(res ErrorResult, exitCode ExitCode)

FatalResult outputs the result and exits with status exitCode.

func NewBufferedStreams

func NewBufferedStreams() (io.Writer, io.Writer, func() *OutputStreamsResult)

NewBufferedStreams returns a pair of io.Writer to buffer the command output. The returned writers will accumulate the output until the command execution is completed. The io.Writes will not affect other feedback streams.

This function returns also a callback that must be called when the command execution is completed, it will return an *OutputStreamsResult object that can be used as a Result or to retrieve the accumulated output to embed it in another object.

func OutputStreams

func OutputStreams() (io.Writer, io.Writer, func() *OutputStreamsResult)

OutputStreams returns a pair of io.Writer to write the command output. The returned writers will accumulate the output until the command execution is completed, so they are not suitable for printing an unbounded stream like a debug logger or an event watcher (use DirectStreams for that purpose).

If the output format is Text the output will be directly streamed to the underlying stdio streams in real time.

This function returns also a callback that must be called when the command execution is completed, it will return an *OutputStreamsResult object that can be used as a Result or to retrieve the accumulated output to embed it in another object.

func Print

func Print(v string)

Print behaves like fmt.Print but writes on the out writer and adds a newline.

func PrintResult

func PrintResult(res Result)

PrintResult is a convenient wrapper to provide feedback for complex data, where the contents can't be just serialized to JSON but requires more structure.

func Printf

func Printf(format string, v ...interface{})

Printf behaves like fmt.Printf but writes on the out writer and adds a newline.

func SetErr

func SetErr(err io.Writer)

SetErr can be used to change the err writer at runtime

func SetFormat

func SetFormat(f OutputFormat)

SetFormat can be used to change the output format at runtime

func SetOut

func SetOut(out io.Writer)

SetOut can be used to change the out writer at runtime

func Warnf

func Warnf(msg string, args ...interface{})

Warning outputs a warning message.

Types

type ErrorResult

type ErrorResult interface {
	Result
	ErrorString() string
}

ErrorResult is a result embedding also an error. In case of textual output the error will be printed on stderr.

type ExitCode

type ExitCode int

ExitCode to be used for Fatal.

const (
	// Success (0 is the no-error return code in Unix)
	Success ExitCode = iota

	// ErrGeneric Generic error (1 is the reserved "catchall" code in Unix)
	ErrGeneric

	// ErrNoConfigFile is returned when the config file is not found (3)
	ErrNoConfigFile

	// ErrNetwork is returned when a network error occurs (5)
	ErrNetwork

	// ErrCoreConfig represents an error in the cli core config, for example some basic
	// files shipped with the installation are missing, or cannot create or get basic
	// directories vital for the CLI to work. (6)
	ErrCoreConfig

	// ErrBadArgument is returned when the arguments are not valid (7)
	ErrBadArgument

	// ErrFailedToListenToTCPPort is returned if the CLI failed to open a TCP port
	// to listen for incoming connections (8)
	ErrFailedToListenToTCPPort

	// ErrBadTCPPortArgument is returned if the TCP port argument is not valid (9)
	ErrBadTCPPortArgument

	// ErrInitializingInventory is returned when the inventory cannot be initialized,
	// usually depends on a wrong configuration of the data dir (10)
	ErrInitializingInventory

	// ErrMissingProgrammer is returned when the programmer argument is missing (11)
	ErrMissingProgrammer
)

type OutputFormat

type OutputFormat int

OutputFormat is an output format

const (
	// Text is the plain text format, suitable for interactive terminals
	Text OutputFormat = iota
	// JSON format
	JSON
	// MinifiedJSON format
	MinifiedJSON
)

func GetFormat

func GetFormat() OutputFormat

GetFormat returns the output format currently set

func ParseOutputFormat

func ParseOutputFormat(in string) (OutputFormat, bool)

ParseOutputFormat parses a string and returns the corresponding OutputFormat. The boolean returned is true if the string was a valid OutputFormat.

func (OutputFormat) String

func (f OutputFormat) String() string

type OutputStreamsResult

type OutputStreamsResult struct {
	Stdout string `json:"stdout"`
	Stderr string `json:"stderr"`
}

OutputStreamsResult contains the accumulated stdout and stderr output when the selected output format is not Text.

func (*OutputStreamsResult) Data

func (r *OutputStreamsResult) Data() interface{}

Data returns the result object itself, it is used to implement the Result interface.

func (*OutputStreamsResult) Empty

func (r *OutputStreamsResult) Empty() bool

Empty returns true if both Stdout and Stderr are empty.

func (*OutputStreamsResult) String

func (r *OutputStreamsResult) String() string

type Result

type Result interface {
	fmt.Stringer
	Data() interface{}
}

Result is anything more complex than a sentence that needs to be printed for the user.

Jump to

Keyboard shortcuts

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