output

package
v1.7.8 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package output provides structured output formatting for CLI commands. It supports text (default) and JSON output modes, allowing commands to produce machine-readable output for CI/CD pipelines and scripting.

Index

Constants

View Source
const (
	StatusSuccess = "success"
	StatusError   = "error"
	StatusWarning = "warning"
)

Status constants for the Response envelope.

Variables

This section is empty.

Functions

func Emit added in v1.7.0

func Emit(cmd *cobra.Command, resp Response) error

Emit writes a Response to cmd.OutOrStdout() when --output is "json". It is a no-op when the output format is text or the flag is absent.

func EmitError added in v1.7.0

func EmitError(cmd *cobra.Command, commandName string, err error) error

EmitError builds an error Response envelope and emits it via Emit.

func IsJSONOutput added in v1.7.0

func IsJSONOutput(cmd *cobra.Command) bool

IsJSONOutput returns true if the --output flag on the command is set to "json".

func RenderMarkdown added in v1.7.0

func RenderMarkdown(content string) string

RenderMarkdown renders markdown content to styled ANSI terminal output via glamour. It detects the terminal width automatically, falling back to 80 columns. If glamour fails for any reason, the original content is returned unchanged.

Types

type Format

type Format string

Format represents the output format for commands.

const (
	// FormatText is the default human-readable output format.
	FormatText Format = "text"
	// FormatJSON produces machine-readable JSON output.
	FormatJSON Format = "json"
)

type Response added in v1.7.0

type Response struct {
	Status  string `json:"status"`
	Command string `json:"command"`
	Data    any    `json:"data,omitempty"`
	Error   string `json:"error,omitempty"`
}

Response is the standard JSON envelope for all command output.

type Writer

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

Writer handles formatted output based on the configured format.

func NewWriter

func NewWriter(w io.Writer, format Format) *Writer

NewWriter creates an output writer for the given format.

func (*Writer) IsJSON

func (o *Writer) IsJSON() bool

IsJSON returns true if the writer is configured for JSON output.

func (*Writer) Render added in v1.7.0

func (o *Writer) Render(markdown string) error

Render writes markdown to the Writer using glamour styling in text mode. In JSON mode it is a no-op — callers should use Write for JSON output.

func (*Writer) Write

func (o *Writer) Write(data any, textFunc func(io.Writer)) error

Write outputs data in the configured format. For JSON format, data is marshalled to indented JSON. For text format, the textFunc is called to produce human-readable output.

Jump to

Keyboard shortcuts

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