Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,27 @@ linters:
- canonicalheader
- clickhouselint
- containedctx
#- contextcheck
- copyloopvar
#- cyclop
- decorder
- dogsled
- dupword
- durationcheck
- embeddedstructfieldcheck
- errchkjson
- errname
#- errorlint
- exhaustive
- exptostd
- fatcontext
- funcorder
#- funlen
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoinits
- gochecksumtype
#- gocognit
- gocritic
#- gocyclo
#- godoclint
- goheader
- gomoddirectives
- gomodguard_v2
- goprintffuncname
#- gosec
- gosmopolitan
- grouper
- iface
Expand All @@ -58,7 +50,6 @@ linters:
- misspell
- modernize
- nakedret
#- nestif
- nilerr
- nilnesserr
- nlreturn
Expand All @@ -67,12 +58,10 @@ linters:
- paralleltest
- perfsprint
- prealloc
#- predeclared
- promlinter
- protogetter
- reassign
- recvcheck
#- revive
- rowserrcheck
- sloglint
- spancheck
Expand Down Expand Up @@ -107,4 +96,5 @@ linters:
- fd
- id
- fn
- ds

5 changes: 2 additions & 3 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/es"

"github.com/samber/lo"
"github.com/urfave/cli/v3"
)

Expand Down Expand Up @@ -55,9 +56,7 @@ func complete(conf *cfg.Config, cmd *cli.Command, what int) {
case Crole:
list, err = es.RoleNames(conf)
case Ccluster:
for cluster := range conf.Clusters {
list = append(list, cluster)
}
list = lo.Keys(conf.Clusters)
case Cdatastream:
list, err = es.DatastreamNames(conf)
case Capi:
Expand Down
2 changes: 1 addition & 1 deletion cmd/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func DatastreamList(conf *cfg.Config) *cli.Command {
},
&cli.StringSliceFlag{
Name: "filter",
Usage: "show only data streams matching the filter",
Usage: "show only data streams matching the filter regexp",
Destination: &conf.Filter,
Aliases: []string{"F"},
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func IndexList(conf *cfg.Config) *cli.Command {
},
&cli.StringSliceFlag{
Name: "filter",
Usage: "show only indices matching the filter",
Usage: "show only indices matching the filter regexp",
Destination: &conf.Filter,
Aliases: []string{"F"},
},
Expand Down Expand Up @@ -239,10 +239,10 @@ func IndexFields(conf *cfg.Config) *cli.Command {
Aliases: []string{"s"},
},
&cli.StringSliceFlag{
Name: "type",
Usage: "show only fields of this type",
Name: "filter",
Usage: "show only fields matching this filter regexp.",
Destination: &conf.Filter,
Aliases: []string{"t"},
Aliases: []string{"F"},
},
},

Expand Down
2 changes: 1 addition & 1 deletion cmd/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func RoleList(conf *cfg.Config) *cli.Command {
Name: "orphaned",
Usage: "include only orphaned roles",
Destination: &conf.Failed,
Aliases: []string{"o"},
Aliases: []string{"O"},
},
},

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module codeberg.org/scip/esctl
go 1.26

require (
codeberg.org/scip/mapmap v0.0.2
github.com/MichaelMure/go-term-markdown v0.1.4
github.com/alecthomas/repr v0.5.3
github.com/charmbracelet/bubbles v1.0.0
Expand All @@ -32,6 +31,7 @@ require (
github.com/go-openapi/spec v0.22.5
github.com/go-openapi/swag/loading v0.26.1
github.com/mattn/go-isatty v0.0.22
github.com/samber/lo v1.53.0
github.com/tidwall/gjson v1.19.0
github.com/tlinden/yadu v0.1.3
github.com/urfave/cli/v3 v3.10.1-0.20260623012112-f980ca84bf65
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
codeberg.org/scip/mapmap v0.0.2 h1:0i61jOUwFmGVwPukrMzrx0Fi4T9Qru2nlmibuaJimBo=
codeberg.org/scip/mapmap v0.0.2/go.mod h1:/ojYo2P7dMA2FWEu+jHKmsKPeq5yDcCvXeHevqZd5OI=
github.com/MichaelMure/go-term-markdown v0.1.4 h1:Ir3kBXDUtOX7dEv0EaQV8CNPpH+T7AfTh0eniMOtNcs=
github.com/MichaelMure/go-term-markdown v0.1.4/go.mod h1:EhcA3+pKYnlUsxYKBJ5Sn1cTQmmBMjeNlpV8nRb+JxA=
github.com/MichaelMure/go-term-text v0.3.1 h1:Kw9kZanyZWiCHOYu9v/8pWEgDQ6UVN9/ix2Vd2zzWf0=
Expand Down Expand Up @@ -153,6 +151,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/samber/lo v1.53.0 h1:t975lj2py4kJPQ6haz1QMgtId2gtmfktACxIXArw3HM=
github.com/samber/lo v1.53.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
33 changes: 23 additions & 10 deletions pkg/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"path/filepath"
"reflect"
"regexp"
"time"

"github.com/alecthomas/repr"
Expand Down Expand Up @@ -79,16 +80,17 @@ type Config struct {
Refresh bool // index copy: -r
SourceIndices []string // index copy: -s

From, To, MaxItems int // search: flags
Filter []string // search: -F
Path string // search+doc sh: -p
Subhelp bool // search+doc sh: -H
Tail bool // search: -f [tail]
Or bool // search: -O
Range string // search: -r
TimestampFormat string // search: --timestamp-format
Explain bool // search: -e
Validate bool // search: --validate
From, To, MaxItems int // search: flags
Filter []string // search: -F
FilterRex []*regexp.Regexp // compiled .Filter
Path string // search+doc sh: -p
Subhelp bool // search+doc sh: -H
Tail bool // search: -f [tail]
Or bool // search: -O
Range string // search: -r
TimestampFormat string // search: --timestamp-format
Explain bool // search: -e
Validate bool // search: --validate

SortBy string // sort: -k
Ascending bool // sort: -a
Expand Down Expand Up @@ -161,6 +163,8 @@ func (conf *Config) Init() error {
return err
}

conf.compileFilters()

conf.HaveJQ = isJQinstalled()

conf.PrintDebug()
Expand Down Expand Up @@ -303,6 +307,15 @@ func (conf *Config) determineDefaultCluster() error {
return nil
}

// compileFilters pre-compiles regexp filters
func (conf *Config) compileFilters() {
conf.FilterRex = make([]*regexp.Regexp, len(conf.Filter))

for i, f := range conf.Filter {
conf.FilterRex[i] = regexp.MustCompile(f)
}
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if err != nil {
Expand Down
51 changes: 25 additions & 26 deletions pkg/es/cluster_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"codeberg.org/scip/esctl/pkg/printer"
"github.com/elastic/go-elasticsearch/v9/typedapi/cluster/health"
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
"github.com/samber/lo"
)

const (
Expand Down Expand Up @@ -153,14 +154,12 @@ func findIlmErrors(conf *cfg.Config, leader, follower string) bool {
}

if len(failed[cluster]) > 0 {
idx := 0
table := printer.NewTable(conf).WithSize(2, len(failed[cluster])).
WithHeaders("ilm errors on "+which, "errors")

for name, count := range failed[cluster] {
table.Entries[idx] = []any{name, count}
idx++
}
table.Entries = lo.MapToSlice(failed[cluster], func(name string, count any) []any {
return []any{name, count}
})

table.Sort()

Expand Down Expand Up @@ -227,16 +226,16 @@ func findIndicesOnlyOnLeader(conf *cfg.Config, indices ClusterIndices, leader, f
return true
}

idx := 0
table := printer.NewTable(conf).WithSize(3, len(indexOnlyOnLeader)).
WithHeaders("index only on leader", "size", "docscount")

for name, index := range indexOnlyOnLeader {
name := printer.Colorize(conf, "red", name)

table.Entries[idx] = []any{name, *index.DatasetSize, *index.DocsCount}
idx++
}
table.Entries = lo.MapToSlice(indexOnlyOnLeader, func(name string, index *types.IndicesRecord) []any {
return []any{
printer.Colorize(conf, "red", name),
*index.DatasetSize,
*index.DocsCount,
}
})

table.Sort()

Expand Down Expand Up @@ -277,16 +276,16 @@ func findOrphanedIndices(conf *cfg.Config, indices ClusterIndices, leader, follo
return true
}

idx := 0
table := printer.NewTable(conf).WithSize(3, len(orphaned)).
WithHeaders("orphaned index on follower", "size", "docscount")

for name, index := range orphaned {
name := printer.Colorize(conf, "red", name)

table.Entries[idx] = []any{name, *index.DatasetSize, *index.DocsCount}
idx++
}
table.Entries = lo.MapToSlice(orphaned, func(name string, index *types.IndicesRecord) []any {
return []any{
printer.Colorize(conf, "red", name),
*index.DatasetSize,
*index.DocsCount,
}
})

table.Sort()

Expand All @@ -313,16 +312,16 @@ func findFailedFollowerIndices(conf *cfg.Config, indices ClusterIndices, followe
return true
}

idx := 0
table := printer.NewTable(conf).WithSize(3, len(red)).
WithHeaders("red index on follower", "size", "docscount")

for name, index := range red {
name := printer.Colorize(conf, "red", name)

table.Entries[idx] = []any{name, *index.DatasetSize, *index.DocsCount}
idx++
}
table.Entries = lo.MapToSlice(red, func(name string, index *types.IndicesRecord) []any {
return []any{
printer.Colorize(conf, "red", name),
*index.DatasetSize,
*index.DocsCount,
}
})

table.Sort()

Expand Down
39 changes: 11 additions & 28 deletions pkg/es/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
"errors"
"fmt"
"log/slog"
"regexp"

"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/printer"
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
"github.com/samber/lo"
)

// DatastreamNames returns a list of datastream names for completion
Expand All @@ -38,12 +38,9 @@ func DatastreamNames(conf *cfg.Config) ([]string, error) {
return nil, fmt.Errorf("failed to get data streams: %w", esErrorString(err))
}

dss := make([]string, len(res.DataStreams))
for idx, ds := range res.DataStreams {
dss[idx] = ds.Name
}

return dss, nil
return lo.Map(res.DataStreams, func(d types.DataStream, _ int) string {
return d.Name
}), nil
}

// DatastreamList list datastreams
Expand Down Expand Up @@ -228,29 +225,15 @@ func DatastreamRollover(conf *cfg.Config, ds string) error {

// filterDatastreams return a filtered list of datastreams
func filterDatastreams(conf *cfg.Config, list []types.DataStream) []types.DataStream {
selected := []types.DataStream{}

for _, ds := range list {
if !conf.Hidden && ds.Hidden {
continue
return lo.Filter(list, func(ds types.DataStream, _ int) bool {
if ds.Hidden {
return conf.Hidden
}

selected = append(selected, ds)
}

if len(conf.Filter) == 0 {
return selected
}

// we support just one filter here, for now
filter := *regexp.MustCompile(conf.Filter[0])
newlist := []types.DataStream{}

for _, ds := range selected {
if filter.MatchString(ds.Name) {
newlist = append(newlist, ds)
if len(conf.Filter) == 0 {
return true
}
}

return newlist
return filterWithRex(conf, ds.Name)
})
}
Loading