formatter

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

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 BindOutputFormat

func BindOutputFormat(persistentFlags *pflag.FlagSet) 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) error

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

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.

func PrintNDJSON

func PrintNDJSON(v any, colored bool) error

PrintNDJSON prints one JSON object per line. If v is a slice, it prints each element on its own line. Otherwise, it prints v.

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.

Types

type OutputFormat

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

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
}

Jump to

Keyboard shortcuts

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