/* 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 . */ package cmd import ( "context" "errors" "fmt" "codeberg.org/scip/esctl/pkg/cfg" "codeberg.org/scip/esctl/pkg/es" "github.com/urfave/cli/v3" ) func Index(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "index", Aliases: []string{"i"}, Usage: "manage indicies", Commands: []*cli.Command{ IndexList(conf), IndexShow(conf), IndexCreate(conf), IndexDelete(conf), IndexClose(conf), IndexAllocation(conf), IndexModify(conf), IndexAlias(conf), IndexFields(conf), }, } } func IndexList(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "list", Aliases: []string{"ls"}, Usage: "list indicies", Flags: []cli.Flag{ &cli.IntFlag{ Name: "max", Usage: "max number of items to process", Destination: &conf.MaxItems, Aliases: []string{"m"}, }, &cli.BoolFlag{ Name: "partials", Usage: "include partial indicies", Destination: &conf.Partials, Aliases: []string{"p"}, }, &cli.BoolFlag{ Name: "reds", Usage: "include only red failed indicies", Destination: &conf.Failed, Aliases: []string{"r"}, }, &cli.StringSliceFlag{ Name: "filter", Usage: "show only indicies matching the filter", Destination: &conf.Filter, Aliases: []string{"F"}, }, }, Action: func(ctx context.Context, cmd *cli.Command) error { return es.IndexList(conf) }, } } func IndexAllocation(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "allocation", Aliases: []string{"a"}, Usage: "explain index allocation", Flags: []cli.Flag{ &cli.IntFlag{ Name: "shard", Usage: "shard number to explain for", Destination: &conf.Shards, Aliases: []string{"s"}, }, &cli.BoolFlag{ Name: "primary", Usage: "explain primary allocation (default true)", Destination: &conf.Primary, Aliases: []string{"p"}, }, }, Action: func(ctx context.Context, cmd *cli.Command) error { index := cmd.Args().Get(0) if index == "" { return errors.New("no index specified") } return es.IndexAllocation(conf, cmd.Args().Get(0)) }, } } func IndexShow(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "show", Aliases: []string{"sh"}, Usage: "show details about an index", Action: func(ctx context.Context, cmd *cli.Command) error { index := cmd.Args().Get(0) if index == "" { return errors.New("no index specified") } return es.IndexShow(conf, cmd.Args().Get(0)) }, ShellComplete: func(ctx context.Context, cmd *cli.Command) { complete(cmd, Cindex) }, } } func IndexCreate(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "create", Aliases: []string{"+"}, Usage: "create a new index", UsageText: `create ... Valid field mapping types: integer, text, date, keyword`, Flags: []cli.Flag{ &cli.BoolFlag{ Name: "wait", Usage: "wait for active shards", Destination: &conf.Wait, Aliases: []string{"w"}, }, &cli.IntFlag{ Name: "shards", Usage: "number of shards to create", Destination: &conf.Shards, Aliases: []string{"s"}, }, &cli.IntFlag{ Name: "replicas", Usage: "number of replicas to create", Destination: &conf.Replicas, Aliases: []string{"r"}, }, }, Action: func(ctx context.Context, cmd *cli.Command) error { args := cmd.Args() if args.Len() == 0 { return fmt.Errorf("no index specified") } mappings := args.Slice()[1:] return es.IndexCreate(conf, args.Get(0), mappings) }, } } func IndexDelete(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "delete", Aliases: []string{"rm"}, Usage: "delete an index", UsageText: "delete ", ShellComplete: func(ctx context.Context, cmd *cli.Command) { complete(cmd, Cindex) }, Action: func(ctx context.Context, cmd *cli.Command) error { index := cmd.Args().Get(0) if index == "" { return errors.New("no index specified") } return es.IndexDelete(conf, cmd.Args().Get(0)) }, } } func IndexClose(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "close", Usage: "close an index", UsageText: "close ", ShellComplete: func(ctx context.Context, cmd *cli.Command) { complete(cmd, Cindex) }, Action: func(ctx context.Context, cmd *cli.Command) error { index := cmd.Args().Get(0) if index == "" { return errors.New("no index specified") } return es.IndexClose(conf, cmd.Args().Get(0)) }, } } func IndexModify(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "modify", Usage: "modify an index", UsageText: "modify ", ShellComplete: func(ctx context.Context, cmd *cli.Command) { complete(cmd, Cindex) }, Flags: []cli.Flag{ &cli.IntFlag{ Name: "replicas", Usage: "number of replicas", Destination: &conf.Replicas, Aliases: []string{"r"}, }, }, Action: func(ctx context.Context, cmd *cli.Command) error { index := cmd.Args().Get(0) if index == "" { return errors.New("no index specified") } return es.IndexModify(conf, index) }, } } func IndexFields(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "fields", Usage: "show info about field capabilities", UsageText: "index fields ", Flags: []cli.Flag{ &cli.BoolFlag{ Name: "aggretable", Usage: "include only aggretable fields", Destination: &conf.Aggretable, Aliases: []string{"a"}, }, &cli.BoolFlag{ Name: "searchable", Usage: "include only searchable fields", Destination: &conf.Searchable, Aliases: []string{"s"}, }, &cli.StringSliceFlag{ Name: "type", Usage: "show only fields of this type", Destination: &conf.Filter, Aliases: []string{"t"}, }, }, ShellComplete: func(ctx context.Context, cmd *cli.Command) { complete(cmd, Cindex) }, Action: func(ctx context.Context, cmd *cli.Command) error { index := cmd.Args().Get(0) if index == "" { return errors.New("no index specified") } return es.IndexFields(conf, index) }, } }