Refactor output code, add JSON, TSV and YAML output modes, add -o (#19), add version subcommand

This commit is contained in:
T. von Dein
2026-05-20 13:06:39 +02:00
parent 32de4863af
commit 0870ceb3df
16 changed files with 439 additions and 190 deletions

View File

@@ -22,10 +22,11 @@ import (
"log/slog"
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/printer"
"github.com/elastic/go-elasticsearch/v9/typedapi/cat/shards"
)
func colorzizeShard(state, name string) string {
func colorzizeShard(conf *cfg.Config, state, name string) string {
color := "red" // in case of RELOCATING and UNASSIGNED
switch state {
@@ -35,7 +36,7 @@ func colorzizeShard(state, name string) string {
color = "yellow"
}
return Colorize(color, name)
return printer.Colorize(conf, color, name)
}
func resolvePrirep(state string) string {
@@ -102,13 +103,13 @@ func printShards(conf *cfg.Config, shardlist shards.Response) error {
headers = append(headers, "node", "ip")
}
table := NewTable(len(headers), len(shardlist))
table := printer.NewTable(conf, len(headers), len(shardlist))
table.Addheaders(headers...)
for idx, shard := range shardlist {
name := colorzizeShard(*shard.State, *shard.Index)
name := colorzizeShard(conf, *shard.State, *shard.Index)
table.entries[idx] = []string{
table.Entries[idx] = []string{
name,
*shard.Shard,
resolvePrirep(*shard.Prirep),
@@ -118,7 +119,7 @@ func printShards(conf *cfg.Config, shardlist shards.Response) error {
}
if conf.Verbose {
table.entries[idx] = append(table.entries[idx],
table.Entries[idx] = append(table.Entries[idx],
*shard.Node,
*shard.Ip,
)
@@ -126,7 +127,7 @@ func printShards(conf *cfg.Config, shardlist shards.Response) error {
}
table.Sort()
if err := table.PrintMarkdown(); err != nil {
if err := table.Print(); err != nil {
return err
}