diff --git a/go.mod b/go.mod index 69d834f..4f2a692 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ go 1.26 require ( github.com/MichaelMure/go-term-markdown v0.1.4 - github.com/alecthomas/repr v0.5.2 + github.com/alecthomas/repr v0.5.3 github.com/charmbracelet/bubbles v1.0.0 github.com/charmbracelet/bubbletea v1.3.10 github.com/charmbracelet/lipgloss v1.1.0 @@ -40,6 +40,7 @@ require ( ) require ( + codeberg.org/scip/mapmap v0.0.2 // indirect github.com/MichaelMure/go-term-text v0.3.1 // indirect github.com/alecthomas/chroma v0.7.1 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect diff --git a/go.sum b/go.sum index 5f7026b..c9b40a4 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +codeberg.org/scip/mapmap v0.0.1 h1:L1jMBo/UNp19MXUF/ONjc1XKVGeuF1x05z0pEhjgkzA= +codeberg.org/scip/mapmap v0.0.1/go.mod h1:/ojYo2P7dMA2FWEu+jHKmsKPeq5yDcCvXeHevqZd5OI= +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= @@ -10,8 +14,8 @@ github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721 h1:JHZL0hZKJ1VEN github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0= github.com/alecthomas/kong v0.2.1-0.20190708041108-0548c6b1afae/go.mod h1:+inYUSluD+p4L8KdviBSgzcqEjUQOfC5fQDRFuc36lI= github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= -github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs= -github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= +github.com/alecthomas/repr v0.5.3 h1:Ebk3yZ0kvrHC7TkTHLDJGDq1LxKeu9sQBQREFMcesS8= +github.com/alecthomas/repr v0.5.3/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= github.com/charmbracelet/bubbles v1.0.0 h1:12J8/ak/uCZEMQ6KU7pcfwceyjLlWsDLAxB5fXonfvc= diff --git a/pkg/es/index_template.go b/pkg/es/index_template.go index bc2dfa0..cec4652 100644 --- a/pkg/es/index_template.go +++ b/pkg/es/index_template.go @@ -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).