mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 23:54:19 +02:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
98a577f68a | ||
|
|
89e2498aad | ||
|
|
0870ceb3df |
15
Makefile
15
Makefile
@@ -23,11 +23,24 @@ UID = root
|
|||||||
GID = 0
|
GID = 0
|
||||||
HAVE_POD := $(shell pod2text -h 2>/dev/null)
|
HAVE_POD := $(shell pod2text -h 2>/dev/null)
|
||||||
|
|
||||||
|
BRANCH = $(shell git branch --show-current)
|
||||||
|
COMMIT = $(shell git rev-parse --short=8 HEAD)
|
||||||
|
BUILD = $(shell date +%Y.%m.%d.%H%M%S)
|
||||||
|
GOVERSION = $(shell grep -E "^go" go.mod | cut -d' ' -f2)
|
||||||
|
APIVERSION = $(shell grep github.com/elastic/go-elasticsearch go.mod | cut -d' ' -f2)
|
||||||
|
|
||||||
|
|
||||||
all: buildlocal
|
all: buildlocal
|
||||||
|
|
||||||
|
|
||||||
buildlocal:
|
buildlocal:
|
||||||
CGO_LDFLAGS='-static' go build -tags osusergo,netgo -ldflags "-extldflags=-static" -o $(tool)
|
CGO_LDFLAGS='-static' go build -tags osusergo,netgo -ldflags "-extldflags=-static" \
|
||||||
|
-ldflags "-X codeberg.org/scip/esctl/pkg/cfg.BRANCH=$(BRANCH) \
|
||||||
|
-X codeberg.org/scip/esctl/pkg/cfg.COMMIT=$(COMMIT) \
|
||||||
|
-X codeberg.org/scip/esctl/pkg/cfg.GOVERSION=$(GOVERSION) \
|
||||||
|
-X codeberg.org/scip/esctl/pkg/cfg.APIVERSION=$(APIVERSION) \
|
||||||
|
-X codeberg.org/scip/esctl/pkg/cfg.BUILD=$(BUILD)" \
|
||||||
|
-o $(tool)
|
||||||
strip $(tool)
|
strip $(tool)
|
||||||
|
|
||||||
install: buildlocal
|
install: buildlocal
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ Elasticsearch CLI
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Command tree
|
Command tree:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
ccr
|
ccr
|
||||||
@@ -34,6 +34,7 @@ Command tree
|
|||||||
index
|
index
|
||||||
alias
|
alias
|
||||||
create
|
create
|
||||||
|
delete
|
||||||
list
|
list
|
||||||
allocation
|
allocation
|
||||||
close
|
close
|
||||||
@@ -53,6 +54,7 @@ Command tree
|
|||||||
snapshot
|
snapshot
|
||||||
list
|
list
|
||||||
show
|
show
|
||||||
|
version
|
||||||
```
|
```
|
||||||
|
|
||||||
Configure `esctl` with environment variables:
|
Configure `esctl` with environment variables:
|
||||||
|
|||||||
64
cmd/doc.go
64
cmd/doc.go
@@ -33,6 +33,7 @@ func Doc(conf *cfg.Config) *cli.Command {
|
|||||||
|
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
DocAdd(conf),
|
DocAdd(conf),
|
||||||
|
DocShow(conf),
|
||||||
//Delete(conf),
|
//Delete(conf),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -43,16 +44,71 @@ func DocAdd(conf *cfg.Config) *cli.Command {
|
|||||||
Name: "add",
|
Name: "add",
|
||||||
Aliases: []string{"+"},
|
Aliases: []string{"+"},
|
||||||
Usage: "add JSON document index",
|
Usage: "add JSON document index",
|
||||||
UsageText: "add [options] <index> '<json-doc>'",
|
UsageText: "add [options] -i <index> '<json-doc>'",
|
||||||
|
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "index",
|
||||||
|
Usage: "index to work on",
|
||||||
|
Sources: cli.EnvVars("ES_INDEX"),
|
||||||
|
Destination: &conf.Index,
|
||||||
|
Aliases: []string{"i"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
args := cmd.Args()
|
args := cmd.Args()
|
||||||
|
|
||||||
if args.Len() != 2 {
|
if args.Len() != 1 {
|
||||||
return errors.New("missing arguments: <index> <json-doc>")
|
return errors.New("missing arguments: <json-doc>")
|
||||||
}
|
}
|
||||||
|
|
||||||
return es.DocAdd(conf, cmd.Args().Get(0), cmd.Args().Get(1))
|
return es.DocAdd(conf, cmd.Args().Get(0))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DocShow(conf *cfg.Config) *cli.Command {
|
||||||
|
return &cli.Command{
|
||||||
|
Name: "show",
|
||||||
|
Aliases: []string{"sh"},
|
||||||
|
Usage: "show a JSON document",
|
||||||
|
UsageText: "show [options] -i <index> <id>",
|
||||||
|
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "index",
|
||||||
|
Usage: "index to work on",
|
||||||
|
Sources: cli.EnvVars("ES_INDEX"),
|
||||||
|
Destination: &conf.Index,
|
||||||
|
Aliases: []string{"i"},
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "path",
|
||||||
|
Usage: "jsonPath filter (e.g. source.message)",
|
||||||
|
Destination: &conf.Path,
|
||||||
|
Aliases: []string{"p"},
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "help-jsonpath",
|
||||||
|
Usage: "show jsonPath help",
|
||||||
|
Destination: &conf.Subhelp,
|
||||||
|
Aliases: []string{"H"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
|
if conf.Subhelp {
|
||||||
|
return showJsonPathHelp()
|
||||||
|
}
|
||||||
|
|
||||||
|
args := cmd.Args()
|
||||||
|
|
||||||
|
if args.Len() != 1 {
|
||||||
|
return errors.New("missing arguments: <id>")
|
||||||
|
}
|
||||||
|
|
||||||
|
return es.DocShow(conf, cmd.Args().Get(0))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ func IndexAlias(conf *cfg.Config) *cli.Command {
|
|||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
IndexAliasCreate(conf),
|
IndexAliasCreate(conf),
|
||||||
IndexAliasList(conf),
|
IndexAliasList(conf),
|
||||||
|
IndexAliasDelete(conf),
|
||||||
//IndexAliasShow(conf),
|
//IndexAliasShow(conf),
|
||||||
//IndexAliasDelete(conf),
|
|
||||||
//IndexAliasAdd(conf), // see https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-update-aliases
|
//IndexAliasAdd(conf), // see https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-update-aliases
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -62,6 +62,26 @@ func IndexAliasCreate(conf *cfg.Config) *cli.Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IndexAliasDelete(conf *cfg.Config) *cli.Command {
|
||||||
|
return &cli.Command{
|
||||||
|
Name: "delete",
|
||||||
|
Aliases: []string{"rm"},
|
||||||
|
Usage: "delete an index alias",
|
||||||
|
UsageText: "delete <index> <alias>",
|
||||||
|
|
||||||
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
|
index := cmd.Args().Get(0)
|
||||||
|
alias := cmd.Args().Get(1)
|
||||||
|
|
||||||
|
if index == "" || alias == "" {
|
||||||
|
return errors.New("no index or alias specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
return es.IndexAliasDelete(conf, index, alias)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func IndexAliasList(conf *cfg.Config) *cli.Command {
|
func IndexAliasList(conf *cfg.Config) *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "list",
|
Name: "list",
|
||||||
|
|||||||
23
cmd/root.go
23
cmd/root.go
@@ -42,7 +42,7 @@ func Main() int {
|
|||||||
cmd := &cli.Command{
|
cmd := &cli.Command{
|
||||||
Name: "esctl",
|
Name: "esctl",
|
||||||
Usage: "manage elasticsearch from cli",
|
Usage: "manage elasticsearch from cli",
|
||||||
Version: cfg.Version,
|
//Version: cfg.Version,
|
||||||
EnableShellCompletion: true,
|
EnableShellCompletion: true,
|
||||||
|
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
@@ -69,6 +69,13 @@ func Main() int {
|
|||||||
Usage: "cluster alias to work with",
|
Usage: "cluster alias to work with",
|
||||||
Destination: &conf.CurrentCluster,
|
Destination: &conf.CurrentCluster,
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "output",
|
||||||
|
Aliases: []string{"o"},
|
||||||
|
Value: "",
|
||||||
|
Usage: "output mode (tsv, markdown, json, yaml) default: tsv",
|
||||||
|
Destination: &conf.Output,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
@@ -81,6 +88,7 @@ func Main() int {
|
|||||||
Node(conf),
|
Node(conf),
|
||||||
Doc(conf),
|
Doc(conf),
|
||||||
Repl(conf),
|
Repl(conf),
|
||||||
|
Version(conf),
|
||||||
},
|
},
|
||||||
|
|
||||||
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
|
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
|
||||||
@@ -101,3 +109,16 @@ func Main() int {
|
|||||||
|
|
||||||
return Finish(cmd.Run(context.Background(), os.Args))
|
return Finish(cmd.Run(context.Background(), os.Args))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Version(conf *cfg.Config) *cli.Command {
|
||||||
|
return &cli.Command{
|
||||||
|
Name: "version",
|
||||||
|
Usage: "show esctl version information",
|
||||||
|
|
||||||
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
|
_, err := fmt.Printf("esctl version: %s\n build: %s\n branch: %s\n commit: %s\n go version: %s\n API Version: %s\n",
|
||||||
|
cfg.Version, cfg.BUILD, cfg.BRANCH, cfg.COMMIT, cfg.GOVERSION, cfg.APIVERSION)
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"fmt"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
"codeberg.org/scip/esctl/pkg/es"
|
"codeberg.org/scip/esctl/pkg/es"
|
||||||
@@ -31,7 +31,17 @@ func Search(conf *cfg.Config) *cli.Command {
|
|||||||
Name: "search",
|
Name: "search",
|
||||||
Aliases: []string{"/"},
|
Aliases: []string{"/"},
|
||||||
Usage: "search within an index",
|
Usage: "search within an index",
|
||||||
UsageText: "search [options] <field=pattern> ...",
|
UsageText: `search [options] [<[field<sep>]pattern> ...]
|
||||||
|
|
||||||
|
<sep> might be one of:
|
||||||
|
=: Must match
|
||||||
|
!=: Must not match
|
||||||
|
?: Should match
|
||||||
|
|
||||||
|
You can omit a field spec and thereby search across all fields.
|
||||||
|
|
||||||
|
You can also search multiple fields by separating them with comma, eg:
|
||||||
|
user,group=root`,
|
||||||
|
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
@@ -57,19 +67,63 @@ func Search(conf *cfg.Config) *cli.Command {
|
|||||||
},
|
},
|
||||||
&cli.StringSliceFlag{
|
&cli.StringSliceFlag{
|
||||||
Name: "filter",
|
Name: "filter",
|
||||||
Usage: "additional filters. format: key=value",
|
Usage: "additional boolean filters. format: key=value",
|
||||||
Destination: &conf.Filter,
|
Destination: &conf.Filter,
|
||||||
Aliases: []string{"F"},
|
Aliases: []string{"F"},
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "jsonpath",
|
||||||
|
Usage: "jsonPath filter (e.g. source.message)",
|
||||||
|
Destination: &conf.Path,
|
||||||
|
Aliases: []string{"p"},
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "help-jsonpath",
|
||||||
|
Usage: "show jsonPath help",
|
||||||
|
Destination: &conf.Subhelp,
|
||||||
|
Aliases: []string{"H"},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
args := cmd.Args()
|
if conf.Subhelp {
|
||||||
if args.Len() == 0 {
|
return showJsonPathHelp()
|
||||||
return errors.New("at least one query must be specified (format: field=pattern)")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
args := cmd.Args()
|
||||||
|
|
||||||
return es.Search(conf, args.Slice())
|
return es.Search(conf, args.Slice())
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func showJsonPathHelp() error {
|
||||||
|
_, err := fmt.Println(`jsonPath usage:
|
||||||
|
|
||||||
|
name.last >> "Anderson"
|
||||||
|
age >> 37
|
||||||
|
children >> ["Sara","Alex","Jack"]
|
||||||
|
children.# >> 3
|
||||||
|
children.1 >> "Alex"
|
||||||
|
child*.2 >> "Jack"
|
||||||
|
c?ildren.0 >> "Sara"
|
||||||
|
fav\.movie >> "Deer Hunter"
|
||||||
|
friends.#.first >> ["Dale","Roger","Jane"]
|
||||||
|
friends.1.last >> "Craig"
|
||||||
|
|
||||||
|
You can also query an array for the first match by using #(...), or
|
||||||
|
find all matches with #(...)#. Queries support the ==, !=, <, <=, >,
|
||||||
|
>= comparison operators and the simple pattern matching % (like) and
|
||||||
|
!% (not like) operators. Eg:
|
||||||
|
|
||||||
|
friends.#(last=="Murphy").first >> "Dale"
|
||||||
|
friends.#(last=="Murphy")#.first >> ["Dale","Jane"]
|
||||||
|
friends.#(age>45)#.last >> ["Craig","Murphy"]
|
||||||
|
friends.#(first%"D*").last >> "Murphy"
|
||||||
|
friends.#(first!%"D*").last >> "Craig"
|
||||||
|
friends.#(nets.#(=="fb"))#.first >> ["Dale","Roger"]
|
||||||
|
|
||||||
|
Documentation: https://github.com/tidwall/gjson/blob/master/SYNTAX.md`)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
3
go.mod
3
go.mod
@@ -43,6 +43,9 @@ require (
|
|||||||
github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 // indirect
|
github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 // indirect
|
||||||
github.com/olekukonko/errors v1.2.0 // indirect
|
github.com/olekukonko/errors v1.2.0 // indirect
|
||||||
github.com/olekukonko/ll v0.1.8 // indirect
|
github.com/olekukonko/ll v0.1.8 // indirect
|
||||||
|
github.com/tidwall/gjson v1.19.0 // indirect
|
||||||
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
|
github.com/tidwall/pretty v1.2.0 // indirect
|
||||||
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
|
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
|
||||||
go.opentelemetry.io/otel v1.35.0 // indirect
|
go.opentelemetry.io/otel v1.35.0 // indirect
|
||||||
go.opentelemetry.io/otel/metric v1.35.0 // indirect
|
go.opentelemetry.io/otel/metric v1.35.0 // indirect
|
||||||
|
|||||||
6
go.sum
6
go.sum
@@ -58,6 +58,12 @@ github.com/olekukonko/ll v0.1.8 h1:ysHCJRGHYKzmBSdz9w5AySztx7lG8SQY+naTGYUbsz8=
|
|||||||
github.com/olekukonko/ll v0.1.8/go.mod h1:RPRC6UcscfFZgjo1nulkfMH5IM0QAYim0LfnMvUuozw=
|
github.com/olekukonko/ll v0.1.8/go.mod h1:RPRC6UcscfFZgjo1nulkfMH5IM0QAYim0LfnMvUuozw=
|
||||||
github.com/olekukonko/tablewriter v1.1.4 h1:ORUMI3dXbMnRlRggJX3+q7OzQFDdvgbN9nVWj1drm6I=
|
github.com/olekukonko/tablewriter v1.1.4 h1:ORUMI3dXbMnRlRggJX3+q7OzQFDdvgbN9nVWj1drm6I=
|
||||||
github.com/olekukonko/tablewriter v1.1.4/go.mod h1:+kedxuyTtgoZLwif3P1Em4hARJs+mVnzKxmsCL/C5RY=
|
github.com/olekukonko/tablewriter v1.1.4/go.mod h1:+kedxuyTtgoZLwif3P1Em4hARJs+mVnzKxmsCL/C5RY=
|
||||||
|
github.com/tidwall/gjson v1.19.0 h1:xwxm7n691Uf3u5OFjzngavjGTh55KX5q/9w9xHW88JU=
|
||||||
|
github.com/tidwall/gjson v1.19.0/go.mod h1:V37/opeE/JbLUOfH0QTXiNez2l0RUjYUhpT4szFQAfc=
|
||||||
|
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||||
|
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||||
|
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||||
|
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
github.com/tlinden/yadu v0.1.3 h1:5cRCUmj+l5yvlM2irtpFBIJwVV2DPEgYSaWvF19FtcY=
|
github.com/tlinden/yadu v0.1.3 h1:5cRCUmj+l5yvlM2irtpFBIJwVV2DPEgYSaWvF19FtcY=
|
||||||
github.com/tlinden/yadu v0.1.3/go.mod h1:l3bRmHKL9zGAR6pnBHY2HRPxBecf7L74BoBgOOpTcUA=
|
github.com/tlinden/yadu v0.1.3/go.mod h1:l3bRmHKL9zGAR6pnBHY2HRPxBecf7L74BoBgOOpTcUA=
|
||||||
github.com/urfave/cli/v3 v3.8.0 h1:XqKPrm0q4P0q5JpoclYoCAv0/MIvH/jZ2umzuf8pNTI=
|
github.com/urfave/cli/v3 v3.8.0 h1:XqKPrm0q4P0q5JpoclYoCAv0/MIvH/jZ2umzuf8pNTI=
|
||||||
|
|||||||
@@ -31,7 +31,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version string = `v0.0.11`
|
Version string = `v0.0.13`
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// initialized during build, see Makefile:buildlocal
|
||||||
|
APIVERSION, GOVERSION, BUILD, COMMIT, BRANCH string
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cluster struct {
|
type Cluster struct {
|
||||||
@@ -43,6 +48,7 @@ type Config struct {
|
|||||||
ConfigFile string // -c
|
ConfigFile string // -c
|
||||||
CurrentCluster string // -C
|
CurrentCluster string // -C
|
||||||
Debug bool // -d
|
Debug bool // -d
|
||||||
|
Output string // -o <mode>
|
||||||
Clusters map[string]*Cluster
|
Clusters map[string]*Cluster
|
||||||
DefaultCluster *Cluster
|
DefaultCluster *Cluster
|
||||||
Index string // index: -i
|
Index string // index: -i
|
||||||
@@ -52,6 +58,8 @@ type Config struct {
|
|||||||
Primary bool // index allocation: -p
|
Primary bool // index allocation: -p
|
||||||
From, To, MaxItems int // search: flags
|
From, To, MaxItems int // search: flags
|
||||||
Filter []string // search: -F
|
Filter []string // search: -F
|
||||||
|
Path string // search+doc sh: -p
|
||||||
|
Subhelp bool // search+doc sh: -H
|
||||||
Exclude string // cluster compare: -e (regexp)
|
Exclude string // cluster compare: -e (regexp)
|
||||||
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
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"codeberg.org/scip/esctl/pkg/printer"
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -115,10 +116,10 @@ func CcrRemoteInfo(conf *cfg.Config, index string) error {
|
|||||||
mode = "leader"
|
mode = "leader"
|
||||||
}
|
}
|
||||||
|
|
||||||
table := NewTable(2, 5)
|
table := printer.NewTable(conf, 2, 5)
|
||||||
table.Addheaders("ccr remote property", "value")
|
table.Addheaders("ccr remote property", "value")
|
||||||
|
|
||||||
table.entries = [][]string{
|
table.Entries = [][]string{
|
||||||
{"Remote Cluster", remote},
|
{"Remote Cluster", remote},
|
||||||
{"CCR Mode", mode},
|
{"CCR Mode", mode},
|
||||||
{"Connected", fmt.Sprintf("%t", info.Connected)},
|
{"Connected", fmt.Sprintf("%t", info.Connected)},
|
||||||
@@ -126,7 +127,7 @@ func CcrRemoteInfo(conf *cfg.Config, index string) error {
|
|||||||
{"Proxy Address", info.ProxyAddress},
|
{"Proxy Address", info.ProxyAddress},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"codeberg.org/scip/esctl/pkg/printer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getRemoteName(conf *cfg.Config) (string, error) {
|
func getRemoteName(conf *cfg.Config) (string, error) {
|
||||||
@@ -175,10 +176,10 @@ func CcrFollowerShow(conf *cfg.Config, index string) error {
|
|||||||
|
|
||||||
follower := res.Indices[0].Shards[0]
|
follower := res.Indices[0].Shards[0]
|
||||||
|
|
||||||
table := NewTable(2, 9)
|
table := printer.NewTable(conf, 2, 9)
|
||||||
table.Addheaders("ccr follower property", "value")
|
table.Addheaders("ccr follower property", "value")
|
||||||
|
|
||||||
table.entries = [][]string{
|
table.Entries = [][]string{
|
||||||
{"name", index},
|
{"name", index},
|
||||||
{"remote_cluster", follower.RemoteCluster},
|
{"remote_cluster", follower.RemoteCluster},
|
||||||
{"leader_checkpoint", fmt.Sprintf("%d", follower.LeaderGlobalCheckpoint)},
|
{"leader_checkpoint", fmt.Sprintf("%d", follower.LeaderGlobalCheckpoint)},
|
||||||
@@ -190,7 +191,7 @@ func CcrFollowerShow(conf *cfg.Config, index string) error {
|
|||||||
{"successful_write_requests", fmt.Sprintf("%d", follower.SuccessfulWriteRequests)},
|
{"successful_write_requests", fmt.Sprintf("%d", follower.SuccessfulWriteRequests)},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"codeberg.org/scip/esctl/pkg/printer"
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/ccr/stats"
|
"github.com/elastic/go-elasticsearch/v9/typedapi/ccr/stats"
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/cluster/health"
|
"github.com/elastic/go-elasticsearch/v9/typedapi/cluster/health"
|
||||||
@@ -52,7 +53,7 @@ type apiResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ClusterList(conf *cfg.Config) error {
|
func ClusterList(conf *cfg.Config) error {
|
||||||
table := NewTable(3, len(conf.Clusters))
|
table := printer.NewTable(conf, 3, len(conf.Clusters))
|
||||||
|
|
||||||
table.Addheaders("cluster", "uri", "default")
|
table.Addheaders("cluster", "uri", "default")
|
||||||
|
|
||||||
@@ -76,13 +77,13 @@ func ClusterList(conf *cfg.Config) error {
|
|||||||
Do(context.Background())
|
Do(context.Background())
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
name = Colorize("green", name)
|
name = printer.Colorize(conf, "green", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
table.entries[idx] = []string{name, cluster.Uri, fmt.Sprintf("%t", current)}
|
table.Entries[idx] = []string{name, cluster.Uri, fmt.Sprintf("%t", current)}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,11 +163,11 @@ func ClusterStatus(conf *cfg.Config) error {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
table := NewTable(2, 5)
|
table := printer.NewTable(conf, 2, 5)
|
||||||
table.Addheaders(cluster, "status")
|
table.Addheaders(cluster, "status")
|
||||||
|
|
||||||
table.entries = [][]string{
|
table.Entries = [][]string{
|
||||||
{"Cluster Name", Colorize(clusterhealth.Status.Name, clusterhealth.ClusterName)},
|
{"Cluster Name", printer.Colorize(conf, clusterhealth.Status.Name, clusterhealth.ClusterName)},
|
||||||
{"ES Version", info.Version.Int},
|
{"ES Version", info.Version.Int},
|
||||||
{"Active Shards", fmt.Sprintf("%d", clusterhealth.ActiveShards)},
|
{"Active Shards", fmt.Sprintf("%d", clusterhealth.ActiveShards)},
|
||||||
{"Active Primary Shards", fmt.Sprintf("%d", clusterhealth.ActivePrimaryShards)},
|
{"Active Primary Shards", fmt.Sprintf("%d", clusterhealth.ActivePrimaryShards)},
|
||||||
@@ -178,7 +179,7 @@ func ClusterStatus(conf *cfg.Config) error {
|
|||||||
table = gatherClusterStats(conf, clusterstats, table)
|
table = gatherClusterStats(conf, clusterstats, table)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,7 +187,7 @@ func ClusterStatus(conf *cfg.Config) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func gatherClusterStats(conf *cfg.Config, clusterstats *clusterstats.Response, table *Table) *Table {
|
func gatherClusterStats(conf *cfg.Config, clusterstats *clusterstats.Response, table *printer.Table) *printer.Table {
|
||||||
var querycount int64
|
var querycount int64
|
||||||
var vmversion string
|
var vmversion string
|
||||||
|
|
||||||
@@ -202,7 +203,7 @@ func gatherClusterStats(conf *cfg.Config, clusterstats *clusterstats.Response, t
|
|||||||
|
|
||||||
isleader := checkClusterIsLeader(conf, conf.CurrentCluster)
|
isleader := checkClusterIsLeader(conf, conf.CurrentCluster)
|
||||||
|
|
||||||
table.entries = append(table.entries, [][]string{
|
table.Entries = append(table.Entries, [][]string{
|
||||||
{"Indicies", fmt.Sprintf("%d", clusterstats.Indices.Count)},
|
{"Indicies", fmt.Sprintf("%d", clusterstats.Indices.Count)},
|
||||||
{"Is Leader", fmt.Sprintf("%t", isleader)},
|
{"Is Leader", fmt.Sprintf("%t", isleader)},
|
||||||
{"Docs", fmt.Sprintf("%d", clusterstats.Indices.Docs.Count)},
|
{"Docs", fmt.Sprintf("%d", clusterstats.Indices.Docs.Count)},
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"codeberg.org/scip/esctl/pkg/printer"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -65,7 +66,7 @@ func ClusterSettingsList(conf *cfg.Config) error {
|
|||||||
return fmt.Errorf("failed to get cluster settings: %s", err)
|
return fmt.Errorf("failed to get cluster settings: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
table := NewTable(2, 0)
|
table := printer.NewTable(conf, 2, 0)
|
||||||
table.Addheaders("setting", "value")
|
table.Addheaders("setting", "value")
|
||||||
entries := [][]string{}
|
entries := [][]string{}
|
||||||
|
|
||||||
@@ -94,10 +95,10 @@ func ClusterSettingsList(conf *cfg.Config) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
table.entries = entries
|
table.Entries = entries
|
||||||
table.Sort()
|
table.Sort()
|
||||||
|
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"codeberg.org/scip/esctl/pkg/printer"
|
||||||
"github.com/elastic/go-elasticsearch/v9"
|
"github.com/elastic/go-elasticsearch/v9"
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/cluster/health"
|
"github.com/elastic/go-elasticsearch/v9/typedapi/cluster/health"
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
||||||
@@ -74,14 +75,14 @@ func checkClusterStatus(conf *cfg.Config, leader, follower string) bool {
|
|||||||
status[cluster] = st
|
status[cluster] = st
|
||||||
}
|
}
|
||||||
|
|
||||||
table := NewTable(3, 5)
|
table := printer.NewTable(conf, 3, 5)
|
||||||
|
|
||||||
table.Addheaders("setting", "leader:"+leader, "follower:"+follower)
|
table.Addheaders("setting", "leader:"+leader, "follower:"+follower)
|
||||||
|
|
||||||
table.entries = [][]string{
|
table.Entries = [][]string{
|
||||||
{"Cluster Name",
|
{"Cluster Name",
|
||||||
Colorize(status[leader].Status.Name, status[leader].ClusterName),
|
printer.Colorize(conf, status[leader].Status.Name, status[leader].ClusterName),
|
||||||
Colorize(status[follower].Status.Name, status[follower].ClusterName),
|
printer.Colorize(conf, status[follower].Status.Name, status[follower].ClusterName),
|
||||||
},
|
},
|
||||||
{"Active Shards",
|
{"Active Shards",
|
||||||
fmt.Sprintf("%d", status[leader].ActiveShards),
|
fmt.Sprintf("%d", status[leader].ActiveShards),
|
||||||
@@ -101,7 +102,7 @@ func checkClusterStatus(conf *cfg.Config, leader, follower string) bool {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -151,17 +152,17 @@ func findIlmErrors(conf *cfg.Config, leader, follower string) bool {
|
|||||||
|
|
||||||
if len(failed[cluster]) > 0 {
|
if len(failed[cluster]) > 0 {
|
||||||
idx := 0
|
idx := 0
|
||||||
table := NewTable(2, len(failed[cluster]))
|
table := printer.NewTable(conf, 2, len(failed[cluster]))
|
||||||
table.Addheaders("ilm errors on "+which, "errors")
|
table.Addheaders("ilm errors on "+which, "errors")
|
||||||
|
|
||||||
for name, count := range failed[cluster] {
|
for name, count := range failed[cluster] {
|
||||||
table.entries[idx] = []string{name, count}
|
table.Entries[idx] = []string{name, count}
|
||||||
idx++
|
idx++
|
||||||
}
|
}
|
||||||
|
|
||||||
table.Sort()
|
table.Sort()
|
||||||
|
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -223,18 +224,18 @@ func findIndicesOnlyOnLeader(conf *cfg.Config, indices ClusterIndices, leader, f
|
|||||||
}
|
}
|
||||||
|
|
||||||
idx := 0
|
idx := 0
|
||||||
table := NewTable(3, len(indexOnlyOnLeader))
|
table := printer.NewTable(conf, 3, len(indexOnlyOnLeader))
|
||||||
table.Addheaders("index only on leader", "size", "docscount")
|
table.Addheaders("index only on leader", "size", "docscount")
|
||||||
|
|
||||||
for name, index := range indexOnlyOnLeader {
|
for name, index := range indexOnlyOnLeader {
|
||||||
name := Colorize("red", name)
|
name := printer.Colorize(conf, "red", name)
|
||||||
|
|
||||||
table.entries[idx] = []string{name, *index.DatasetSize, *index.DocsCount}
|
table.Entries[idx] = []string{name, *index.DatasetSize, *index.DocsCount}
|
||||||
idx++
|
idx++
|
||||||
}
|
}
|
||||||
|
|
||||||
table.Sort()
|
table.Sort()
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -273,18 +274,18 @@ func findOrphanedIndices(conf *cfg.Config, indices ClusterIndices, leader, follo
|
|||||||
}
|
}
|
||||||
|
|
||||||
idx := 0
|
idx := 0
|
||||||
table := NewTable(3, len(orphaned))
|
table := printer.NewTable(conf, 3, len(orphaned))
|
||||||
table.Addheaders("orphaned index on follower", "size", "docscount")
|
table.Addheaders("orphaned index on follower", "size", "docscount")
|
||||||
|
|
||||||
for name, index := range orphaned {
|
for name, index := range orphaned {
|
||||||
name := Colorize("red", name)
|
name := printer.Colorize(conf, "red", name)
|
||||||
|
|
||||||
table.entries[idx] = []string{name, *index.DatasetSize, *index.DocsCount}
|
table.Entries[idx] = []string{name, *index.DatasetSize, *index.DocsCount}
|
||||||
idx++
|
idx++
|
||||||
}
|
}
|
||||||
|
|
||||||
table.Sort()
|
table.Sort()
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -307,18 +308,18 @@ func findFailedFollowerIndices(conf *cfg.Config, indices ClusterIndices, followe
|
|||||||
}
|
}
|
||||||
|
|
||||||
idx := 0
|
idx := 0
|
||||||
table := NewTable(3, len(red))
|
table := printer.NewTable(conf, 3, len(red))
|
||||||
table.Addheaders("red index on follower", "size", "docscount")
|
table.Addheaders("red index on follower", "size", "docscount")
|
||||||
|
|
||||||
for name, index := range red {
|
for name, index := range red {
|
||||||
name := Colorize("red", name)
|
name := printer.Colorize(conf, "red", name)
|
||||||
|
|
||||||
table.entries[idx] = []string{name, *index.DatasetSize, *index.DocsCount}
|
table.Entries[idx] = []string{name, *index.DatasetSize, *index.DocsCount}
|
||||||
idx++
|
idx++
|
||||||
}
|
}
|
||||||
|
|
||||||
table.Sort()
|
table.Sort()
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,10 +19,12 @@ package es
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
// create index with:
|
// create index with:
|
||||||
@@ -32,7 +34,7 @@ import (
|
|||||||
// then add a doc:
|
// then add a doc:
|
||||||
//
|
//
|
||||||
// esctl doc add foo2 '{"id":"d8d8d","user":"scip"}'
|
// esctl doc add foo2 '{"id":"d8d8d","user":"scip"}'
|
||||||
func DocAdd(conf *cfg.Config, index, jsondoc string) error {
|
func DocAdd(conf *cfg.Config, jsondoc string) error {
|
||||||
data := map[string]any{}
|
data := map[string]any{}
|
||||||
|
|
||||||
err := json.Unmarshal([]byte(jsondoc), &data)
|
err := json.Unmarshal([]byte(jsondoc), &data)
|
||||||
@@ -42,14 +44,46 @@ func DocAdd(conf *cfg.Config, index, jsondoc string) error {
|
|||||||
|
|
||||||
now := fmt.Sprintf("%d", time.Now().Unix())
|
now := fmt.Sprintf("%d", time.Now().Unix())
|
||||||
|
|
||||||
_, err = conf.DefaultCluster.ES.Create(index, now).
|
res, err := conf.DefaultCluster.ES.Create(conf.Index, now).
|
||||||
Document(data).
|
Document(data).
|
||||||
Header("content-type", "application/json").
|
Header("content-type", "application/json").
|
||||||
Header("accept", "application/json").
|
Header("accept", "application/json").
|
||||||
Do(context.Background())
|
Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create new doc in index %s: %s", index, err)
|
return fmt.Errorf("failed to create new doc in index %s: %s", conf.Index, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println(res.Id_)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DocShow(conf *cfg.Config, id string) error {
|
||||||
|
res, err := conf.DefaultCluster.ES.Get(conf.Index, id).
|
||||||
|
Header("content-type", "application/json").
|
||||||
|
Header("accept", "application/json").
|
||||||
|
Do(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to retrieve doc in index %s: %s", conf.Index, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !res.Found {
|
||||||
|
return errors.New("no document with that id found")
|
||||||
|
}
|
||||||
|
|
||||||
|
docjson := fmt.Sprintf(`{"id":%s, "index":"%s", "source":%s}`,
|
||||||
|
res.Id_,
|
||||||
|
res.Index_,
|
||||||
|
res.Source_)
|
||||||
|
|
||||||
|
if conf.Path != "" {
|
||||||
|
value := gjson.Get(docjson, conf.Path)
|
||||||
|
fmt.Println(value.String())
|
||||||
|
} else {
|
||||||
|
fmt.Println(docjson)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"codeberg.org/scip/esctl/pkg/printer"
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/cat/indices"
|
"github.com/elastic/go-elasticsearch/v9/typedapi/cat/indices"
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
|
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/healthstatus"
|
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/healthstatus"
|
||||||
@@ -95,13 +96,13 @@ func IndexList(conf *cfg.Config) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
table := NewTable(3, size)
|
table := printer.NewTable(conf, 3, size)
|
||||||
table.Addheaders("name", "size", "docscount")
|
table.Addheaders("name", "size", "docscount")
|
||||||
|
|
||||||
for idx, index := range list {
|
for idx, index := range list {
|
||||||
name := Colorize(*index.Health, *index.Index)
|
name := printer.Colorize(conf, *index.Health, *index.Index)
|
||||||
|
|
||||||
table.entries[idx] = []string{name, *index.DatasetSize, *index.DocsCount}
|
table.Entries[idx] = []string{name, *index.DatasetSize, *index.DocsCount}
|
||||||
|
|
||||||
if idx == size-1 {
|
if idx == size-1 {
|
||||||
break
|
break
|
||||||
@@ -109,7 +110,7 @@ func IndexList(conf *cfg.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
table.Sort()
|
table.Sort()
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,7 +129,14 @@ func IndexShow(conf *cfg.Config, index string) error {
|
|||||||
|
|
||||||
slog.Debug("ES result", "index", res)
|
slog.Debug("ES result", "index", res)
|
||||||
|
|
||||||
table := NewTable(2, 5)
|
fields := make([]string, len(res[index].Mappings.Properties))
|
||||||
|
idx := 0
|
||||||
|
for field := range res[index].Mappings.Properties {
|
||||||
|
fields[idx] = field
|
||||||
|
idx++
|
||||||
|
}
|
||||||
|
|
||||||
|
table := printer.NewTable(conf, 2, 5)
|
||||||
table.Addheaders("index property", "value")
|
table.Addheaders("index property", "value")
|
||||||
|
|
||||||
ts, err := strconv.ParseInt(res[index].Settings.Index.CreationDate.(string), 10, 64)
|
ts, err := strconv.ParseInt(res[index].Settings.Index.CreationDate.(string), 10, 64)
|
||||||
@@ -138,15 +146,16 @@ func IndexShow(conf *cfg.Config, index string) error {
|
|||||||
|
|
||||||
created := time.Unix(ts/1000, 0)
|
created := time.Unix(ts/1000, 0)
|
||||||
|
|
||||||
table.entries = [][]string{
|
table.Entries = [][]string{
|
||||||
{"name", index},
|
{"name", index},
|
||||||
{"replicas", *res[index].Settings.Index.NumberOfReplicas},
|
{"replicas", *res[index].Settings.Index.NumberOfReplicas},
|
||||||
{"shards", *res[index].Settings.Index.NumberOfShards},
|
{"shards", *res[index].Settings.Index.NumberOfShards},
|
||||||
{"created", created.Format("2006-01-02 15:04:05")},
|
{"created", created.Format("2006-01-02 15:04:05")},
|
||||||
{"uuid", *res[index].Settings.Index.Uuid},
|
{"uuid", *res[index].Settings.Index.Uuid},
|
||||||
|
{"fields", strings.Join(fields, ",")},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,7 +256,7 @@ func IndexAllocation(conf *cfg.Config, index string) error {
|
|||||||
|
|
||||||
currentNode := res.CurrentNode
|
currentNode := res.CurrentNode
|
||||||
|
|
||||||
table := NewTable(2, 10)
|
table := printer.NewTable(conf, 2, 10)
|
||||||
table.Addheaders("index allocation setting", "value")
|
table.Addheaders("index allocation setting", "value")
|
||||||
|
|
||||||
roles := make([]string, len(currentNode.Roles))
|
roles := make([]string, len(currentNode.Roles))
|
||||||
@@ -255,7 +264,7 @@ func IndexAllocation(conf *cfg.Config, index string) error {
|
|||||||
roles[idx] = role.Name
|
roles[idx] = role.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
table.entries = [][]string{
|
table.Entries = [][]string{
|
||||||
{"Index", index},
|
{"Index", index},
|
||||||
{"Current node", currentNode.Name},
|
{"Current node", currentNode.Name},
|
||||||
{"Current k8s node", currentNode.Attributes["k8s_node_name"]},
|
{"Current k8s node", currentNode.Attributes["k8s_node_name"]},
|
||||||
@@ -268,7 +277,7 @@ func IndexAllocation(conf *cfg.Config, index string) error {
|
|||||||
{"Can remain on current node", res.CanRemainOnCurrentNode.Name},
|
{"Can remain on current node", res.CanRemainOnCurrentNode.Name},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,15 +24,15 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"codeberg.org/scip/esctl/pkg/printer"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FIXME: add filter support, see IndexCreate mapping
|
// FIXME: add filter support, see IndexCreate mapping
|
||||||
func IndexAliasCreate(conf *cfg.Config, index, alias string) error {
|
func IndexAliasCreate(conf *cfg.Config, index, alias string) error {
|
||||||
create := conf.DefaultCluster.ES.Indices.PutAlias(index, alias).
|
res, err := conf.DefaultCluster.ES.Indices.PutAlias(index, alias).
|
||||||
Header("content-type", "application/json").
|
Header("content-type", "application/json").
|
||||||
Header("accept", "application/json")
|
Header("accept", "application/json").
|
||||||
|
Do(context.Background())
|
||||||
res, err := create.Do(context.Background())
|
|
||||||
|
|
||||||
slog.Debug("create alias", "result", res)
|
slog.Debug("create alias", "result", res)
|
||||||
|
|
||||||
@@ -51,12 +51,11 @@ func IndexAliasList(conf *cfg.Config) error {
|
|||||||
filter = *regexp.MustCompile(conf.Filter[0])
|
filter = *regexp.MustCompile(conf.Filter[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
create := conf.DefaultCluster.ES.Indices.GetAlias().
|
res, err := conf.DefaultCluster.ES.Indices.GetAlias().
|
||||||
Index("_all").
|
Index("_all").
|
||||||
Header("content-type", "application/json").
|
Header("content-type", "application/json").
|
||||||
Header("accept", "application/json")
|
Header("accept", "application/json").
|
||||||
|
Do(context.Background())
|
||||||
res, err := create.Do(context.Background())
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to list index aliases: %s", err)
|
return fmt.Errorf("failed to list index aliases: %s", err)
|
||||||
@@ -78,12 +77,12 @@ func IndexAliasList(conf *cfg.Config) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
table := NewTable(2, len(aliaslist))
|
table := printer.NewTable(conf, 2, len(aliaslist))
|
||||||
table.Addheaders("index", "alias")
|
table.Addheaders("index", "alias")
|
||||||
|
|
||||||
idx := 0
|
idx := 0
|
||||||
for index, aliases := range aliaslist {
|
for index, aliases := range aliaslist {
|
||||||
table.entries[idx] = []string{
|
table.Entries[idx] = []string{
|
||||||
index,
|
index,
|
||||||
strings.Join(aliases, ","),
|
strings.Join(aliases, ","),
|
||||||
}
|
}
|
||||||
@@ -93,9 +92,24 @@ func IndexAliasList(conf *cfg.Config) error {
|
|||||||
|
|
||||||
table.Sort()
|
table.Sort()
|
||||||
|
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IndexAliasDelete(conf *cfg.Config, index, alias string) error {
|
||||||
|
res, err := conf.DefaultCluster.ES.Indices.DeleteAlias(index, alias).
|
||||||
|
Header("content-type", "application/json").
|
||||||
|
Header("accept", "application/json").
|
||||||
|
Do(context.Background())
|
||||||
|
|
||||||
|
slog.Debug("delete alias", "result", res)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to delete index alias: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"codeberg.org/scip/esctl/pkg/printer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NodeList(conf *cfg.Config) error {
|
func NodeList(conf *cfg.Config) error {
|
||||||
@@ -33,11 +34,11 @@ func NodeList(conf *cfg.Config) error {
|
|||||||
|
|
||||||
slog.Debug("ES result", "nodes", nodes)
|
slog.Debug("ES result", "nodes", nodes)
|
||||||
|
|
||||||
table := NewTable(7, len(nodes))
|
table := printer.NewTable(conf, 7, len(nodes))
|
||||||
table.Addheaders("name", "ip", "load1m", "load5m", "load15m", "ram %", "heap %")
|
table.Addheaders("name", "ip", "load1m", "load5m", "load15m", "ram %", "heap %")
|
||||||
|
|
||||||
for idx, node := range nodes {
|
for idx, node := range nodes {
|
||||||
table.entries[idx] = []string{
|
table.Entries[idx] = []string{
|
||||||
*node.Name,
|
*node.Name,
|
||||||
*node.Ip,
|
*node.Ip,
|
||||||
*node.Load1M,
|
*node.Load1M,
|
||||||
@@ -49,7 +50,7 @@ func NodeList(conf *cfg.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
table.Sort()
|
table.Sort()
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,12 +20,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/core/search"
|
"github.com/tidwall/gjson"
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
|
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -35,40 +32,19 @@ Execute an ES search.
|
|||||||
additional filters can be given as -F key=value
|
additional filters can be given as -F key=value
|
||||||
*/
|
*/
|
||||||
func Search(conf *cfg.Config, queries []string) error {
|
func Search(conf *cfg.Config, queries []string) error {
|
||||||
query := esdsl.NewBoolQuery()
|
search := conf.DefaultCluster.ES.Search().
|
||||||
|
Index(conf.Index)
|
||||||
|
|
||||||
for _, q := range queries {
|
if len(queries) > 0 {
|
||||||
parts := strings.Split(q, "=")
|
req, err := prepareQuery(conf, queries)
|
||||||
if len(parts) != 2 {
|
if err != nil {
|
||||||
return fmt.Errorf("search queries must be in the form field=pattern")
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
query.Must(esdsl.NewMatchQuery(parts[0], parts[1]))
|
search.Request(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
filters := make([]types.QueryVariant, len(conf.Filter))
|
res, err := search.Do(context.Background())
|
||||||
|
|
||||||
for idx, filter := range conf.Filter {
|
|
||||||
parts := strings.Split(filter, "=")
|
|
||||||
if len(parts) != 2 {
|
|
||||||
return fmt.Errorf("invalid filter spec: %s, expecting key=value", filter)
|
|
||||||
}
|
|
||||||
|
|
||||||
filters[idx] = esdsl.NewTermQuery(parts[0], esdsl.NewFieldValue().String(parts[1]))
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(filters) > 0 {
|
|
||||||
query.Filter(filters...)
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := conf.DefaultCluster.ES.Search().
|
|
||||||
Index(conf.Index).
|
|
||||||
Request(&search.Request{
|
|
||||||
Query: query.QueryCaster(),
|
|
||||||
From: &conf.From,
|
|
||||||
Size: &conf.To,
|
|
||||||
}).
|
|
||||||
Do(context.Background())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to run search (esdsl): %s", err)
|
return fmt.Errorf("failed to run search (esdsl): %s", err)
|
||||||
}
|
}
|
||||||
@@ -76,7 +52,18 @@ func Search(conf *cfg.Config, queries []string) error {
|
|||||||
slog.Debug("ES result", "search", res)
|
slog.Debug("ES result", "search", res)
|
||||||
|
|
||||||
for _, hit := range res.Hits.Hits {
|
for _, hit := range res.Hits.Hits {
|
||||||
fmt.Printf("%s\n", hit.Source_)
|
docjson := fmt.Sprintf(`{"id":%s, "score":%0.4f, "index":"%s", "source":%s}`,
|
||||||
|
*hit.Id_,
|
||||||
|
*hit.Score_,
|
||||||
|
hit.Index_,
|
||||||
|
hit.Source_)
|
||||||
|
|
||||||
|
if conf.Path != "" {
|
||||||
|
value := gjson.Get(docjson, conf.Path)
|
||||||
|
fmt.Println(value.String())
|
||||||
|
} else {
|
||||||
|
fmt.Println(docjson)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
146
pkg/es/search_filter.go
Normal file
146
pkg/es/search_filter.go
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
/*
|
||||||
|
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 es
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"github.com/elastic/go-elasticsearch/v9/typedapi/core/search"
|
||||||
|
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
|
||||||
|
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Fmust = iota
|
||||||
|
Fmustnot
|
||||||
|
Fshould
|
||||||
|
)
|
||||||
|
|
||||||
|
// hold filter configuration, can be used multiple times through a search
|
||||||
|
type filter struct {
|
||||||
|
term, filter string // message=foo
|
||||||
|
mterm []string // multi_match
|
||||||
|
criteria int // must(=), mustnot(!=), should(?)
|
||||||
|
multi bool // message,title=foo => filter:foo, multi: []string{"message","title"}
|
||||||
|
}
|
||||||
|
|
||||||
|
// build a new filter object
|
||||||
|
func NewFilter(query string) (*filter, error) {
|
||||||
|
var separator string
|
||||||
|
var criteria int // we use the constants on top for this
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case strings.Contains(query, "!="):
|
||||||
|
criteria = Fmustnot
|
||||||
|
separator = "!="
|
||||||
|
case strings.Contains(query, "?"):
|
||||||
|
criteria = Fshould
|
||||||
|
separator = "?"
|
||||||
|
default:
|
||||||
|
criteria = Fmust
|
||||||
|
separator = "="
|
||||||
|
}
|
||||||
|
|
||||||
|
part := strings.Split(query, separator)
|
||||||
|
if len(part) != 2 {
|
||||||
|
return nil, fmt.Errorf("search queries must be in the form field<sep>pattern where <sep> must be one of: =, !=, ?")
|
||||||
|
}
|
||||||
|
|
||||||
|
f := &filter{term: part[0], filter: part[1], criteria: criteria}
|
||||||
|
|
||||||
|
if strings.Contains(part[0], ",") {
|
||||||
|
// a MultiMatchQuery, match across multiple fields at once
|
||||||
|
multi := strings.Split(part[0], ",")
|
||||||
|
f.multi = true
|
||||||
|
f.mterm = multi
|
||||||
|
}
|
||||||
|
|
||||||
|
return f, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepare q user search query and turn it into a proper search request
|
||||||
|
func prepareQuery(conf *cfg.Config, queries []string) (*search.Request, error) {
|
||||||
|
if len(queries) == 0 {
|
||||||
|
// nothing given, just return all docs, if any
|
||||||
|
return &search.Request{
|
||||||
|
Query: esdsl.NewMatchAllQuery().QueryCaster(),
|
||||||
|
From: &conf.From,
|
||||||
|
Size: &conf.To,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(queries) == 1 && !strings.ContainsAny(queries[0], "!=?") {
|
||||||
|
// a general query w/o fields, search across all fields
|
||||||
|
return &search.Request{
|
||||||
|
Query: esdsl.NewSimpleQueryStringQuery(queries[0]).QueryCaster(),
|
||||||
|
From: &conf.From,
|
||||||
|
Size: &conf.To,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// complex query, form a proper query struct
|
||||||
|
query := esdsl.NewBoolQuery()
|
||||||
|
|
||||||
|
for _, q := range queries {
|
||||||
|
filter, err := NewFilter(q)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// by default we match on a single field
|
||||||
|
var match types.QueryVariant = esdsl.NewMatchQuery(filter.term, filter.filter)
|
||||||
|
|
||||||
|
if filter.multi {
|
||||||
|
// ok, match across multiple given fields
|
||||||
|
match = esdsl.NewMultiMatchQuery(filter.filter).Fields(filter.mterm...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// apply logic
|
||||||
|
switch filter.criteria {
|
||||||
|
case Fmustnot:
|
||||||
|
query.MustNot(match)
|
||||||
|
case Fmust:
|
||||||
|
query.Must(match)
|
||||||
|
case Fshould:
|
||||||
|
query.Should(match)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// there might be boolean filters as well
|
||||||
|
filters := make([]types.QueryVariant, len(conf.Filter))
|
||||||
|
|
||||||
|
for idx, filter := range conf.Filter {
|
||||||
|
parts := strings.Split(filter, "=")
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return nil, fmt.Errorf("invalid filter spec: %s, expecting key=value", filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
filters[idx] = esdsl.NewTermQuery(parts[0], esdsl.NewFieldValue().String(parts[1]))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(filters) > 0 {
|
||||||
|
query.Filter(filters...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &search.Request{
|
||||||
|
Query: query.QueryCaster(),
|
||||||
|
From: &conf.From,
|
||||||
|
Size: &conf.To,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
@@ -22,10 +22,11 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"codeberg.org/scip/esctl/pkg/printer"
|
||||||
"github.com/elastic/go-elasticsearch/v9/typedapi/cat/shards"
|
"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
|
color := "red" // in case of RELOCATING and UNASSIGNED
|
||||||
|
|
||||||
switch state {
|
switch state {
|
||||||
@@ -35,7 +36,7 @@ func colorzizeShard(state, name string) string {
|
|||||||
color = "yellow"
|
color = "yellow"
|
||||||
}
|
}
|
||||||
|
|
||||||
return Colorize(color, name)
|
return printer.Colorize(conf, color, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolvePrirep(state string) string {
|
func resolvePrirep(state string) string {
|
||||||
@@ -102,13 +103,13 @@ func printShards(conf *cfg.Config, shardlist shards.Response) error {
|
|||||||
headers = append(headers, "node", "ip")
|
headers = append(headers, "node", "ip")
|
||||||
}
|
}
|
||||||
|
|
||||||
table := NewTable(len(headers), len(shardlist))
|
table := printer.NewTable(conf, len(headers), len(shardlist))
|
||||||
table.Addheaders(headers...)
|
table.Addheaders(headers...)
|
||||||
|
|
||||||
for idx, shard := range shardlist {
|
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,
|
name,
|
||||||
*shard.Shard,
|
*shard.Shard,
|
||||||
resolvePrirep(*shard.Prirep),
|
resolvePrirep(*shard.Prirep),
|
||||||
@@ -118,7 +119,7 @@ func printShards(conf *cfg.Config, shardlist shards.Response) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if conf.Verbose {
|
if conf.Verbose {
|
||||||
table.entries[idx] = append(table.entries[idx],
|
table.Entries[idx] = append(table.Entries[idx],
|
||||||
*shard.Node,
|
*shard.Node,
|
||||||
*shard.Ip,
|
*shard.Ip,
|
||||||
)
|
)
|
||||||
@@ -126,7 +127,7 @@ func printShards(conf *cfg.Config, shardlist shards.Response) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
table.Sort()
|
table.Sort()
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"codeberg.org/scip/esctl/pkg/printer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -83,11 +84,11 @@ func SnapshotList(conf *cfg.Config) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
table := NewTable(5, len(snapshots))
|
table := printer.NewTable(conf, 5, len(snapshots))
|
||||||
table.Addheaders("name", "index", "start", "orphaned", "status")
|
table.Addheaders("name", "index", "start", "orphaned", "status")
|
||||||
|
|
||||||
for idx, snap := range snapshots {
|
for idx, snap := range snapshots {
|
||||||
table.entries[idx] = []string{
|
table.Entries[idx] = []string{
|
||||||
snap.Name,
|
snap.Name,
|
||||||
snap.Forindex,
|
snap.Forindex,
|
||||||
snap.Start,
|
snap.Start,
|
||||||
@@ -97,7 +98,7 @@ func SnapshotList(conf *cfg.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
table.Sort()
|
table.Sort()
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,12 +117,12 @@ func SnapshotShow(conf *cfg.Config, snapshot string) error {
|
|||||||
return errors.New("no snapshot retrieved")
|
return errors.New("no snapshot retrieved")
|
||||||
}
|
}
|
||||||
|
|
||||||
table := NewTable(2, 17)
|
table := printer.NewTable(conf, 2, 17)
|
||||||
table.Addheaders("snapshot property", "value")
|
table.Addheaders("snapshot property", "value")
|
||||||
|
|
||||||
snap := res.Snapshots[0]
|
snap := res.Snapshots[0]
|
||||||
|
|
||||||
table.entries = [][]string{
|
table.Entries = [][]string{
|
||||||
{"snapshot", snapshot},
|
{"snapshot", snapshot},
|
||||||
{"uuid", snap.Uuid},
|
{"uuid", snap.Uuid},
|
||||||
{"repository", *snap.Repository},
|
{"repository", *snap.Repository},
|
||||||
@@ -137,7 +138,7 @@ func SnapshotShow(conf *cfg.Config, snapshot string) error {
|
|||||||
{"shards-successful", fmt.Sprintf("%d", snap.Shards.Successful)},
|
{"shards-successful", fmt.Sprintf("%d", snap.Shards.Successful)},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := table.PrintMarkdown(); err != nil {
|
if err := table.Print(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
114
pkg/es/table.go
114
pkg/es/table.go
@@ -1,114 +0,0 @@
|
|||||||
/*
|
|
||||||
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 es
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/olekukonko/tablewriter"
|
|
||||||
"github.com/olekukonko/tablewriter/renderer"
|
|
||||||
"github.com/olekukonko/tablewriter/tw"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Table struct {
|
|
||||||
headers []string
|
|
||||||
entries [][]string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTable(columns, rows int) *Table {
|
|
||||||
table := Table{}
|
|
||||||
|
|
||||||
table.headers = make([]string, columns)
|
|
||||||
table.entries = make([][]string, rows)
|
|
||||||
|
|
||||||
return &table
|
|
||||||
}
|
|
||||||
|
|
||||||
func (data *Table) PrintMarkdown() error {
|
|
||||||
tableString := &strings.Builder{}
|
|
||||||
|
|
||||||
table := tablewriter.NewTable(tableString,
|
|
||||||
tablewriter.WithRenderer(
|
|
||||||
renderer.NewBlueprint(
|
|
||||||
tw.Rendition{
|
|
||||||
Borders: tw.Border{
|
|
||||||
Left: tw.On,
|
|
||||||
Right: tw.On,
|
|
||||||
Top: tw.Off,
|
|
||||||
Bottom: tw.Off,
|
|
||||||
},
|
|
||||||
Settings: tw.Settings{
|
|
||||||
Separators: tw.Separators{
|
|
||||||
ShowHeader: tw.On,
|
|
||||||
ShowFooter: tw.Off,
|
|
||||||
BetweenRows: tw.Off,
|
|
||||||
BetweenColumns: 0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Symbols: tw.NewSymbols(tw.StyleMarkdown),
|
|
||||||
})),
|
|
||||||
|
|
||||||
tablewriter.WithConfig(
|
|
||||||
tablewriter.Config{
|
|
||||||
Header: tw.CellConfig{
|
|
||||||
Formatting: tw.CellFormatting{
|
|
||||||
Alignment: tw.AlignLeft,
|
|
||||||
AutoFormat: tw.Off,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Row: tw.CellConfig{
|
|
||||||
Formatting: tw.CellFormatting{
|
|
||||||
Alignment: tw.AlignLeft,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
table.Header(data.headers)
|
|
||||||
|
|
||||||
if err := table.Bulk(data.entries); err != nil {
|
|
||||||
return fmt.Errorf("failed to add data to table renderer: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := table.Render(); err != nil {
|
|
||||||
return fmt.Errorf("failed to render table: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(tableString.String())
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (data *Table) Sort() {
|
|
||||||
// sanity checks
|
|
||||||
if len(data.entries) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(data.entries, func(i, j int) bool {
|
|
||||||
return data.entries[i][0] < data.entries[j][0]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (data *Table) Addheaders(headers ...string) {
|
|
||||||
for idx, header := range headers {
|
|
||||||
data.headers[idx] = bold(strings.ToUpper(header))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -15,9 +15,12 @@ You should have received a copy of the GNU General Public License
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package es
|
package printer
|
||||||
|
|
||||||
import "github.com/fatih/color"
|
import (
|
||||||
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"github.com/fatih/color"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
green = color.New(color.BgGreen, color.FgHiWhite).SprintFunc()
|
green = color.New(color.BgGreen, color.FgHiWhite).SprintFunc()
|
||||||
@@ -26,7 +29,12 @@ var (
|
|||||||
bold = color.New(color.Bold).SprintFunc()
|
bold = color.New(color.Bold).SprintFunc()
|
||||||
)
|
)
|
||||||
|
|
||||||
func Colorize(col, what string) string {
|
func Colorize(conf *cfg.Config, col, what string) string {
|
||||||
|
switch conf.Output {
|
||||||
|
case "json", "yaml":
|
||||||
|
return what
|
||||||
|
}
|
||||||
|
|
||||||
switch col {
|
switch col {
|
||||||
case "green":
|
case "green":
|
||||||
what = green(what)
|
what = green(what)
|
||||||
306
pkg/printer/table.go
Normal file
306
pkg/printer/table.go
Normal file
@@ -0,0 +1,306 @@
|
|||||||
|
/*
|
||||||
|
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 printer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
|
"github.com/olekukonko/tablewriter"
|
||||||
|
"github.com/olekukonko/tablewriter/renderer"
|
||||||
|
"github.com/olekukonko/tablewriter/tw"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Table struct {
|
||||||
|
Mode string // table, markdown, json
|
||||||
|
Headers []string
|
||||||
|
Entries [][]string
|
||||||
|
|
||||||
|
lenHeaders []int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTable(conf *cfg.Config, columns, rows int) *Table {
|
||||||
|
table := Table{Mode: conf.Output}
|
||||||
|
|
||||||
|
table.Headers = make([]string, columns)
|
||||||
|
table.Entries = make([][]string, rows)
|
||||||
|
table.lenHeaders = make([]int, columns)
|
||||||
|
|
||||||
|
return &table
|
||||||
|
}
|
||||||
|
|
||||||
|
func (data *Table) Print() error {
|
||||||
|
switch data.Mode {
|
||||||
|
case "markdown", "md":
|
||||||
|
return data.PrintMarkdown()
|
||||||
|
case "json":
|
||||||
|
return data.PrintJSON()
|
||||||
|
case "yaml":
|
||||||
|
return data.PrintYAML()
|
||||||
|
default:
|
||||||
|
return data.PrintTSV()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ansiCtrlSeq = regexp.MustCompile(`\033.[0-9;]+m`)
|
||||||
|
)
|
||||||
|
|
||||||
|
// needed for json and yaml output
|
||||||
|
func (data *Table) toMap() []map[string]string {
|
||||||
|
raw := make([]map[string]string, len(data.Entries))
|
||||||
|
|
||||||
|
for idx, entries := range data.Entries {
|
||||||
|
raw[idx] = make(map[string]string, len(data.Headers))
|
||||||
|
for eidx, entry := range entries {
|
||||||
|
raw[idx][data.Headers[eidx]] = entry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return raw
|
||||||
|
}
|
||||||
|
|
||||||
|
func (data *Table) PrintYAML() error {
|
||||||
|
raw := data.toMap()
|
||||||
|
|
||||||
|
body, err := yaml.Marshal(raw)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to produce YAML output: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(string(body))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (data *Table) PrintJSON() error {
|
||||||
|
raw := data.toMap()
|
||||||
|
|
||||||
|
body, err := json.MarshalIndent(raw, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to produce JSON output: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(string(body))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Well. tablewriter should be able to print in TSV mode, but it
|
||||||
|
// doesn't work because of the various ansic color escapes we're
|
||||||
|
// using. Therefore we do it the hard way and use our own TSV printer.
|
||||||
|
func (data *Table) PrintTSV() error {
|
||||||
|
// determine lenght's
|
||||||
|
for idx, head := range data.Headers {
|
||||||
|
data.lenHeaders[idx] = visibleLen(head)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entries := range data.Entries {
|
||||||
|
for idx, entry := range entries {
|
||||||
|
length := visibleLen(entry)
|
||||||
|
if data.lenHeaders[idx] < length {
|
||||||
|
data.lenHeaders[idx] = length
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// output
|
||||||
|
for idx, header := range data.Headers {
|
||||||
|
fmt.Print(header, strings.Repeat(" ", data.lenHeaders[idx]-visibleLen(header)))
|
||||||
|
if idx < len(data.Headers)-1 {
|
||||||
|
fmt.Print(" ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
for _, entries := range data.Entries {
|
||||||
|
for idx, entry := range entries {
|
||||||
|
length := visibleLen(entry)
|
||||||
|
|
||||||
|
if isInt(entry) {
|
||||||
|
// align right
|
||||||
|
fmt.Print(strings.Repeat(" ", data.lenHeaders[idx]-length), entry)
|
||||||
|
} else {
|
||||||
|
fmt.Print(entry, strings.Repeat(" ", data.lenHeaders[idx]-length))
|
||||||
|
}
|
||||||
|
|
||||||
|
if idx < len(data.Headers)-1 {
|
||||||
|
fmt.Print(" ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (data *Table) PrintTabularFail() error {
|
||||||
|
OFS := " "
|
||||||
|
|
||||||
|
tableString := &strings.Builder{}
|
||||||
|
|
||||||
|
styleTSV := tw.NewSymbolCustom("space").WithColumn("\t")
|
||||||
|
|
||||||
|
table := tablewriter.NewTable(tableString,
|
||||||
|
tablewriter.WithRenderer(
|
||||||
|
renderer.NewBlueprint(tw.Rendition{
|
||||||
|
Borders: tw.BorderNone,
|
||||||
|
Symbols: styleTSV,
|
||||||
|
Settings: tw.Settings{
|
||||||
|
Separators: tw.SeparatorsNone,
|
||||||
|
Lines: tw.LinesNone,
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
tablewriter.WithConfig(tablewriter.Config{
|
||||||
|
Header: tw.CellConfig{
|
||||||
|
Formatting: tw.CellFormatting{
|
||||||
|
AutoFormat: tw.Off,
|
||||||
|
},
|
||||||
|
Padding: tw.CellPadding{Global: tw.Padding{Left: "", Right: OFS}},
|
||||||
|
},
|
||||||
|
Row: tw.CellConfig{
|
||||||
|
Formatting: tw.CellFormatting{
|
||||||
|
AutoWrap: tw.WrapNone,
|
||||||
|
Alignment: tw.AlignLeft,
|
||||||
|
},
|
||||||
|
Padding: tw.CellPadding{Global: tw.Padding{Right: OFS}},
|
||||||
|
},
|
||||||
|
|
||||||
|
Debug: true,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
table.Header(data.Headers)
|
||||||
|
|
||||||
|
if err := table.Bulk(data.Entries); err != nil {
|
||||||
|
return fmt.Errorf("failed to add data to table renderer: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := table.Render(); err != nil {
|
||||||
|
return fmt.Errorf("failed to render table: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// we need to trim our output here, because tablewriter appends
|
||||||
|
// excess whitespace to our rows.
|
||||||
|
cleanedString := &strings.Builder{}
|
||||||
|
|
||||||
|
for _, row := range strings.Split(strings.TrimSpace(tableString.String()), "\n") {
|
||||||
|
cleanedString.WriteString(strings.TrimSpace(row))
|
||||||
|
cleanedString.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(cleanedString.String())
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (data *Table) PrintMarkdown() error {
|
||||||
|
tableString := &strings.Builder{}
|
||||||
|
|
||||||
|
table := tablewriter.NewTable(tableString,
|
||||||
|
tablewriter.WithRenderer(
|
||||||
|
renderer.NewBlueprint(
|
||||||
|
tw.Rendition{
|
||||||
|
Borders: tw.Border{
|
||||||
|
Left: tw.On,
|
||||||
|
Right: tw.On,
|
||||||
|
Top: tw.Off,
|
||||||
|
Bottom: tw.Off,
|
||||||
|
},
|
||||||
|
Settings: tw.Settings{
|
||||||
|
Separators: tw.Separators{
|
||||||
|
ShowHeader: tw.On,
|
||||||
|
ShowFooter: tw.Off,
|
||||||
|
BetweenRows: tw.Off,
|
||||||
|
BetweenColumns: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Symbols: tw.NewSymbols(tw.StyleMarkdown),
|
||||||
|
})),
|
||||||
|
|
||||||
|
tablewriter.WithConfig(
|
||||||
|
tablewriter.Config{
|
||||||
|
Header: tw.CellConfig{
|
||||||
|
Formatting: tw.CellFormatting{
|
||||||
|
Alignment: tw.AlignLeft,
|
||||||
|
AutoFormat: tw.Off,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Row: tw.CellConfig{
|
||||||
|
Formatting: tw.CellFormatting{
|
||||||
|
Alignment: tw.AlignLeft,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
table.Header(data.Headers)
|
||||||
|
|
||||||
|
if err := table.Bulk(data.Entries); err != nil {
|
||||||
|
return fmt.Errorf("failed to add data to table renderer: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := table.Render(); err != nil {
|
||||||
|
return fmt.Errorf("failed to render table: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(tableString.String())
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (data *Table) Sort() {
|
||||||
|
// sanity checks
|
||||||
|
if len(data.Entries) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(data.Entries, func(i, j int) bool {
|
||||||
|
return data.Entries[i][0] < data.Entries[j][0]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (data *Table) Addheaders(headers ...string) {
|
||||||
|
for idx, header := range headers {
|
||||||
|
switch data.Mode {
|
||||||
|
case "json", "yaml":
|
||||||
|
data.Headers[idx] = strings.ReplaceAll(strings.ToLower(header), " ", "_")
|
||||||
|
default:
|
||||||
|
data.Headers[idx] = bold(strings.ReplaceAll(strings.ToUpper(header), " ", "-"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the length of a string but only visible chars, w/o ansi color escapes
|
||||||
|
func visibleLen(word string) int {
|
||||||
|
return len(ansiCtrlSeq.ReplaceAllLiteralString(word, ""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func isInt(num string) bool {
|
||||||
|
if _, err := strconv.Atoi(num); err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user