mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 21:44:19 +02:00
173 lines
4.3 KiB
Go
173 lines
4.3 KiB
Go
/*
|
|
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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"codeberg.org/scip/esctl/pkg/cfg"
|
|
"codeberg.org/scip/esctl/pkg/es"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
func IndexAlias(conf *cfg.Config) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "alias",
|
|
Aliases: []string{"a"},
|
|
Usage: "manage index aliases",
|
|
|
|
Commands: []*cli.Command{
|
|
IndexAliasCreate(conf),
|
|
IndexAliasList(conf),
|
|
IndexAliasDelete(conf),
|
|
IndexAliasRollover(conf),
|
|
// FIXME: implement IndexAliasAdd
|
|
// see https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-update-aliases
|
|
// IndexAliasAdd(conf),
|
|
},
|
|
}
|
|
}
|
|
|
|
func IndexAliasCreate(conf *cfg.Config) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "create",
|
|
Aliases: []string{"+"},
|
|
Usage: "create an index alias",
|
|
UsageText: "create <index> <alias>",
|
|
|
|
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)
|
|
alias := cmd.Args().Get(1)
|
|
|
|
if index == "" || alias == "" {
|
|
return errors.New("no index or alias specified")
|
|
}
|
|
|
|
return es.IndexAliasCreate(conf, index, alias)
|
|
},
|
|
}
|
|
}
|
|
|
|
func IndexAliasDelete(conf *cfg.Config) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "delete",
|
|
Aliases: []string{"rm"},
|
|
Usage: "delete an index alias",
|
|
UsageText: "delete <index> <alias>",
|
|
|
|
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)
|
|
alias := cmd.Args().Get(1)
|
|
|
|
if index == "" || alias == "" {
|
|
return errors.New("no index or alias specified")
|
|
}
|
|
|
|
return es.IndexAliasDelete(conf, index, alias)
|
|
},
|
|
}
|
|
}
|
|
|
|
func IndexAliasRollover(conf *cfg.Config) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "rollover",
|
|
Aliases: []string{"roll"},
|
|
Usage: "roll over an index alias",
|
|
UsageText: "rollover <alias>",
|
|
|
|
ShellComplete: func(ctx context.Context, cmd *cli.Command) {
|
|
complete(conf, cmd, Cindex)
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
|
&cli.BoolFlag{
|
|
Name: "wait",
|
|
Usage: "wait for active shards",
|
|
Destination: &conf.Wait,
|
|
Aliases: []string{"w"},
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "dry-run",
|
|
Usage: "checks conditions but does not perform a rollover",
|
|
Destination: &conf.Wait,
|
|
Aliases: []string{"n"},
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "max-age",
|
|
Usage: "roll over after max age (eg: 7d, 2m, 8h)",
|
|
Destination: &conf.MaxAge,
|
|
},
|
|
&cli.Int64Flag{
|
|
Name: "max-docs",
|
|
Usage: "roll over after max docs",
|
|
Destination: &conf.MaxDocs,
|
|
},
|
|
&cli.IntFlag{
|
|
Name: "max-primary-shard-size",
|
|
Usage: "roll over when primary shard size reaches size",
|
|
Destination: &conf.MaxShardSize,
|
|
},
|
|
&cli.IntFlag{
|
|
Name: "max-primary-shard-docs",
|
|
Usage: "roll over after max docs in primary shard reached",
|
|
Destination: &conf.MaxShardDocs,
|
|
},
|
|
},
|
|
|
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
|
alias := cmd.Args().Get(0)
|
|
|
|
if alias == "" {
|
|
return errors.New("no alias specified")
|
|
}
|
|
|
|
return es.IndexAliasRollover(conf, alias)
|
|
},
|
|
}
|
|
}
|
|
|
|
func IndexAliasList(conf *cfg.Config) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "list",
|
|
Aliases: []string{"ls"},
|
|
Usage: "list index aliases",
|
|
|
|
Flags: []cli.Flag{
|
|
&cli.StringSliceFlag{
|
|
Name: "filter",
|
|
Usage: "show only aliases for indices matching the filter",
|
|
Destination: &conf.Filter,
|
|
Aliases: []string{"F"},
|
|
},
|
|
},
|
|
|
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
|
return es.IndexAliasList(conf)
|
|
},
|
|
}
|
|
}
|