output

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package output provides functionality to write output in different formats.

Index

Constants

View Source
const (
	// JSONFormat is used to indicate that the output of a command should be in JSON format
	JSONFormat = "json"
	// JSONLFormat is used to indicate that the output of a command should be in JSON format using
	// new line delimited JSON.
	JSONLFormat = "jsonl"
	// YAMLFormat is used to indicate that the output of a command should be in YAML format
	YAMLFormat = "yaml"
	// TableFormat is used to indicate that the output of a command should be printed as a table
	TableFormat = "table"
)

Variables

View Source
var DeprecatedTextColor = color.New(color.FgRed)

DeprecatedTextColor is the color used for deprecation messages.

Functions

func Configv1ChangelogMessage

func Configv1ChangelogMessage(change *models.ResourceChange) string

Configv1ChangelogMessage creates a user readable changelog message.

func Deprecated

func Deprecated(cmd *cobra.Command, alternatives ...string)

Deprecated prints a deprecation message to the given command's stderr with a list of alternative commands.

Types

type ColorPrinter

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

ColorPrinter prints to a given io.Writer with color.

func NewColorPrinter

func NewColorPrinter(w io.Writer, c *color.Color) ColorPrinter

NewColorPrinter returns a printer that ensures all output is the given color.

func NewStderrPrinter

func NewStderrPrinter(cmd *cobra.Command) ColorPrinter

NewStderrPrinter returns a printer that ensures all output is the color blue.

func (ColorPrinter) Printf

func (c ColorPrinter) Printf(format string, a ...any) (n int, err error)

Printf prints a formatted string.

func (ColorPrinter) Println

func (c ColorPrinter) Println(a ...any) (n int, err error)

Println prints a line.

type FileOperation

type FileOperation interface {
	MkDirAll(string, os.FileMode) error
	OpenFile(string, int, os.FileMode) (io.WriteCloser, error)
}

FileOperation is an interface that abstracts the file operations for the various writer implementations of chronoctl

type FileOps

type FileOps struct{}

FileOps is the default implementation of FileOperation

func (FileOps) MkDirAll

func (FileOps) MkDirAll(path string, perm os.FileMode) error

MkDirAll makes all directories in a path

func (FileOps) OpenFile

func (FileOps) OpenFile(name string, flag int, perm os.FileMode) (io.WriteCloser, error)

OpenFile opens a file

type Flags

type Flags struct {
	// Format is which Format to use when writing output.
	Format string
	// Writers maps a Format to a specific writer, allowing commands to register
	// their own output Format, e.g. a table view.
	Writers map[string]ObjectWriter
	// OutputDirectory is the directory where the output is written to.
	OutputDirectory string
	// CreateFilePerObject indicates whether to create a file per object.
	CreateFilePerObject bool
	// contains filtered or unexported fields
}

Flags is a struct that contains the configuration for the output flags

func NewFlags

func NewFlags(opts ...OutputOption) *Flags

NewFlags returns a new Flags object with the given options applied.

func (*Flags) AddFlags

func (f *Flags) AddFlags(cmd *cobra.Command)

AddFlags adds the output flags to the given command.fr

func (Flags) Close

func (f Flags) Close(w io.Writer) error

Close closes the object writer stream.

func (Flags) NewWriterManager

func (f Flags) NewWriterManager(w io.Writer) (*WriterManager, error)

NewWriterManager returns a writer manager

func (*Flags) Validate

func (f *Flags) Validate() error

Validate validates the output flags.

func (*Flags) WithOutputOptions

func (f *Flags) WithOutputOptions(opts ...OutputOption)

WithOutputOptions allows late application of options.

func (Flags) WriteAfterClose

func (f Flags) WriteAfterClose(s string)

WriteAfterClose prints out final messages after closing our object writer stream. Use for this can be printing an additional command to be run.

func (Flags) WriteObject

func (f Flags) WriteObject(object interface{}, w io.Writer) error

WriteObject marshals and writes an object.

type JSONObjectWriter

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

JSONObjectWriter marshalls and writes JSON to an output stream. Not safe for concurrent use.

func NewJSONObjectWriter

func NewJSONObjectWriter(useArray bool) *JSONObjectWriter

NewJSONObjectWriter returns a new writer for JSON output

func (*JSONObjectWriter) Close

func (j *JSONObjectWriter) Close(w io.Writer, opts ObjectWriterOpts) error

Close closes the object writer stream and prints any final messages.

func (*JSONObjectWriter) WriteAfterClose

func (j *JSONObjectWriter) WriteAfterClose(s string)

WriteAfterClose sets a final message to print after closing our object writer stream.

func (*JSONObjectWriter) WriteObject

func (j *JSONObjectWriter) WriteObject(w io.Writer, obj interface{}, opts ObjectWriterOpts) error

WriteObject marshals and writes a JSON object to the output stream.

type ObjectWriter

type ObjectWriter interface {
	WriteObject(io.Writer, interface{}, ObjectWriterOpts) error
	WriteAfterClose(string)
	Close(io.Writer, ObjectWriterOpts) error
}

ObjectWriter marshals an object and streams it to output.

type ObjectWriterOpts

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

ObjectWriterOpts are options for writing objects.

type OutputOption

type OutputOption func(*Flags) //nolint:revive // ignore repetitive name warning

OutputOption is a function that configures output options for writers

func WithWriter

func WithWriter(formatName string, writer ObjectWriter) OutputOption

WithWriter registers a writer with the given Format name.

func WithoutCreateFilePerObject

func WithoutCreateFilePerObject() OutputOption

WithoutCreateFilePerObject is an option that configuration a writer to not output each object to a separate file.

func WithoutOutputDirectory

func WithoutOutputDirectory() OutputOption

WithoutOutputDirectory disables the out-dir flag

type RowExtractor

type RowExtractor func(interface{}) (map[string]string, error)

RowExtractor takes in an object and extracts a map of header name -> value.

type Streamer

type Streamer[T types.Object] func(T) error

Streamer is a callback for processing an individual entity. If an error is returned, the caller should stop processing results.

func NewWriteObjectStreamer

func NewWriteObjectStreamer[T types.Object](manager *WriterManager) Streamer[T]

NewWriteObjectStreamer streams objects to the io.Writer provided.

type TableOptions

type TableOptions func(*options)

TableOptions is a function that configures a TableWriter.

func SetAutoWrapText

func SetAutoWrapText(autoWrap bool) TableOptions

SetAutoWrapText sets whether or not the table writer should wrap text.

type TableWriter

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

TableWriter marshalls and writes objects in a table format to an output stream.

func NewTableWriter

func NewTableWriter(headers []string, extractor RowExtractor, os ...TableOptions) *TableWriter

NewTableWriter returns an object marshaller that renders a table. Headers are essentially the column keys for the table, and extractor takes in a single object and produces a map of header key -> header value. Not safe for concurrent use. The table writer can be customized by passing in options.

func (*TableWriter) Close

func (t *TableWriter) Close(w io.Writer, _ ObjectWriterOpts) error

Close renders the table and prints any final messages.

func (*TableWriter) WriteAfterClose

func (t *TableWriter) WriteAfterClose(s string)

WriteAfterClose sets a final message to print after closing our object writer stream.

func (*TableWriter) WriteObject

func (t *TableWriter) WriteObject(w io.Writer, obj interface{}, _ ObjectWriterOpts) error

WriteObject writes an object in table format to the output stream.

type WriterManager

type WriterManager struct {
	W            io.Writer
	ObjectWriter ObjectWriter
	FilePerObj   bool
	Dir          string
	Format       string

	FileOps FileOperation
	// contains filtered or unexported fields
}

WriterManager is a struct that manages the output of chronoctl

func NewWriterManager

func NewWriterManager(flags *Flags, stdout io.Writer, fileOps FileOperation) (*WriterManager, error)

NewWriterManager creates a new WriterManager

func (*WriterManager) Close

func (m *WriterManager) Close() error

Close closes the output writer

func (*WriterManager) WriteObject

func (m *WriterManager) WriteObject(obj types.Object) error

WriteObject writes an object to the output writer

type YAMLObjectWriter

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

YAMLObjectWriter is an ObjectWriter that marshals and streams yaml.

func NewYAMLObjectWriter

func NewYAMLObjectWriter() *YAMLObjectWriter

NewYAMLObjectWriter returns a new YAMLObjectWriter, which marshals and streams yaml.

func (*YAMLObjectWriter) Close

Close closes the object writer stream and prints any final messages

func (*YAMLObjectWriter) WriteAfterClose

func (y *YAMLObjectWriter) WriteAfterClose(s string)

WriteAfterClose sets a final message to print after closing our object writer stream.

func (*YAMLObjectWriter) WriteObject

func (y *YAMLObjectWriter) WriteObject(w io.Writer, obj interface{}, opts ObjectWriterOpts) error

WriteObject marshals and writes a YAML object to the output stream.

Jump to

Keyboard shortcuts

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