Documentation
¶
Overview ¶
Package format contains utilities for formatting and printing output, including support for spinners, colored YAML/JSON, and resource stripping.
Index ¶
- Constants
- func Failuref(icon, format string, a ...any) string
- func IsInteractiveEnvironment(out io.Writer) bool
- func IsTerminal(stream any) bool
- func Object(obj client.Object) string
- func PrettyPrintObject(obj any, opts PrintOpts) error
- func PrettyPrintObjects[T any](objs []T, opts PrintOpts) error
- func Progress(icon, message string) string
- func Progressf(icon, format string, a ...any) string
- func WriteBase64Decoded(out io.Writer, s string) error
- type IOStreams
- type JSONOutputOptions
- type OutputFormatType
- type PrintOpts
- type Reader
- type Writer
- func (w *Writer) BeforeApply(streams IOStreams) error
- func (w *Writer) Confirm(reader Reader, message string) (bool, error)
- func (w *Writer) Err() io.Writer
- func (w *Writer) Failuref(icon string, format string, a ...any)
- func (w *Writer) Info(icon string, message string)
- func (w *Writer) Infof(icon string, format string, a ...any)
- func (w *Writer) Out() io.Writer
- func (w *Writer) Printf(format string, a ...any)
- func (w *Writer) Println(a ...any)
- func (w *Writer) Spinner(message, stopMessage string) (*yacspin.Spinner, error)
- func (w *Writer) Success(icon string, message string)
- func (w *Writer) Successf(icon string, format string, a ...any)
- func (w *Writer) Warningf(format string, a ...any)
- func (w Writer) Write(p []byte) (int, error)
Constants ¶
const ( SuccessChar = "✓" FailureChar = "✗" InfoChar = "ℹ" OutputFormatTypeYAML OutputFormatType = 0 OutputFormatTypeJSON OutputFormatType = 1 )
Variables ¶
This section is empty.
Functions ¶
func IsInteractiveEnvironment ¶ added in v1.8.2
func IsTerminal ¶ added in v1.21.0
IsTerminal reports whether stream, a reader or writer, is a terminal. Only streams exposing their file descriptor, such as an os.File, can be one, so wrapping a stream hides its terminal.
func PrettyPrintObject ¶ added in v1.2.2
PrettyPrintObject prints the supplied object in "pretty" colored yaml with some metadata, status and other default fields stripped out.
func PrettyPrintObjects ¶
PrettyPrintObjects prints the supplied objects in "pretty" colored yaml with some metadata, status and other default fields stripped out. If multiple objects are supplied, they will be divided with a yaml divider.
func Progress ¶ added in v1.14.0
Progress is a formatted message for use with a spinner.Suffix. An icon can be added which is displayed at the end of the message.
func Progressf ¶ added in v1.14.0
Progressf is a formatted message for use with a spinner.Suffix. An icon can be added which is displayed at the end of the message.
func WriteBase64Decoded ¶ added in v1.21.0
WriteBase64Decoded decodes the base64 encoded string s and writes the decoded content, followed by a newline, to out. Surrounding whitespace of both the encoded and the decoded content is dropped. An empty s writes nothing.
Types ¶
type IOStreams ¶ added in v1.21.0
type IOStreams struct {
// In is the input commands prompt from.
In io.Reader
// Out receives the output of a command.
Out io.Writer
// ErrOut receives diagnostics, see [Writer.Err].
ErrOut io.Writer
}
IOStreams are the standard input, output and error of a command.
Kong binds them for the BeforeApply hooks of Writer and Reader, so that every command reads and writes through the streams passed to the CLI instead of the ones of the process.
This is built after k8s.io/cli-runtime/pkg/genericiooptions.IOStreams.
type JSONOutputOptions ¶ added in v1.9.0
type JSONOutputOptions struct {
// PrintSingleItem will print a single item of an array as is
// (without the array notation)
PrintSingleItem bool
}
type OutputFormatType ¶ added in v1.9.0
type OutputFormatType int
type PrintOpts ¶
type PrintOpts struct {
// Out will be used to print to if set instead of stdout.
Out io.Writer
// ExcludeAdditional allows to exclude more fields of the object
ExcludeAdditional [][]string
// format type of the output, e.g. yaml or json
Format OutputFormatType
// JSONOpts defines special options for JSON output
JSONOpts JSONOutputOptions
// AllFields prints all fields of the object.
AllFields bool
}
PrintOpts customizes the printing.
type Reader ¶ added in v1.14.0
Reader is a wrapper around an io.Reader.
func (*Reader) BeforeApply ¶ added in v1.14.0
BeforeApply ensures that Kong initializes the Reader with the input of the bound IOStreams. It is subject to the same export requirement as Writer.BeforeApply.
func (*Reader) In ¶ added in v1.21.0
In returns the stream the Reader reads from. It is the stream itself rather than a wrapper, so that a terminal stays detectable and a subprocess inherits it, unlike the Reader, which never is a terminal. It is nil if the receiver or its stream is nil.
func (Reader) Read ¶ added in v1.19.0
Read implements io.Reader. It shadows the promoted method of the embedded io.Reader so that reading from an uninitialized Reader returns io.EOF instead of panicking. The receiver is a value so that Reader keeps implementing io.Reader when passed around by value.
type Writer ¶ added in v1.14.0
Writer is a wrapper around an io.Writer that provides helper methods for printing formatted messages.
func NewWriter ¶ added in v1.14.0
NewWriter returns a new Writer writing the output of a command to out and diagnostics to errOut.
func (*Writer) BeforeApply ¶ added in v1.14.0
BeforeApply ensures that Kong initializes the writer with the output and error of the bound IOStreams.
Kong discovers hook methods by descending into the embedded fields of the selected command, and it only descends into fields that are exported. A Writer embedded in an unexported base command struct is therefore never initialized, and every message printed through it is silently discarded. Command structs that embed a Writer, and every struct that embeds those in turn, must consequently be exported.
func (*Writer) Confirm ¶ added in v1.14.0
Confirm prints a confirm dialog using the supplied message and then waits until prompt is confirmed or denied. Only y and yes are accepted for confirmation. The prompt is written to Writer.Err, so that it is shown even if the output is redirected.
func (*Writer) Err ¶ added in v1.21.0
Err returns the writer for diagnostics, such as warnings, progress and the output of subprocesses, which must not mix with the output of a command. Like Writer.Out, it is the stream itself rather than a wrapper. It is io.Discard if the receiver or its error writer is nil.
func (*Writer) Failuref ¶ added in v1.14.0
Failuref is a formatted message for indicating a failure.
func (*Writer) Out ¶ added in v1.21.0
Out returns the stream the Writer writes the output of a command to. It is the stream itself rather than a wrapper, so that a terminal stays detectable and a subprocess inherits it, unlike the Writer, which never is a terminal. It is nil if the receiver or its stream is nil.
func (*Writer) Printf ¶ added in v1.14.0
Printf formats according to a format specifier and writes to the underlying writer. Prefer the dedicated methods if applicable.
func (*Writer) Println ¶ added in v1.14.0
Println prints a formatted message to the underlying writer. Prefer the dedicated methods if applicable.
func (*Writer) Spinner ¶ added in v1.14.0
Spinner creates a new spinner with the given message and stop message. As progress, it writes to Writer.Err.
func (*Writer) Success ¶ added in v1.14.0
Success returns a message for indicating a successful step.
func (*Writer) Successf ¶ added in v1.14.0
Successf is a formatted message for indicating a successful step.
func (*Writer) Warningf ¶ added in v1.14.0
Warningf is a formatted message for indicating a warning. As a diagnostic, it writes to Writer.Err.
func (Writer) Write ¶ added in v1.19.0
Write implements io.Writer. It shadows the promoted method of the embedded io.Writer so that writing to an uninitialized Writer discards the output instead of panicking. The receiver is a value so that Writer keeps implementing io.Writer when passed around by value.