This repository has been archived by the owner on Sep 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index_aliases.go
88 lines (73 loc) · 1.78 KB
/
index_aliases.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"log"
"regexp"
)
var cmdIndexAliases = &Command{
Run: runIndexAliases,
Usage: "index-aliases [<pattern>]",
Short: "list all aliases of an index",
Long: `
Prints a list of all aliases of an index matching the specified pattern.
Example:
$ es index-aliases
master -> alias1, alias2
marvel
$ es index-aliases 'mas.*'
master -> alias1, alias2
`,
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases.html",
}
func runIndexAliases(cmd *Command, args []string) {
var pattern = ""
if len(args) > 0 {
pattern = args[0]
}
var response map[string]interface{}
ESReq("GET", "/_cluster/state?pretty=1").Do(&response)
error, hasError := response["error"];
if hasError {
status, _ := response["status"]
log.Fatalf("Error: %v (%v)\n", error, status)
}
metadata, ok := response["metadata"].(map[string]interface{})
if !ok {
log.Fatalf("Could not find metadata element in response\n")
}
indices, ok := metadata["indices"].(map[string]interface{})
if ok {
for indexName, _ := range indices {
if len(pattern) > 0 {
matched, err := regexp.MatchString(pattern, indexName)
if err != nil {
log.Fatal("unvalid pattern")
}
if matched {
printAliasesOfIndex(indexName, indices)
}
} else {
printAliasesOfIndex(indexName, indices)
}
}
} else {
// No indices
}
}
func printAliasesOfIndex(indexName string, indices map[string]interface{}) {
fmt.Printf(indexName)
if index, ok := indices[indexName].(map[string]interface{}); ok {
aliases, ok := index["aliases"].([]interface{})
if ok {
for i, alias := range aliases {
var aliasName = alias.(string)
if i == 0 {
fmt.Printf(" -> %s", aliasName)
} else {
fmt.Printf(", %s", aliasName)
}
}
}
}
fmt.Println()
}