mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 08:14:18 +02:00
use mapmap.Slicer to filter index templates ls and index ls (#96)
This commit is contained in:
@@ -30,9 +30,11 @@ import (
|
||||
|
||||
"codeberg.org/scip/esctl/pkg/cfg"
|
||||
"codeberg.org/scip/esctl/pkg/printer"
|
||||
"codeberg.org/scip/mapmap"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/cat/indices"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/healthstatus"
|
||||
)
|
||||
|
||||
@@ -53,41 +55,35 @@ func IndexNames(conf *cfg.Config) ([]string, error) {
|
||||
}
|
||||
|
||||
func filterIndices(conf *cfg.Config, list indices.Response) indices.Response {
|
||||
// apply partials filter first
|
||||
selectedlist := indices.Response{}
|
||||
var filter *regexp.Regexp
|
||||
|
||||
if len(conf.Filter) > 0 {
|
||||
filter = regexp.MustCompile(conf.Filter[0])
|
||||
}
|
||||
|
||||
return mapmap.NewSlicer(list).MapSliceValuesImmutable(func(index types.IndicesRecord) bool {
|
||||
fmt.Println(*index.Index)
|
||||
if strings.HasPrefix(*index.Index, ".ds-") {
|
||||
// ignore data stream backing indicies
|
||||
return false
|
||||
}
|
||||
|
||||
for _, index := range list {
|
||||
if !conf.Partials && strings.HasPrefix(*index.Index, "partial-") {
|
||||
continue
|
||||
return false
|
||||
}
|
||||
|
||||
if !conf.Hidden && strings.HasPrefix(*index.Index, ".") {
|
||||
continue
|
||||
return false
|
||||
}
|
||||
|
||||
if strings.HasPrefix(*index.Index, ".ds-") {
|
||||
// ignore data stream backing indicies
|
||||
continue
|
||||
if len(conf.Filter) > 0 {
|
||||
if !filter.MatchString(*index.Index) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
selectedlist = append(selectedlist, index)
|
||||
}
|
||||
|
||||
if len(conf.Filter) == 0 {
|
||||
return selectedlist
|
||||
}
|
||||
|
||||
// we support just one filter here, for now
|
||||
filter := *regexp.MustCompile(conf.Filter[0])
|
||||
newlist := indices.Response{}
|
||||
|
||||
for _, index := range selectedlist {
|
||||
if filter.MatchString(*index.Index) {
|
||||
newlist = append(newlist, index)
|
||||
}
|
||||
}
|
||||
|
||||
return newlist
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func IndexList(conf *cfg.Config) error {
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
|
||||
"codeberg.org/scip/esctl/pkg/cfg"
|
||||
"codeberg.org/scip/esctl/pkg/printer"
|
||||
"codeberg.org/scip/mapmap"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
||||
)
|
||||
@@ -61,31 +62,9 @@ func IndexTemplateList(conf *cfg.Config, filter string) error {
|
||||
table := printer.NewTable(conf, 5, 0)
|
||||
table.Addheaders("name", "description", "index patterns", "priority")
|
||||
|
||||
for _, tpl := range res.IndexTemplates {
|
||||
if !conf.Hidden && strings.HasPrefix(tpl.Name, ".") {
|
||||
continue
|
||||
}
|
||||
|
||||
if filter != "" && !strings.Contains(tpl.Name, filter) {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(conf.Filter) > 0 {
|
||||
skip := true
|
||||
|
||||
for _, filter := range conf.Filter {
|
||||
for _, pattern := range tpl.IndexTemplate.IndexPatterns {
|
||||
if strings.Contains(pattern, filter) {
|
||||
skip = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if skip {
|
||||
continue
|
||||
}
|
||||
}
|
||||
tplList := filterIndexTemplates(conf, filter, res.IndexTemplates)
|
||||
|
||||
for _, tpl := range tplList {
|
||||
desc := strings.TrimPrefix(strings.TrimSuffix(string(tpl.IndexTemplate.Meta_["description"]), `"`), `"`)
|
||||
|
||||
var prio int64
|
||||
@@ -101,6 +80,32 @@ func IndexTemplateList(conf *cfg.Config, filter string) error {
|
||||
return table.Print()
|
||||
}
|
||||
|
||||
// Filter index templates by name, index pattern or hidden flag, using
|
||||
// mapmap.Slicer
|
||||
func filterIndexTemplates(conf *cfg.Config, nameFilter string,
|
||||
templates []types.IndexTemplateItem) []types.IndexTemplateItem {
|
||||
return mapmap.NewSlicer(templates).MapSliceValuesImmutable(func(tpl types.IndexTemplateItem) bool {
|
||||
if !conf.Hidden && strings.HasPrefix(tpl.Name, ".") {
|
||||
return false
|
||||
}
|
||||
|
||||
if nameFilter != "" && !strings.Contains(tpl.Name, nameFilter) {
|
||||
return false
|
||||
}
|
||||
|
||||
if len(conf.Filter) > 0 {
|
||||
if !mapmap.NewSlicer(tpl.IndexTemplate.IndexPatterns).
|
||||
FindSliceInSlice(conf.Filter, func(val, find string) bool {
|
||||
return strings.Contains(val, find)
|
||||
}) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func IndexTemplateShow(conf *cfg.Config, tplname string) error {
|
||||
res, err := conf.DefaultCluster.ES().Indices.GetIndexTemplate().
|
||||
Name(tplname).
|
||||
|
||||
Reference in New Issue
Block a user