diff --git a/Makefile b/Makefile index 23e2825..746a571 100644 --- a/Makefile +++ b/Makefile @@ -67,10 +67,10 @@ test: clean buildlocal testlint: test lint -lint: +lint-basic: golangci-lint run --enable-only errcheck,govet,ineffassign,staticcheck,unused -lint-full: +lint: golangci-lint run --show-stats=false testfuzzy: clean diff --git a/cmd/api.go b/cmd/api.go index beccd6f..ad49604 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -91,6 +91,12 @@ func ApiRepl(conf *cfg.Config) *cli.Command { Aliases: []string{"p"}, Sources: cli.EnvVars("PAGER", "ES_JSON_PAGER"), }, + &cli.BoolFlag{ + Name: "human-readable-cat", + Usage: "enable human readable /_cat output", + Destination: &conf.HumanCat, + Aliases: []string{"H"}, + }, }, Action: func(ctx context.Context, cmd *cli.Command) error { diff --git a/pkg/cfg/config.go b/pkg/cfg/config.go index 628c92a..3ee5605 100644 --- a/pkg/cfg/config.go +++ b/pkg/cfg/config.go @@ -101,7 +101,8 @@ type Config struct { MaxDocs, MaxShardSize, MaxShardDocs int // roll over DryRun bool // rollover: -n - Tag string // api ls: -t + Tag string // api ls: -t + HumanCat bool // api repl: -H Ilm Ilm // ilm create diff --git a/pkg/es/api.go b/pkg/es/api.go index c8e71b2..8710b7b 100644 --- a/pkg/es/api.go +++ b/pkg/es/api.go @@ -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 {