add index alias delete (#20)

This commit is contained in:
T. von Dein
2026-05-21 14:00:35 +02:00
parent 0870ceb3df
commit 89e2498aad
4 changed files with 48 additions and 12 deletions

View File

@@ -6,7 +6,7 @@ Elasticsearch CLI
## Usage ## Usage
Command tree Command tree:
```console ```console
ccr ccr
@@ -34,6 +34,7 @@ Command tree
index index
alias alias
create create
delete
list list
allocation allocation
close close
@@ -53,6 +54,7 @@ Command tree
snapshot snapshot
list list
show show
version
``` ```
Configure `esctl` with environment variables: Configure `esctl` with environment variables:

View File

@@ -35,8 +35,8 @@ func IndexAlias(conf *cfg.Config) *cli.Command {
Commands: []*cli.Command{ Commands: []*cli.Command{
IndexAliasCreate(conf), IndexAliasCreate(conf),
IndexAliasList(conf), IndexAliasList(conf),
IndexAliasDelete(conf),
//IndexAliasShow(conf), //IndexAliasShow(conf),
//IndexAliasDelete(conf),
//IndexAliasAdd(conf), // see https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-update-aliases //IndexAliasAdd(conf), // see https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-update-aliases
}, },
} }
@@ -62,6 +62,26 @@ func IndexAliasCreate(conf *cfg.Config) *cli.Command {
} }
} }
func IndexAliasDelete(conf *cfg.Config) *cli.Command {
return &cli.Command{
Name: "delete",
Aliases: []string{"rm"},
Usage: "delete an index alias",
UsageText: "delete <index> <alias>",
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 IndexAliasList(conf *cfg.Config) *cli.Command { func IndexAliasList(conf *cfg.Config) *cli.Command {
return &cli.Command{ return &cli.Command{
Name: "list", Name: "list",

View File

@@ -31,10 +31,11 @@ import (
) )
const ( const (
Version string = `v0.0.12` Version string = `v0.0.13`
) )
var ( var (
// initialized during build, see Makefile:buildlocal
APIVERSION, GOVERSION, BUILD, COMMIT, BRANCH string APIVERSION, GOVERSION, BUILD, COMMIT, BRANCH string
) )

View File

@@ -29,11 +29,10 @@ import (
// FIXME: add filter support, see IndexCreate mapping // FIXME: add filter support, see IndexCreate mapping
func IndexAliasCreate(conf *cfg.Config, index, alias string) error { func IndexAliasCreate(conf *cfg.Config, index, alias string) error {
create := conf.DefaultCluster.ES.Indices.PutAlias(index, alias). res, err := conf.DefaultCluster.ES.Indices.PutAlias(index, alias).
Header("content-type", "application/json"). Header("content-type", "application/json").
Header("accept", "application/json") Header("accept", "application/json").
Do(context.Background())
res, err := create.Do(context.Background())
slog.Debug("create alias", "result", res) slog.Debug("create alias", "result", res)
@@ -52,12 +51,11 @@ func IndexAliasList(conf *cfg.Config) error {
filter = *regexp.MustCompile(conf.Filter[0]) filter = *regexp.MustCompile(conf.Filter[0])
} }
create := conf.DefaultCluster.ES.Indices.GetAlias(). res, err := conf.DefaultCluster.ES.Indices.GetAlias().
Index("_all"). Index("_all").
Header("content-type", "application/json"). Header("content-type", "application/json").
Header("accept", "application/json") Header("accept", "application/json").
Do(context.Background())
res, err := create.Do(context.Background())
if err != nil { if err != nil {
return fmt.Errorf("failed to list index aliases: %s", err) return fmt.Errorf("failed to list index aliases: %s", err)
@@ -100,3 +98,18 @@ func IndexAliasList(conf *cfg.Config) error {
return nil return nil
} }
func IndexAliasDelete(conf *cfg.Config, index, alias string) error {
res, err := conf.DefaultCluster.ES.Indices.DeleteAlias(index, alias).
Header("content-type", "application/json").
Header("accept", "application/json").
Do(context.Background())
slog.Debug("delete alias", "result", res)
if err != nil {
return fmt.Errorf("failed to delete index alias: %s", err)
}
return nil
}