diff --git a/alias.go b/alias.go index 3afb04b..5e53222 100644 --- a/alias.go +++ b/alias.go @@ -106,8 +106,7 @@ func handleAliasCommand() { replacecmd.SetOutput(os.Stdout) cfgpath := replacecmd.String("config", "", "path to configuration") aname := replacecmd.String("name", "", "(required) name of alias") - oiname := replacecmd.String("oldindex", "", - "(required) name of the old index") + oiname := replacecmd.String("oldindex", "", "name of the old index") niname := replacecmd.String("newindex", "", "(required) name of the new index") replacecmd.Parse(os.Args[3:]) @@ -117,12 +116,7 @@ func handleAliasCommand() { replacecmd.PrintDefaults() os.Exit(1) } - if *oiname == "" { - fmt.Fprintf(os.Stderr, - "oldindex parameter is required for alias replace\n") - replacecmd.PrintDefaults() - os.Exit(1) - } + if *niname == "" { fmt.Fprintf(os.Stderr, "newindex parameter is required for alias replace\n") @@ -133,10 +127,29 @@ func handleAliasCommand() { cfg := LoadConfig(*cfgpath) es := ClientFromConfig(cfg.Elastic) - _, err := es.Alias(). - Remove(*oiname, *aname). - Add(*niname, *aname). - Do(context.Background()) + oinames := make([]string, 0, 10) + if *oiname != "" { + oinames = append(oinames, *oiname) + } else { + aliases, err := es.CatAliases().Do(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error()) + os.Exit(1) + } + + for _, alias := range aliases { + if alias.Alias == *aname { + oinames = append(oinames, alias.Index) + } + } + } + + as := es.Alias() + for _, idx := range oinames { + as.Remove(idx, *aname) + } + as.Add(*niname, *aname) + _, err := as.Do(context.Background()) if err != nil { fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error()) os.Exit(1)