mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
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:
5 files changed
+209
-40
No files matched your search
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user