-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
88 lines (70 loc) · 2.36 KB
/
main.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
88
package main
import (
"fmt"
"log"
"os"
"github.com/gosuri/uitable"
"github.com/hashicorp/consul/api"
"github.com/jawher/mow.cli"
"github.com/pteich/consul-kv-search/search"
)
var Version string
func main() {
app := cli.App("consul-kv-search", "CLI tool to search for data in Consul K/V store. https://github.com/pteich/consul-kv-search")
app.Version("v version", Version)
app.Spec = "[-a] [-t] [-p] [-r] [-w] [--keys|--values] QUERY"
var (
configConsulAddr = app.StringOpt("a address", "127.0.0.1:8500", "Address and port of Consul server")
configToken = app.StringOpt("t token", "", "Consul ACL token to use")
configPath = app.StringOpt("p path", "/", "K/V path to start search")
configWrap = app.BoolOpt("w wrap", false, "Wrap text in output table")
configQueryRegex = app.BoolOpt("r regex", false, "Query interpreted as regular expression (instead of glob)")
configQueryScopeKeys = app.BoolOpt("keys", false, "Search keys only (default everyhwere)")
configQueryScopeValues = app.BoolOpt("values", false, "Search values only (default everyhwere)")
configQuery = app.StringArg("QUERY", "*", "Search query")
)
app.Action = func() {
consulConfig := api.DefaultConfig()
consulConfig.Address = *configConsulAddr
consulConfig.Token = *configToken
consulClient, err := api.NewClient(consulConfig)
if err != nil {
log.Fatalf("could not connect to Consul at %s - %v", *configConsulAddr, err)
}
consulSearch := search.New(consulClient)
var foundPairs []search.ResultPair
scope := search.Everywhere
if *configQueryScopeKeys {
scope = search.Keys
} else if *configQueryScopeValues {
scope = search.Values
}
if *configQueryRegex {
foundPairs, err = consulSearch.SearchRegex(*configQuery, *configPath, scope)
} else {
foundPairs, err = consulSearch.SearchGlob(*configQuery, *configPath, scope)
}
if err != nil {
log.Fatal(err)
}
found := len(foundPairs)
if found <= 0 {
fmt.Println("0 entries found")
return
}
fmt.Printf("%d entries found\n\n", found)
table := uitable.New()
table.MaxColWidth = 100
table.Wrap = *configWrap
table.Separator = " | "
for _, element := range foundPairs {
table.AddRow(element.Key, element.Value)
table.AddRow("")
}
fmt.Println(table)
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}