diff --git a/README.md b/README.md index 4296f72..d604377 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Elasticsearch CLI ## Usage -Command tree +Command tree: ```console ccr @@ -34,6 +34,7 @@ Command tree index alias create + delete list allocation close @@ -53,6 +54,7 @@ Command tree snapshot list show + version ``` Configure `esctl` with environment variables: diff --git a/cmd/index_alias.go b/cmd/index_alias.go index 22c7592..eb855f5 100644 --- a/cmd/index_alias.go +++ b/cmd/index_alias.go @@ -35,8 +35,8 @@ func IndexAlias(conf *cfg.Config) *cli.Command { Commands: []*cli.Command{ IndexAliasCreate(conf), IndexAliasList(conf), + IndexAliasDelete(conf), //IndexAliasShow(conf), - //IndexAliasDelete(conf), //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 ", + + 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 { return &cli.Command{ Name: "list", diff --git a/pkg/cfg/config.go b/pkg/cfg/config.go index cc240e3..4cc710d 100644 --- a/pkg/cfg/config.go +++ b/pkg/cfg/config.go @@ -31,10 +31,11 @@ import ( ) const ( - Version string = `v0.0.12` + Version string = `v0.0.13` ) var ( + // initialized during build, see Makefile:buildlocal APIVERSION, GOVERSION, BUILD, COMMIT, BRANCH string ) diff --git a/pkg/es/index_alias.go b/pkg/es/index_alias.go index f59a7a7..06ec477 100644 --- a/pkg/es/index_alias.go +++ b/pkg/es/index_alias.go @@ -24,16 +24,15 @@ import ( "strings" "codeberg.org/scip/esctl/pkg/cfg" - "codeberg.org/scip/esctl/pkg/printer" + "codeberg.org/scip/esctl/pkg/printer" ) // FIXME: add filter support, see IndexCreate mapping 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("accept", "application/json") - - res, err := create.Do(context.Background()) + Header("accept", "application/json"). + Do(context.Background()) slog.Debug("create alias", "result", res) @@ -52,12 +51,11 @@ func IndexAliasList(conf *cfg.Config) error { filter = *regexp.MustCompile(conf.Filter[0]) } - create := conf.DefaultCluster.ES.Indices.GetAlias(). + res, err := conf.DefaultCluster.ES.Indices.GetAlias(). Index("_all"). Header("content-type", "application/json"). - Header("accept", "application/json") - - res, err := create.Do(context.Background()) + Header("accept", "application/json"). + Do(context.Background()) if err != nil { return fmt.Errorf("failed to list index aliases: %s", err) @@ -100,3 +98,18 @@ func IndexAliasList(conf *cfg.Config) error { 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 +}