/
searchCmd.go
141 lines (125 loc) · 4.34 KB
/
searchCmd.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Copyright © 2020 Platform9 Systems
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"net/url"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/platform9/grafanactl/pkg/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func prepareTable(searchResults []client.GrafanaSearchHit) *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Id", "Title", "Tags", "isStarred"})
for _, hit := range searchResults {
id := strconv.FormatUint(uint64(hit.ID), 10)
isStarred := strconv.FormatBool(hit.IsStarred)
tags := strings.Join(hit.Tags, ", ")
table.Append([]string{id, hit.Title, tags, isStarred})
}
return table
}
// merges two int slices, ensuring that no duplicate ints are in the resulting slice
// May be combined with mergeStringSlices(..) if we can figure out how to handle multiple
// types without too much interface{} juggling
func mergeIntSlices(slice1 []int, slice2 []int) []int {
for _, item2 := range slice2 {
for _, item1 := range slice1 {
if item1 == item2 {
// item2 exists already, go to next item2
break
}
}
// item2 doesn't exist, append to the slice
slice1 = append(slice1, item2)
}
return slice1
}
// merges two string slices, ensuring that no duplicate ints are in the resulting slice
// May be combined with mergeIntSlices(..) if we can figure out how to handle multiple
// types without too much interface{} juggling
func mergeStringSlices(slice1 []string, slice2 []string) []string {
for _, item2 := range slice2 {
for _, item1 := range slice1 {
if item1 == item2 {
// item2 exists already, go to next item2
break
}
}
// item2 doesn't exist, append to the slice
slice1 = append(slice1, item2)
}
return slice1
}
func getSearchParams(cmd *cobra.Command, args []string) url.Values {
params := url.Values{}
// query
if len(args) > 0 {
params.Set("query", strings.Join(args, " "))
}
// prefer flag to positional args
query := viper.GetString("query")
if query != "" {
params.Set("query", query)
}
// tags
// Note: viper.GetStringSlice() does not seem to properly bind the flag
// as a workaround, we will merge the lists ourselves
tagsViper := viper.GetStringSlice("tag")
tagsCobra, _ := cmd.Flags().GetStringSlice("tag")
tags := mergeStringSlices(tagsViper, tagsCobra)
if len(tags) > 0 {
for _, tag := range tags {
params.Add("tag", tag)
}
}
// dashboardIds
// Note: viper.GetIntSlice() does not seem to properly bind the flag
// as a workaround, we will merge the lists ourselves
dashboardsViper := viper.GetIntSlice("dashboard")
dashboardsCobra, _ := cmd.Flags().GetIntSlice("dashboard")
dashboards := mergeIntSlices(dashboardsViper, dashboardsCobra)
if len(dashboards) > 0 {
for _, id := range dashboards {
params.Add("dashboardIds", strconv.FormatInt(int64(id), 10))
}
}
// folderIds
// Note: viper.GetIntSlice() does not seem to properly bind the flag
// as a workaround, we will merge the lists ourselves
foldersViper := viper.GetIntSlice("folder")
foldersCobra, _ := cmd.Flags().GetIntSlice("folder")
folders := mergeIntSlices(foldersViper, foldersCobra)
if len(folders) > 0 {
for _, id := range folders {
params.Add("folderIds", strconv.FormatInt(int64(id), 10))
}
}
// starred
starred := viper.GetBool("starred")
if starred {
params.Set("starred", strconv.FormatBool(starred))
}
return params
}
func loadSearchFlags(cmd *cobra.Command) {
cmd.Flags().StringP("query", "q", "", "Search Query")
cmd.Flags().StringSliceP("tag", "t", []string{}, "List of tags to search for.")
cmd.Flags().IntSliceP("dashboard", "d", []int{}, "List of dashboard id's to search for")
cmd.Flags().IntSliceP("folder", "f", []int{}, "List of folder id's to search in for dashboards")
cmd.Flags().Bool("starred", false, "Flag indicating if only starred Dashboards should be returned")
viper.BindPFlags(cmd.Flags())
}