add support for human readable /_cat endpoints: 'api repl -H' (#87)

This commit is contained in:
T. von Dein
2026-07-10 11:21:45 +02:00
parent 53abe666e8
commit 9a42b86d62
4 changed files with 38 additions and 22 deletions

View File

@@ -45,22 +45,25 @@ import (
)
const (
intro = `Input format: verb path [data]"
Example:
post /yourindex/_ccr/pause_follow
put /yourindex/_settings {"number_of_replicas": 1}
You can also put multiline JSON after the path like:
put /yourindex/_settings
{
"number_of_replicas": 1
}
If you do NOT supply a JSON in the first line, you need to hit ENTER
twice to complete.`
intro = `# Input format: verb path [data]"
#
# Example:
#
# post /yourindex/_ccr/pause_follow
# put /yourindex/_settings {"number_of_replicas": 1}
#
# You can also put multiline JSON after the path like:
#
# put /yourindex/_settings
# {
# "number_of_replicas": 1
# }
#
# If you do NOT supply a JSON in the first line, you need to hit ENTER
# twice to complete.
#
# Supply the flag --human-readable-cat, -H to view /_cat API calls in
# human readable form.`
)
// holds an API operation via go-openapi/spec
@@ -143,7 +146,11 @@ func ApiRepl(conf *cfg.Config) error {
fmt.Printf("failed to call API: %s\n", esErrorString(err))
}
pageJsonOutput(conf, raw)
if conf.HumanCat && strings.HasPrefix(parts[1], "/_cat") {
fmt.Println(string(raw))
} else {
pageJsonOutput(conf, raw)
}
}
//nolint:nilerr
@@ -216,8 +223,10 @@ func CallAPI(conf *cfg.Config, verb, path, data string) ([]byte, error) {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
if !conf.HumanCat && strings.HasPrefix(path, "/_cat") {
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
}
// make sure we have got all we need
if err := conf.DefaultCluster.CheckAuth(); err != nil {