From db50072a7b98bd26b9ab9e636a348f5b41fc7149 Mon Sep 17 00:00:00 2001 From: "T. von Dein" Date: Mon, 18 May 2026 13:56:07 +0200 Subject: [PATCH] add support for json color output in repl using jq, if installed (#16) --- pkg/cfg/config.go | 5 ++++- pkg/cfg/jq.go | 38 ++++++++++++++++++++++++++++++++++++++ pkg/es/repl.go | 27 +++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 pkg/cfg/jq.go diff --git a/pkg/cfg/config.go b/pkg/cfg/config.go index 1ff2018..b08dbcc 100644 --- a/pkg/cfg/config.go +++ b/pkg/cfg/config.go @@ -31,7 +31,7 @@ import ( ) const ( - Version string = `v0.0.9` + Version string = `v0.0.10` ) type Cluster struct { @@ -55,6 +55,7 @@ type Config struct { All, Verbose bool // cluster status: -a -v Persistent, Transient, Default bool // -p -t -D cluster settings set Force bool // ccr follower renew: -f + HaveJQ bool // determined at runtime by ourselfes } func NewConfig() *Config { @@ -110,6 +111,8 @@ func (conf *Config) Init() error { } } + conf.HaveJQ = isJQinstalled() + conf.PrintDebug() return nil diff --git a/pkg/cfg/jq.go b/pkg/cfg/jq.go new file mode 100644 index 0000000..6b328bc --- /dev/null +++ b/pkg/cfg/jq.go @@ -0,0 +1,38 @@ +/* +Copyright © 2026 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package cfg + +import ( + "context" + "os/exec" + "strings" +) + +func isJQinstalled() bool { + cmd := exec.CommandContext(context.Background(), "jq", "-h") + out, err := cmd.Output() + if err != nil { + return false + } + + if strings.Contains(string(out), "Usage") { + return true + } + + return false +} diff --git a/pkg/es/repl.go b/pkg/es/repl.go index 73ed2f7..b9a6404 100644 --- a/pkg/es/repl.go +++ b/pkg/es/repl.go @@ -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) }