feedback

package
v1.5.19 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package feedback provides an uniform API that can be used to print feedback to the users in different formats.

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 ExitWhenParentProcessEnds

func ExitWhenParentProcessEnds()

ExitWhenParentProcessEnds waits until the controlling parent process ends and then exits the current process. This is useful to terminate the current process when it is daemonized and the controlling parent process is terminated to avoid leaving zombie processes. It is recommended to call this function as a goroutine.

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 InputUserField

func InputUserField(prompt string, secret bool) (string, error)

InputUserField prompts the user to input the provided user field.

func InteractiveStreams

func InteractiveStreams() (io.Reader, io.Writer, error)

InteractiveStreams returns the underlying io.Reader and io.Writer to directly stream to stdin and stdout. It errors if the selected output format is not Text or the terminal is not interactive.

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 NewCommand

func NewCommand() *cobra.Command

NewCommand creates a new `feedback` command

func NewDownloadProgressBarCB

func NewDownloadProgressBarCB() func(*rpc.DownloadProgress)

NewDownloadProgressBarCB creates a progress bar callback that outputs a progress bar on the terminal

func NewTaskProgressCB

func NewTaskProgressCB() func(curr *rpc.TaskProgress)

NewTaskProgressCB returns a commands.TaskProgressCB progress listener that outputs to terminal

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 ProgressBar

func ProgressBar() rpc.DownloadProgressCB

ProgressBar returns a DownloadProgressCB that prints a progress bar.

func RestoreModeStdin

func RestoreModeStdin()

RestoreModeStdin restore the terminal settings to the normal non-RAW state. This function must be called after SetRawModeStdin to not leave the terminal in an undefined state.

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 SetRawModeStdin

func SetRawModeStdin()

SetRawModeStdin sets the stdin stream in RAW mode (no buffering, echo disabled, no terminal escape codes nor signals interpreted)

func TaskProgress

func TaskProgress() rpc.TaskProgressCB

TaskProgress returns a TaskProgressCB that prints the task progress.

func Warning

func Warning(msg string)

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
	// YAML format
	YAML
)

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