cmdutil

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cmdutil provides shared helpers for all commands.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddListFlags added in v0.2.0

func AddListFlags(cmd *cobra.Command, page, limit *int)

AddListFlags registers the standard --limit/-L and --page/-p flags on a command.

func AsMap

func AsMap(v any) map[string]any

AsMap safely casts any to map[string]any.

func ClampRowsToLimit added in v0.2.0

func ClampRowsToLimit(rows []map[string]any, limit int) []map[string]any

ClampRowsToLimit trims rows when a server ignores --limit. It is harmless when the server already honored the requested limit.

func Confirm added in v0.2.0

func Confirm(prompt string, yes bool) bool

Confirm asks for a y/N confirmation unless yes is already true.

func ExtractList

func ExtractList(data any, listKeys ...string) (raw any, rows []map[string]any, total int, page int)

ExtractList pulls rows and pagination info from a standard API list response. The API may return pagination at the top level or nested under a "pagination" key:

{"data": [...], "pagination": {"total": N, "page": N, "totalPages": N}}
{"todos": [...]}

func FormatHHMM added in v0.2.0

func FormatHHMM(value any) (string, bool)

FormatHHMM converts numeric API times such as 830 to "08:30". Already-formatted "08:30" strings pass through unchanged.

func FormatWeekday added in v0.2.0

func FormatWeekday(value any) (string, bool)

FormatWeekday converts API weekday values (1=Mon, 7=Sun) to short names.

func Int64PtrIfPositive added in v0.2.0

func Int64PtrIfPositive(value int) *int64

Int64PtrIfPositive returns a pointer for positive integer flag values.

func Int64PtrIfSet added in v0.2.0

func Int64PtrIfSet(value string) (*int64, error)

Int64PtrIfSet parses a non-empty integer flag value.

func IntStringPtrIfPositive added in v0.2.0

func IntStringPtrIfPositive(value int) *string

IntStringPtrIfPositive returns a decimal string pointer for positive flag values.

func IsInteractive added in v0.2.0

func IsInteractive() bool

IsInteractive reports whether stdin and stdout are both terminals.

func PaginateRows added in v0.2.0

func PaginateRows(rows []map[string]any, page, limit int) ([]map[string]any, int, int, error)

PaginateRows applies client-side pagination for endpoints that do not expose page/limit parameters. A zero page means page 1 when a limit is set.

func PromptPick added in v0.2.0

func PromptPick(rows []map[string]any, cols []output.Column, idKey string, prompt string) (map[string]any, error)

PromptPick displays a table and lets the user pick an item by number (1-based) or by typing an ID directly. Returns the selected row, or nil if cancelled. The idKey specifies which column holds the unique identifier.

func PromptSelect added in v0.2.0

func PromptSelect(label string, choices []string) string

PromptSelect lets a user choose by 1-based index or by exact choice text.

func PromptText added in v0.2.0

func PromptText(label string) string

PromptText reads a single trimmed line from stdin after printing a label.

func RowsAsAny added in v0.2.0

func RowsAsAny(rows []map[string]any) []any

RowsAsAny converts []map[string]any to []any for JSON/jq compatibility.

func RowsFromAny added in v0.2.0

func RowsFromAny(data any) []map[string]any

RowsFromAny converts an API array value into table rows, ignoring non-object entries to match the existing detail-rendering behavior.

func ServerFromCmd

func ServerFromCmd(cmd *cobra.Command) string

ServerFromCmd extracts the --server flag, falling back to the configured default.

func ShouldUseInteractive added in v0.2.0

func ShouldUseInteractive(cmd *cobra.Command, force, disabled bool, optionFlags ...string) (bool, error)

ShouldUseInteractive decides whether a command should open a TUI.

Default behavior: in a TTY with no explicit list/filter flags set, the TUI opens automatically so users can browse interactively. Any changed flag (--search, --limit, etc.) switches to plain table output for scripting.

Override with --interactive to force the TUI (requires a terminal), or --no-interactive to suppress it (useful in aliases or scripts).

func StringPtrIfSet added in v0.2.0

func StringPtrIfSet(value string) *string

StringPtrIfSet returns a pointer for non-empty flag values.

func WithListRows added in v0.2.0

func WithListRows(data any, key string, rows []map[string]any, total, page int) any

WithListRows returns data with key replaced by rows and pagination metadata updated. It preserves other top-level response fields when possible.

Types

type ListResult added in v0.2.0

type ListResult struct {
	Raw   any
	Rows  []map[string]any
	Total int
	Page  int
	Key   string
}

func NewListResult added in v0.2.0

func NewListResult(data any, key string) ListResult

NewListResult extracts rows and pagination metadata from a list response. If the explicit key yields no rows, it falls back to treating the entire response as an array — matching the previous ExtractList behavior.

func (ListResult) FinalizeClientSide added in v0.2.0

func (r ListResult) FinalizeClientSide(page, limit int) (ListResult, error)

FinalizeClientSide applies client-side pagination and rewrites Raw so JSON and table output show the same rows.

func (ListResult) FinalizeServerSide added in v0.2.0

func (r ListResult) FinalizeServerSide(limit int) ListResult

FinalizeServerSide clamps rows when a server ignores limit and rewrites Raw so JSON and table output show the same rows.

func (ListResult) WithRows added in v0.2.0

func (r ListResult) WithRows(rows []map[string]any, total, page int) ListResult

WithRows returns a result with filtered/sorted rows. The Raw field is not updated — call FinalizeClientSide or FinalizeServerSide afterward to synchronize Raw with the new rows for consistent JSON/jq output.

Jump to

Keyboard shortcuts

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