func (cmd *Alias) Run(args []string) error { only := cmdFlag.Bool("only", true, "") if len(args) < 1 { cmd.Usage(os.Stdout) return nil } indexName := args[0] aliasName := aliasNameFromIndexName(indexName) cmdFlag.Parse(args[1:]) client := elasticsearch.NewClient( elasticsearch.NewConfig().WithURL(*urlStr), ) _, aliased := indexNamesAndAliasedIndexByAliasName(client, aliasName) if len(aliased) == 0 { if err := makeAlias(client, aliasName, indexName); err != nil { return err } fmt.Printf("%s <- %s\n", indexName, aliasName) return nil } if *only { addActions := []Action{ Action{aliasName, indexName}, } removeActions := []Action{} for name, _ := range aliased { if name != indexName { removeActions = append(removeActions, Action{aliasName, name}) } } if err := updateAliases(client, addActions, removeActions); err != nil { return err } } else { if err := makeAlias(client, aliasName, indexName); err != nil { return err } } fmt.Printf("%s <- %s\n", indexName, aliasName) return nil }
func (cmd *Settings) Run(args []string) error { if len(args) < 1 { cmd.Usage(os.Stdout) return nil } indexName := args[0] cmdFlag.Parse(args[1:]) client := elasticsearch.NewClient( elasticsearch.NewConfig().WithURL(*urlStr), ) req := elasticsearch.NewRequest("GET", path.Join(indexName, "_settings"), nil) res, err := client.Do(req) if err != nil { return err } b, _ := json.MarshalIndent(res[indexName], "", " ") fmt.Println(string(b)) return nil }
func (cmd *List) Run(args []string) error { if len(args) < 1 { cmd.Usage(os.Stdout) return nil } aliasName := args[0] cmdFlag.Parse(args[1:]) client := elasticsearch.NewClient( elasticsearch.NewConfig().WithURL(*urlStr), ) indices, aliased := indexNamesAndAliasedIndexByAliasName(client, aliasName) for _, index := range indices { fmt.Printf("%s", index) if aliased[index] { fmt.Printf(" <- %s", aliasName) } fmt.Println("") } return nil }
func (cmd *Create) Run(args []string) error { if len(args) < 1 { cmd.Usage(os.Stdout) return nil } aliasName := args[0] indexName := indexNameFromAliasName(aliasName) cmdFlag.Parse(args[1:]) client := elasticsearch.NewClient( elasticsearch.NewConfig().WithURL(*urlStr), ) req := elasticsearch.NewRequest("PUT", indexName, strings.NewReader(*cmd.mappings)) res, err := client.Do(req) if err != nil { return err } if v, ok := res["acknowledged"].(bool); ok && v { _, aliased := indexNamesAndAliasedIndexByAliasName(client, aliasName) if len(aliased) == 0 { if err := makeAlias(client, aliasName, indexName); err != nil { return err } fmt.Printf("%s <- %s\n", indexName, aliasName) return nil } fmt.Printf("%s\n", indexName) } if v, ok := res["error"].(string); ok && len(v) > 0 { return errors.New(v) } return nil }