fix(api): filter raw responses with --jq and end output with a newline

tea api /version --jq .version failed with "--jq requires --json", and
--json is rejected for raw responses. api is annotated tea.rawjson now:
--jq filters each successful JSON response, page by page with
--paginate, and prints strings without quotes. A non-JSON response is
an error with --jq; an error response prints its body unfiltered.

JSON responses end with a newline, so an error no longer starts on the
line of the body; on a terminal JSON is indented and colored in the key
order of the response, and other text is sanitized as before. Piped
non-JSON bodies stay byte-exact.

api gains -i for --include and --silent, a Long help modeled on gh api
(placeholders, -f/-F typing, pagination, --jq) and examples, and its
help lists --jq.

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-11 23:49:54 +02:00
1 parent 5e8aa1011f
commit 60de407eca
5 files changed
+209 -40

No files matched your search

+14
View File
@@ -125,6 +125,20 @@ func (o JSONOutput) encode(w io.Writer, value any) error {
return err
}
// IndentJSON formats JSON text for a terminal like Write with Pretty, but keeps the key order of the
// text: indented, sanitized, ending with a newline and, with color, highlighted.
func IndentJSON(data []byte, color bool) (string, error) {
var buf bytes.Buffer
if err := json.Indent(&buf, bytes.TrimSpace(data), "", " "); err != nil {
return "", err
}
text := SafeText(buf.String()) + "\n"
if color {
text = colorJSON(text)
}
return text, nil
}
// JSON colors, following gh: bold blue keys, green strings, yellow booleans, cyan null.
const (
jsonKeyColor = "\x1b[1;34m"