mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 22:34:19 +02:00
add support for json color output in repl using jq, if installed (#16)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user