/* 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" "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 indices", Commands: []*cli.Command{ IndexList(conf), IndexShow(conf), IndexCreate(conf, false), IndexCreate(conf, true), IndexDelete(conf), IndexClose(conf), IndexFields(conf), IndexIlm(conf), IndexDu(conf), IndexCopy(conf), // sub commands IndexAlias(conf), IndexTemplate(conf), }, } } func IndexList(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "list", Aliases: []string{"ls"}, Usage: "list indices", 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 indices", Destination: &conf.Partials, Aliases: []string{"p"}, }, &cli.BoolFlag{ Name: "hidden", Usage: "include hidden indices", Destination: &conf.Hidden, Aliases: []string{"H"}, }, &cli.BoolFlag{ Name: "failed", Usage: "include only red failed indices", Destination: &conf.Failed, Aliases: []string{"f"}, }, &cli.StringSliceFlag{ Name: "filter", Usage: "show only indices matching the filter", Destination: &conf.Filter, Aliases: []string{"F"}, }, }, Action: func(ctx context.Context, cmd *cli.Command) error { return es.IndexList(conf) }, } } 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(conf, cmd, Cindex) }, } } func IndexCreate(conf *cfg.Config, modify bool) *cli.Command { name := "create" usage := "create a new index" if modify { name = "update" usage = "update an index" } return &cli.Command{ Name: name, Aliases: []string{"+"}, Usage: usage, UsageText: name + ` ... 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"}, Value: 1, }, &cli.StringFlag{ Name: "ilm-policy", Usage: "ilm policy to apply", Destination: &conf.Policy, Aliases: []string{"p"}, }, }, Action: func(ctx context.Context, cmd *cli.Command) error { args := cmd.Args() if args.Len() == 0 { return errors.New("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(conf, 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(conf, 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 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(conf, 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) }, } } func IndexIlm(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "ilm", Usage: "show ilm status", UsageText: "index ilm ", ShellComplete: func(ctx context.Context, cmd *cli.Command) { complete(conf, 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.IlmExplain(conf, index) }, } } func IndexDu(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "du", Usage: "show index disk usage", UsageText: "index du ", ShellComplete: func(ctx context.Context, cmd *cli.Command) { complete(conf, 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.IndexDiskusage(conf, index) }, } } func IndexCopy(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "copy", Aliases: []string{"alias", "cp"}, Usage: "copy (reindex) documents from one index to another", UsageText: "index copy [options] -s -t ", Flags: []cli.Flag{ // FIXME: not implemented by typed API // &cli.BoolFlag{ // Name: "missing", // op_type // Usage: "copy only missing docs", // Destination: &conf.Missing, // Aliases: []string{"m"}, // }, // &cli.BoolFlag{ // Name: "sync", // version_type to external // Usage: "create missing docs and update outdated docs", // Destination: &conf.Sync, // Aliases: []string{"S"}, // }, &cli.BoolFlag{ Name: "force", // conflicts to proceed Usage: "continue reindexing even when conflicts happen", Destination: &conf.Force, Aliases: []string{"f"}, }, &cli.Float64Flag{ Name: "requests-per-second", Usage: "the maximum number of documents to index per second (-1 turns off throttling)", Destination: &conf.RequestsPerSecond, Aliases: []string{"R"}, }, &cli.Int64Flag{ Name: "max-docs", Usage: "the maximum number of documents to reindex", Destination: &conf.MaxDocs, Aliases: []string{"m"}, }, &cli.DurationFlag{ Name: "timeout", Usage: "timeout for write operations (e.g. 300m or 120s)", Destination: &conf.Timeout, Aliases: []string{"T"}, }, &cli.BoolFlag{ Name: "refresh", Usage: "refresh affected shards to make this operation visible to search", Destination: &conf.Refresh, Aliases: []string{"r"}, }, &cli.BoolFlag{ Name: "wait", Usage: "wait for active shards", Destination: &conf.Wait, Aliases: []string{"w"}, }, &cli.StringSliceFlag{ Name: "source", Usage: "source index (multiple supported)", Destination: &conf.SourceIndices, Aliases: []string{"s"}, Required: true, }, &cli.StringFlag{ Name: "target", Usage: "target index", Destination: &conf.Index, Aliases: []string{"t"}, Required: true, }, }, Action: func(ctx context.Context, cmd *cli.Command) error { return es.IndexCopy(conf) }, } }