From b73cb87f128e28ce679cce383fb7b90b77a6ae87 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Wed, 15 Jul 2026 14:51:12 +0200 Subject: [PATCH] add index template completion, index pattern filter and name filter --- cmd/completion.go | 3 +++ cmd/index_template.go | 21 +++++++++++++--- pkg/es/index_template.go | 54 ++++++++++++++++++++++++++++++++++------ 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/cmd/completion.go b/cmd/completion.go index ca647d0..cf6c1a8 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -34,6 +34,7 @@ const ( Cilm Cnode Cclustersettings + Cindextemplate ) func complete(conf *cfg.Config, cmd *cli.Command, what int) { @@ -65,6 +66,8 @@ func complete(conf *cfg.Config, cmd *cli.Command, what int) { list, err = es.NodeNames(conf) case Cclustersettings: list, err = es.ClusterSettingsNames(conf) + case Cindextemplate: + list, err = es.IndexTemplateNames(conf) } if err != nil { diff --git a/cmd/index_template.go b/cmd/index_template.go index 0dc2891..14aa8f0 100644 --- a/cmd/index_template.go +++ b/cmd/index_template.go @@ -49,8 +49,23 @@ func IndexTemplateList(conf *cfg.Config) *cli.Command { Aliases: []string{"ls"}, Usage: "list index templates", + Flags: []cli.Flag{ + &cli.StringSliceFlag{ + Name: "filter-index-pattern", + Usage: "show only index templates which use patterns, which match the filter", + Destination: &conf.Filter, + Aliases: []string{"F"}, + }, + &cli.BoolFlag{ + Name: "hidden", + Usage: "show hidden index templates as well", + Destination: &conf.Hidden, + Aliases: []string{"H"}, + }, + }, + Action: func(ctx context.Context, cmd *cli.Command) error { - return es.IndexTemplateList(conf) + return es.IndexTemplateList(conf, cmd.Args().Get(0)) }, } } @@ -71,7 +86,7 @@ func IndexTemplateShow(conf *cfg.Config) *cli.Command { }, ShellComplete: func(ctx context.Context, cmd *cli.Command) { - complete(conf, cmd, Cindex) + complete(conf, cmd, Cindextemplate) }, } } @@ -215,7 +230,7 @@ func IndexTemplateDelete(conf *cfg.Config) *cli.Command { }, ShellComplete: func(ctx context.Context, cmd *cli.Command) { - complete(conf, cmd, Cindex) + complete(conf, cmd, Cindextemplate) }, } } diff --git a/pkg/es/index_template.go b/pkg/es/index_template.go index 1eaed07..bc2dfa0 100644 --- a/pkg/es/index_template.go +++ b/pkg/es/index_template.go @@ -33,7 +33,23 @@ import ( ) // used for completion -func IndexTemplateList(conf *cfg.Config) error { +func IndexTemplateNames(conf *cfg.Config) ([]string, error) { + res, err := conf.DefaultCluster.ES().Indices.GetIndexTemplate(). + Do(context.Background()) + if err != nil { + return nil, fmt.Errorf("failed to get index templates: %w", esErrorString(err)) + } + + names := make([]string, len(res.IndexTemplates)) + + for idx, tpl := range res.IndexTemplates { + names[idx] = tpl.Name + } + + return names, nil +} + +func IndexTemplateList(conf *cfg.Config, filter string) error { res, err := conf.DefaultCluster.ES().Indices.GetIndexTemplate(). Do(context.Background()) if err != nil { @@ -42,20 +58,42 @@ func IndexTemplateList(conf *cfg.Config) error { slog.Debug("res", "index templates", res) - table := printer.NewTable(conf, 5, len(res.IndexTemplates)) - table.Addheaders("name", "description", "priority") + table := printer.NewTable(conf, 5, 0) + table.Addheaders("name", "description", "index patterns", "priority") - for idx, tpl := range res.IndexTemplates { - desc, err := json.Marshal(tpl.IndexTemplate.Meta_["description"]) - if err != nil { - return fmt.Errorf("failed to unmarshal meta json data: %w", err) + 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 + } + } + + desc := strings.TrimPrefix(strings.TrimSuffix(string(tpl.IndexTemplate.Meta_["description"]), `"`), `"`) + var prio int64 if tpl.IndexTemplate.Priority != nil { prio = *tpl.IndexTemplate.Priority } - table.Entries[idx] = []any{tpl.Name, desc, prio} + + table.AddRow(tpl.Name, desc, tpl.IndexTemplate.IndexPatterns, prio) } table.Sort()