mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 06:34:18 +02:00
add external pager support via ES_JSON_PAGER (#65)
This commit is contained in:
@@ -106,6 +106,8 @@ type Config struct {
|
||||
|
||||
FromNode, ToNode string // cluster reroute move: -f + -t
|
||||
AllowPrimary, AcceptDataLoss bool // cluster reroute cancel: -p,-a
|
||||
|
||||
Pager string // api repl: -p || PAGER
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
|
||||
@@ -36,3 +36,14 @@ func GetTermWidth() int {
|
||||
|
||||
return 80
|
||||
}
|
||||
|
||||
func GetTermHeight() int {
|
||||
if term.IsTerminal(int(os.Stdout.Fd())) {
|
||||
_, height, err := term.GetSize(int(os.Stdout.Fd()))
|
||||
if err == nil {
|
||||
return height
|
||||
}
|
||||
}
|
||||
|
||||
return 25
|
||||
}
|
||||
|
||||
@@ -141,14 +141,50 @@ func ApiRepl(conf *cfg.Config) error {
|
||||
fmt.Printf("failed to call API: %s\n", esErrorString(err))
|
||||
}
|
||||
|
||||
if err := prettyfiJson(conf, raw); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
pageJsonOutput(conf, raw)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func pageJsonOutput(conf *cfg.Config, raw []byte) {
|
||||
tmpconf := &cfg.Config{HaveJQ: conf.HaveJQ}
|
||||
|
||||
if conf.Pager != "" {
|
||||
tmpconf.HaveJQ = false
|
||||
}
|
||||
|
||||
output, err := prettyfiJson(tmpconf, raw)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
lines := len(strings.Split(output, "\n"))
|
||||
height := cfg.GetTermHeight()
|
||||
|
||||
if lines > height {
|
||||
if conf.Pager != "" {
|
||||
cmd := strings.Split(conf.Pager, " ")
|
||||
pager := exec.Command(cmd[0], cmd[1:]...)
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(output)
|
||||
|
||||
pager.Stdout = os.Stdout
|
||||
pager.Stdin = &buf
|
||||
pager.Stderr = os.Stderr
|
||||
|
||||
err := pager.Run()
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("failed to execute pager '%s': %s", conf.Pager, err)
|
||||
}
|
||||
} else {
|
||||
printer.Pager("json output", output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func encodeAuth(username, password string) string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
|
||||
}
|
||||
@@ -197,7 +233,7 @@ func CallAPI(conf *cfg.Config, verb, path, data string) ([]byte, error) {
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func prettyfiJson(conf *cfg.Config, raw []byte) error {
|
||||
func prettyfiJson(conf *cfg.Config, raw []byte) (string, error) {
|
||||
if conf.HaveJQ {
|
||||
cmd := exec.CommandContext(context.Background(), "jq", "-C")
|
||||
cmd.Stdin = bytes.NewReader(raw)
|
||||
@@ -207,23 +243,18 @@ func prettyfiJson(conf *cfg.Config, raw []byte) error {
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
fmt.Println(out.String())
|
||||
|
||||
return nil
|
||||
return out.String(), nil
|
||||
}
|
||||
|
||||
var pretty bytes.Buffer
|
||||
err := json.Indent(&pretty, raw, "", "\t")
|
||||
if err != nil {
|
||||
return fmt.Errorf("json parse error: %s", err)
|
||||
return "", fmt.Errorf("json parse error: %s", err)
|
||||
}
|
||||
|
||||
fmt.Println(pretty.String())
|
||||
|
||||
return nil
|
||||
return pretty.String(), nil
|
||||
}
|
||||
|
||||
// interactively read arbitrary JSON data from STDIN, which is
|
||||
|
||||
@@ -39,9 +39,11 @@ func getIndexTemplateSettings(conf *cfg.Config, tplname string, table *printer.T
|
||||
}
|
||||
|
||||
if conf.Debug {
|
||||
if err := prettyfiJson(conf, raw); err != nil {
|
||||
output, err := prettyfiJson(conf, raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(output)
|
||||
}
|
||||
|
||||
if len(data.IndexTemplates) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user