add support for json color output in repl using jq, if installed (#16)

This commit is contained in:
T. von Dein
2026-05-18 13:56:07 +02:00
parent b71819f9e8
commit db50072a7b
3 changed files with 67 additions and 3 deletions

View File

@@ -18,6 +18,7 @@ package es
import (
"bytes"
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
@@ -25,6 +26,7 @@ import (
"io"
"net/http"
"os"
"os/exec"
"slices"
"strings"
@@ -74,9 +76,30 @@ func CallAPI(conf *cfg.Config, input []string) error {
return fmt.Errorf("failed to read response body: %s", err)
}
return prettyfiJson(conf, body)
}
func prettyfiJson(conf *cfg.Config, raw []byte) error {
if conf.HaveJQ {
cmd := exec.CommandContext(context.Background(), "jq", "-C")
cmd.Stdin = bytes.NewReader(raw)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return err
}
fmt.Println(out.String())
return nil
}
var pretty bytes.Buffer
error := json.Indent(&pretty, body, "", "\t")
if error != nil {
err := json.Indent(&pretty, raw, "", "\t")
if err != nil {
return fmt.Errorf("json parse error: %s", err)
}