formatter

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OutputFormatFlagName = "output-format"
)

Variables

View Source
var (
	Stdout io.Writer = os.Stdout
	Stderr io.Writer = os.Stderr
)
View Source
var ErrInvalidOutputFormat = errors.New("invalid output format")

ErrInvalidOutputFormat is returned when the provided output format is unsupported.

Functions

func AvailableOutputFormats added in v0.1.0

func AvailableOutputFormats() []string

func BindOutputFormat

func BindOutputFormat(persistentFlags *pflag.FlagSet, defaultValue OutputFormat) error

func CountItems

func CountItems(v any) int

CountItems returns the number of items if v is a slice/array; otherwise 1.

func ExitCode

func ExitCode(err error) int

ExitCode maps an error to a conventional CLI exit code. 0: success 2: usage/config/input error (print usage) 124: timeout 130: interrupted (canceled) 1: general error

func FormatShortTime

func FormatShortTime(t time.Time) string

FormatShortTime renders a timestamp in a compact, human-friendly format.

func Int64String

func Int64String(v int64) string

Int64String returns the base-10 string representation of v.

func PrintAppResponseMeta

func PrintAppResponseMeta(st *styles.Styles, meta *responsemeta.ResponseMeta, verbose bool, colored bool)

PrintAppResponseMeta renders application-level response metadata without leaking http.Request/Response. When verbose is true, sanitized headers are printed for debugging purposes, as well as the request URL.

func PrintByFormat

func PrintByFormat(data any, format OutputFormat, colored bool) cenclierrors.CencliError

PrintByFormat prints data to stdout according to the provided output format. Falls back to JSON when format is unrecognized.

Note: NDJSON is not supported here - it requires streaming via WithStreamingOutput. Commands that support NDJSON must use OutputTypeStreaming and skip PrintData when streaming.

func PrintDataWithTemplate

func PrintDataWithTemplate(templatePath string, colored bool, data any) cenclierrors.CencliError

PrintDataWithTemplate renders data through a template and writes the result to stdout. Callers must provide what kind of data needs to be rendered, so its correspoinding template can be used. Returns error if the template does not exist for the given entity, or the template fails to render.

func PrintError

func PrintError(err error, cmd *cobra.Command)

PrintError prints an error in a standardized format. Takes an optional cobra command to print usage information if the error should print usage.

func PrintJSON

func PrintJSON(v any, colored bool) error

PrintJSON prints v as pretty JSON, optionally colored. Uses the standard library for marshaling (to support omitzero), then colorizes the output if requested.

func PrintTree

func PrintTree(v any, colored bool) cenclierrors.CencliError

func PrintYAML

func PrintYAML(v any, colored bool) error

func Printf

func Printf(w io.Writer, format string, a ...any)

func Println

func Println(w io.Writer, a ...any)

func StderrIsTTY

func StderrIsTTY() bool

func StdoutIsTTY

func StdoutIsTTY() bool

func TruncateEnd

func TruncateEnd(s string, max int) string

TruncateEnd returns s truncated to max characters with an ellipsis suffix if needed.

func WriteNDJSONItem added in v0.1.0

func WriteNDJSONItem(w io.Writer, item any, colored bool) error

WriteNDJSONItem encodes a single item as NDJSON (one line of JSON) to the provided writer. This enables true streaming output where each item is written immediately. The item is encoded without pretty-printing (compact JSON on a single line). Uses the standard library for marshaling (to support omitzero), then colorizes the output if requested.

Types

type OutputFormat

type OutputFormat string
const (
	OutputFormatJSON     OutputFormat = "json"
	OutputFormatYAML     OutputFormat = "yaml"
	OutputFormatNDJSON   OutputFormat = "ndjson"
	OutputFormatTree     OutputFormat = "tree"
	OutputFormatShort    OutputFormat = "short"
	OutputFormatTemplate OutputFormat = "template"
)

func (OutputFormat) String

func (o OutputFormat) String() string

func (*OutputFormat) UnmarshalText

func (o *OutputFormat) UnmarshalText(text []byte) error

type TemplateFailureError

type TemplateFailureError interface {
	cenclierrors.CencliError
}

type TreeError

type TreeError interface {
	cenclierrors.CencliError
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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