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
/
Copy pathsnapshots.go
72 lines (62 loc) · 1.41 KB
/
snapshots.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
package main
import (
"fmt"
"log"
"regexp"
"os"
)
var cmdSnapshots = &Command{
Run: runSnapshots,
Usage: "snapshots <repo> [<pattern>]",
Short: "list all snapshots in a repo",
Long: `
Prints a list of all snapshots in a repo matching the specified pattern.
Example:
$ es snapshots nfs
logstash_1
fluentd_1
$ es snapshots nfs 'logstash.*'
logstash_1
`,
ApiUrl: "http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_snapshot",
}
func init() {
// parse args here, if necessary
}
func runSnapshots(cmd *Command, args []string) {
if len(args) < 1 {
cmd.printUsage()
os.Exit(1)
}
repo := args[0]
var pattern = ""
if len(args) > 1 {
pattern = args[1]
}
type snapshot struct {
Snapshot string `json:"snapshot"`
}
var response struct {
Snapshots []snapshot `json:"snapshots,omitempty"`
Error string `json:"error,omitempty"`
Status int `json:"status,omitempty"`
}
ESReq("GET", "/_snapshot/"+repo+"/_all").Do(&response)
if len(response.Error) > 0 {
log.Fatalf("Error: %v (%v)\n", response.Error, response.Status)
} else {
for _, snapshot := range response.Snapshots {
if len(pattern) > 0 {
matched, err := regexp.MatchString(pattern, snapshot.Snapshot)
if err != nil {
log.Fatal("invalid pattern")
}
if matched {
fmt.Println(snapshot.Snapshot)
}
} else {
fmt.Println(snapshot.Snapshot)
}
}
}
}