/
root.go
279 lines (247 loc) · 8.8 KB
/
root.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
Copyright © 2020 Maksym Postument 777rip777@gmail.com
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 (
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"github.com/mpostument/grafana-sync/grafana"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
var cfgFile string
var customHeaders map[string]string
var rootCmd = &cobra.Command{
Use: "grafana-sync",
Short: "Root command for grafana interaction",
Long: `Root command for grafana interaction.`,
Version: "1.5.0",
}
var pullDashboardsCmd = &cobra.Command{
Use: "pull-dashboards",
Short: "Pull grafana dashboards in to the directory",
Long: `Save to the directory grafana dashboards.
Directory name specified by flag --directory. If flag --tag is used,
only dashboards with given tag are pulled`,
Run: func(cmd *cobra.Command, args []string) {
var (
folderId int
err error
)
url, _ := cmd.Flags().GetString("url")
apiKey := viper.GetString("apikey")
directory, _ := cmd.Flags().GetString("directory")
tag, _ := cmd.Flags().GetString("tag")
folderName, _ := cmd.Flags().GetString("folderName")
if folderName != "" {
folderId, err = grafana.FindFolderId(url, apiKey, folderName)
if err != nil {
log.Fatalln(err)
}
} else {
folderId, _ = cmd.Flags().GetInt("folderId")
}
if err := grafana.PullDashboard(url, apiKey, directory, tag, folderId); err != nil {
log.Fatalln("Pull dashboards command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}
var pushDashboardsCmd = &cobra.Command{
Use: "push-dashboards",
Short: "Push grafana dashboards from directory",
Long: `Read json with dashboards description and publish to grafana.`,
Run: func(cmd *cobra.Command, args []string) {
var (
folderId int
err error
)
url, _ := cmd.Flags().GetString("url")
apiKey, _ := cmd.Flags().GetString("apikey")
directory, _ := cmd.Flags().GetString("directory")
folderName, _ := cmd.Flags().GetString("folderName")
if folderName != "" {
folderId, err = grafana.FindFolderId(url, apiKey, folderName)
if err != nil {
log.Fatalln(err)
}
} else {
folderId, _ = cmd.Flags().GetInt("folderId")
}
if err := grafana.PushDashboard(url, apiKey, directory, folderId); err != nil {
log.Fatalln("Push dashboards command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}
var pullFoldersCmd = &cobra.Command{
Use: "pull-folders",
Short: "Pull grafana folders json in to the directory",
Long: `Save to the directory grafana folders json.
Directory name specified by flag --directory.`,
Run: func(cmd *cobra.Command, args []string) {
url, _ := cmd.Flags().GetString("url")
apiKey := viper.GetString("apikey")
directory, _ := cmd.Flags().GetString("directory")
if err := grafana.PullFolders(url, apiKey, directory); err != nil {
log.Fatalln("Pull folders command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}
var pushFoldersCmd = &cobra.Command{
Use: "push-folders",
Short: "Read json and create grafana folders",
Long: `Read json with folders description and publish to grafana.`,
Run: func(cmd *cobra.Command, args []string) {
url, _ := cmd.Flags().GetString("url")
apiKey := viper.GetString("apikey")
directory, _ := cmd.Flags().GetString("directory")
if err := grafana.PushFolder(url, apiKey, directory); err != nil {
log.Fatalln("Push folders command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}
var pullNotificationsCmd = &cobra.Command{
Use: "pull-notifications",
Short: "Pull grafana notifications json in to the directory",
Long: `Save to the directory grafana folders json.
Directory name specified by flag --directory.`,
Run: func(cmd *cobra.Command, args []string) {
url, _ := cmd.Flags().GetString("url")
apiKey := viper.GetString("apikey")
directory, _ := cmd.Flags().GetString("directory")
if err := grafana.PullNotifications(url, apiKey, directory); err != nil {
log.Fatalln("Pull notifications command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}
var pushNotificationsCmd = &cobra.Command{
Use: "push-notifications",
Short: "Read json and create grafana notifications",
Long: `Read json with notifications description and publish to grafana.`,
Run: func(cmd *cobra.Command, args []string) {
url, _ := cmd.Flags().GetString("url")
apiKey := viper.GetString("apikey")
directory, _ := cmd.Flags().GetString("directory")
if err := grafana.PushNotification(url, apiKey, directory); err != nil {
log.Fatalln("Push notifications command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}
var pullDataSourcesCmd = &cobra.Command{
Use: "pull-datasources",
Short: "Pull grafana datasources json in to the directory",
Long: `Save to the directory grafana datasources json.
Directory name specified by flag --directory.`,
Run: func(cmd *cobra.Command, args []string) {
url, _ := cmd.Flags().GetString("url")
apiKey := viper.GetString("apikey")
directory, _ := cmd.Flags().GetString("directory")
if err := grafana.PullDatasources(url, apiKey, directory); err != nil {
log.Fatalln("Pull datasources command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}
var pushDataSourcesCmd = &cobra.Command{
Use: "push-datasources",
Short: "Read json and create grafana datasources",
Long: `Read json with datasources description and publish to grafana.`,
Run: func(cmd *cobra.Command, args []string) {
url, _ := cmd.Flags().GetString("url")
apiKey := viper.GetString("apikey")
directory, _ := cmd.Flags().GetString("directory")
if err := grafana.PushDatasources(url, apiKey, directory); err != nil {
log.Fatalln("Push datasources command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.grafana-sync.yaml)")
rootCmd.PersistentFlags().StringP("url", "u", "http://localhost:3000", "Grafana Url with port")
rootCmd.PersistentFlags().StringP("directory", "d", ".", "Directory where to save dashboards")
rootCmd.PersistentFlags().StringP("apikey", "a", "", "Grafana api key")
rootCmd.PersistentFlags().StringToStringVar(&customHeaders, "customHeaders", map[string]string{}, "Key-value pairs of custom http headers (key1=value1,key2=value2)")
pullDataSourcesCmd.PersistentFlags().StringP("tag", "t", "", "Dashboard tag to read")
pushDashboardsCmd.PersistentFlags().IntP("folderId", "f", 0, "Directory Id to which push dashboards")
pushDashboardsCmd.PersistentFlags().StringP("folderName", "n", "", "Directory name to which push dashboards")
pullDashboardsCmd.PersistentFlags().IntP("folderId", "f", -1, "Directory Id from which pull dashboards")
pullDashboardsCmd.PersistentFlags().StringP("folderName", "n", "", "Directory name from which pull dashboards")
pullDashboardsCmd.PersistentFlags().StringP("tag", "t", "", "Dashboard tag to p")
if err := viper.BindPFlag("apikey", rootCmd.PersistentFlags().Lookup("apikey")); err != nil {
log.Println(err)
}
if err := viper.BindPFlag("customHeaders", rootCmd.PersistentFlags().Lookup("customHeaders")); err != nil {
log.Println(err)
}
rootCmd.AddCommand(pullDashboardsCmd)
rootCmd.AddCommand(pushDashboardsCmd)
rootCmd.AddCommand(pullFoldersCmd)
rootCmd.AddCommand(pushFoldersCmd)
rootCmd.AddCommand(pullNotificationsCmd)
rootCmd.AddCommand(pushNotificationsCmd)
rootCmd.AddCommand(pullDataSourcesCmd)
rootCmd.AddCommand(pushDataSourcesCmd)
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".grafana-sync" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".grafana-sync")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
grafana.InitHttpClient(viper.GetStringMapString("customHeaders"))
}