mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 11:44:18 +02:00
add support for json color output in repl using jq, if installed (#16)
This commit is contained in:
@@ -31,7 +31,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version string = `v0.0.9`
|
Version string = `v0.0.10`
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cluster struct {
|
type Cluster struct {
|
||||||
@@ -55,6 +55,7 @@ type Config struct {
|
|||||||
All, Verbose bool // cluster status: -a -v
|
All, Verbose bool // cluster status: -a -v
|
||||||
Persistent, Transient, Default bool // -p -t -D cluster settings set
|
Persistent, Transient, Default bool // -p -t -D cluster settings set
|
||||||
Force bool // ccr follower renew: -f
|
Force bool // ccr follower renew: -f
|
||||||
|
HaveJQ bool // determined at runtime by ourselfes
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() *Config {
|
func NewConfig() *Config {
|
||||||
@@ -110,6 +111,8 @@ func (conf *Config) Init() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conf.HaveJQ = isJQinstalled()
|
||||||
|
|
||||||
conf.PrintDebug()
|
conf.PrintDebug()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
38
pkg/cfg/jq.go
Normal file
38
pkg/cfg/jq.go
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -18,6 +18,7 @@ package es
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -25,6 +26,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -74,9 +76,30 @@ func CallAPI(conf *cfg.Config, input []string) error {
|
|||||||
return fmt.Errorf("failed to read response body: %s", err)
|
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
|
var pretty bytes.Buffer
|
||||||
error := json.Indent(&pretty, body, "", "\t")
|
err := json.Indent(&pretty, raw, "", "\t")
|
||||||
if error != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("json parse error: %s", err)
|
return fmt.Errorf("json parse error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user