-
Notifications
You must be signed in to change notification settings - Fork 94
/
main.go
350 lines (293 loc) · 8.62 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* @license
* Copyright 2020 Dynatrace LLC
* 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 main
import (
"fmt"
"os"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/deploy"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/download"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/util"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/version"
"github.com/spf13/afero"
"github.com/urfave/cli/v2"
)
func main() {
statusCode := Run(os.Args)
os.Exit(statusCode)
}
func Run(args []string) int {
return RunImpl(args, afero.NewOsFs())
}
func RunImpl(args []string, fs afero.Fs) (statusCode int) {
var app *cli.App
if newCli, ok := os.LookupEnv("NEW_CLI"); ok && newCli != "0" {
app = buildExperimentalCli(fs)
} else {
app = buildCli(fs)
}
err := app.Run(args)
if err != nil {
util.Log.Error("%s\n", err)
return 1
}
return 0
}
func buildCli(fs afero.Fs) *cli.App {
fmt.Print(`You are currently using the old CLI structure which will be used by
default until monaco version 2.0.0
Check out the beta of the new CLI by adding the environment variable
"NEW_CLI".
We can't wait for your feedback.
`)
app := cli.NewApp()
app.ArgsUsage = "[working directory]"
app.Usage = "Automates the deployment of Dynatrace Monitoring Configuration to one or multiple Dynatrace environments."
app.Version = version.MonitoringAsCode
cli.VersionPrinter = func(c *cli.Context) {
fmt.Println(c.App.Version)
}
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Usage: "print the version",
}
app.Description = `
Tool used to deploy dynatrace configurations via the cli
Examples:
Deploy a specific project inside a root config folder:
monaco -p='project-folder' -e='environments.yaml' projects-root-folder
Deploy a specific project to a specific tenant:
monaco --environments environments.yaml --specific-environment dev --project myProject
`
app.Before = func(c *cli.Context) error {
err := util.SetupLogging(c.Bool("verbose"))
if err != nil {
return err
}
util.Log.Info("Dynatrace Monitoring as Code v" + version.MonitoringAsCode)
return nil
}
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
},
&cli.PathFlag{
Name: "environments",
Usage: "Yaml file containing environments to deploy to",
Aliases: []string{"e"},
Required: true,
TakesFile: true,
},
&cli.StringFlag{
Name: "specific-environment",
Usage: "Specific environment (from list) to deploy to",
Aliases: []string{"se"},
DefaultText: "none",
},
&cli.StringFlag{
Name: "project",
Usage: "Project configuration to deploy (also deploys any dependent configurations)",
Aliases: []string{"p"},
DefaultText: "none",
},
&cli.BoolFlag{
Name: "dry-run",
Aliases: []string{"d"},
Usage: "Switches to just validation instead of actual deployment",
},
&cli.BoolFlag{
Name: "continue-on-error",
Usage: "Proceed deployment even if config upload fails",
Aliases: []string{"c"},
},
}
app.Action = func(ctx *cli.Context) error {
if ctx.NArg() > 1 {
util.Log.Error("Too many arguments! Either specify a relative path to the working directory, or omit it for using the current working directory.")
cli.ShowAppHelpAndExit(ctx, 1)
}
var workingDir string
if ctx.Args().Present() {
workingDir = ctx.Args().First()
} else {
workingDir = "."
}
return deploy.Deploy(
workingDir,
fs,
ctx.Path("environments"),
ctx.String("specific-environment"),
ctx.String("project"),
ctx.Bool("dry-run"),
ctx.Bool("continue-on-error"),
)
}
return app
}
func buildExperimentalCli(fs afero.Fs) *cli.App {
fmt.Print(`You are using the new CLI structure which is currently in Beta.
Please provide feedback here:
https://github.com/dynatrace-oss/dynatrace-monitoring-as-code/issues/45.
We plan to make this CLI GA in version 2.0.0
`)
app := cli.NewApp()
app.Usage = "Automates the deployment of Dynatrace Monitoring Configuration to one or multiple Dynatrace environments."
app.Version = version.MonitoringAsCode
cli.VersionPrinter = func(c *cli.Context) {
fmt.Println(c.App.Version)
}
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Usage: "print the version",
}
app.Description = `
Tool used to deploy dynatrace configurations via the cli
Examples:
Deploy a specific project inside a root config folder:
monaco deploy -p='project-folder' -e='environments.yaml' projects-root-folder
Deploy a specific project to a specific tenant:
monaco deploy --environments environments.yaml --specific-environment dev --project myProject
`
deployCommand := getDeployCommand(fs)
downloadCommand := getDownloadCommand(fs)
app.Commands = []*cli.Command{&deployCommand, &downloadCommand}
return app
}
func getDeployCommand(fs afero.Fs) cli.Command {
command := cli.Command{
Name: "deploy",
Usage: "deploys the given environment",
UsageText: "deploy [command options] [working directory]",
ArgsUsage: "[working directory]",
Before: func(c *cli.Context) error {
err := util.SetupLogging(c.Bool("verbose"))
if err != nil {
return err
}
util.Log.Info("Dynatrace Monitoring as Code v" + version.MonitoringAsCode)
return nil
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
},
&cli.PathFlag{
Name: "environments",
Usage: "Yaml file containing environment to deploy to",
Aliases: []string{"e"},
Required: true,
TakesFile: true,
},
&cli.StringFlag{
Name: "specific-environment",
Usage: "Specific environment (from list) to deploy to",
Aliases: []string{"s"},
},
&cli.StringFlag{
Name: "project",
Usage: "Project configuration to deploy (also deploys any dependent configurations)",
Aliases: []string{"p"},
},
&cli.BoolFlag{
Name: "dry-run",
Aliases: []string{"d"},
Usage: "Switches to just validation instead of actual deployment",
},
&cli.BoolFlag{
Name: "continue-on-error",
Usage: "Proceed deployment even if config upload fails",
Aliases: []string{"c"},
},
},
Action: func(ctx *cli.Context) error {
if ctx.NArg() > 1 {
util.Log.Error("Too many arguments! Either specify a relative path to the working directory, or omit it for using the current working directory.")
cli.ShowAppHelpAndExit(ctx, 1)
}
var workingDir string
if ctx.Args().Present() {
workingDir = ctx.Args().First()
} else {
workingDir = "."
}
return deploy.Deploy(
workingDir,
fs,
ctx.Path("environments"),
ctx.String("specific-environment"),
ctx.String("project"),
ctx.Bool("dry-run"),
ctx.Bool("continue-on-error"),
)
},
}
return command
}
func getDownloadCommand(fs afero.Fs) cli.Command {
command := cli.Command{
Name: "download",
Usage: "download the given environment",
UsageText: "download [command options] [working directory]",
Before: func(c *cli.Context) error {
err := util.SetupLogging(c.Bool("verbose"))
if err != nil {
return err
}
util.Log.Info("Dynatrace Monitoring as Code v" + version.MonitoringAsCode)
return nil
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
},
&cli.PathFlag{
Name: "environments",
Usage: "Yaml file containing environment to deploy to",
Aliases: []string{"e"},
Required: true,
TakesFile: true,
},
&cli.StringFlag{
Name: "specific-environment",
Usage: "Specific environment (from list) to deploy to",
Aliases: []string{"s"},
},
&cli.StringFlag{
Name: "downloadSpecificAPI",
Usage: "Comma separated list of API's to download ",
Aliases: []string{"p"},
},
},
Action: func(ctx *cli.Context) error {
var workingDir string
if ctx.Args().Present() {
workingDir = ctx.Args().First()
} else {
workingDir = "."
}
return download.GetConfigsFilterByEnvironment(
workingDir,
fs,
ctx.Path("environments"),
ctx.String("specific-environment"),
ctx.String("downloadSpecificAPI"),
)
},
}
return command
}