Documentation
¶
Overview ¶
Package render owns the output contract shared by the verb groups: `-o` dispatch and validation, the table callback, the `{schemaVersion, data}` and `{schemaVersion, error}` envelopes, and the auth-error prefix. Importable only from packages below pkg/cmd/.
Index ¶
- Constants
- func HandleAuthError(err error) error
- func HandleError(ios *iostreams.IOStreams, outputFormat string, err error) error
- func PrintEnvelope(w io.Writer, data any) error
- func PrintError(ios *iostreams.IOStreams, outputFormat string, err error) error
- func Render(ios *iostreams.IOStreams, outputFormat string, data any, ...) error
- func RenderBare(ios *iostreams.IOStreams, outputFormat string, data any, ...) error
- func ResolveFormat(explicit string) string
- func ValidateJSONOrTable(format string) error
- func ValidateOutputFormat(format string, allowed ...string) error
- type ErrorBody
- type ErrorEnvelope
- type TableRenderer
Constants ¶
const ( FormatTable = "table" FormatJSON = "json" FormatYAML = "yaml" )
The `-o` values.
const OutputFlagUsage = "Output format: table, json (default: table)"
OutputFlagUsage is the `-o` help text of the verbs that accept json and table.
const SchemaVersion = "1"
SchemaVersion is the version in every success and error envelope. Bump only when the envelope shape changes in a way scripting consumers must detect.
Variables ¶
This section is empty.
Functions ¶
func HandleAuthError ¶
HandleAuthError prefixes a portal.ErrUnauthorized chain with "authentication required: ". The "krci auth login" hint comes from the ErrUnauthorized text. Other errors pass through unchanged.
func HandleError ¶
HandleError is the error path of an envelope verb: HandleAuthError, then PrintError.
func PrintEnvelope ¶
PrintEnvelope writes data in the `{schemaVersion, data}` envelope to w.
func PrintError ¶
PrintError writes the error envelope to stdout under -o json, unless err reports context cancellation. It adds no auth-error prefix. It returns err, so the root command prints the message on stderr and exits 1, or exits silently if the command was interrupted.
func Render ¶
func Render(ios *iostreams.IOStreams, outputFormat string, data any, renderTable TableRenderer) error
Render dispatches on the `-o` flag. "json" emits the `{schemaVersion, data}` envelope, "table" and the empty default call renderTable, any other value is an unknown-format error.
func RenderBare ¶
func RenderBare(ios *iostreams.IOStreams, outputFormat string, data any, renderTable TableRenderer) error
RenderBare is Render with "json" emitting data as is, without the envelope. Use it only for the bare verbs listed in docs/json-schemas.md; new verbs use Render.
func ResolveFormat ¶
ResolveFormat returns explicit, or FormatTable when it is empty.
func ValidateJSONOrTable ¶
ValidateJSONOrTable is ValidateOutputFormat with json and table allowed.
func ValidateOutputFormat ¶
ValidateOutputFormat accepts "" (the default) and every value in allowed; the match is case-sensitive. Any other value is an unknown-format error that lists allowed, in order. allowed must not be empty.
Types ¶
type ErrorBody ¶
type ErrorBody struct {
Message string `json:"message"`
}
ErrorBody carries the human-readable error message.
type ErrorEnvelope ¶
type ErrorEnvelope struct {
SchemaVersion string `json:"schemaVersion"`
Error ErrorBody `json:"error"`
}
ErrorEnvelope is the error shape of the envelope verbs.