-
Notifications
You must be signed in to change notification settings - Fork 66
/
command.go
98 lines (81 loc) · 3.32 KB
/
command.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
package main
import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/newrelic/newrelic-cli/internal/cli"
"github.com/newrelic/newrelic-cli/internal/client"
"github.com/newrelic/newrelic-cli/internal/config"
configAPI "github.com/newrelic/newrelic-cli/internal/config/api"
diagnose "github.com/newrelic/newrelic-cli/internal/diagnose"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/split"
"github.com/newrelic/newrelic-cli/internal/utils"
"github.com/newrelic/newrelic-client-go/v2/newrelic"
nrErrors "github.com/newrelic/newrelic-client-go/v2/pkg/errors"
)
var (
outputFormat string
outputPlain bool
)
// Command represents the base command when called without any subcommands
var Command = &cobra.Command{
PersistentPreRun: initializeCLI,
Use: appName,
Short: "The New Relic CLI",
Long: `The New Relic CLI enables users to perform tasks against the New Relic APIs`,
Version: cli.Version(),
DisableAutoGenTag: true, // Do not print generation date on documentation
}
func initializeCLI(cmd *cobra.Command, args []string) {
// Initialize logger
logLevel := configAPI.GetLogLevel()
config.InitLogger(log.StandardLogger(), logLevel)
// Initialize feature flag service
split.Init()
// Initialize client
if client.NRClient == nil {
client.NRClient = createClient()
}
}
func createClient() *newrelic.NewRelic {
c, err := client.NewClient(configAPI.GetActiveProfileName())
if err != nil {
// An error was encountered initializing the client. This may not be a
// problem since many commands don't require the use of an initialized client
log.Debugf("error initializing client: %s", err)
}
return c
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the RootCmd.
func Execute() error {
Command.Use = appName
Command.Version = cli.Version()
// Silence Cobra's internal handling of command usage help text.
// Note, the help text is still displayed if any command arg or
// flag validation fails.
Command.SilenceUsage = true
// Silence Cobra's internal handling of error messaging
// since we have a custom error handler in main.go
Command.SilenceErrors = true
err := Command.Execute()
if _, ok := err.(*nrErrors.PaymentRequiredError); ok {
diagnose.PrintPaymentRequiredErrorMessage()
log.Debug(err)
return nil
}
return err
}
func init() {
cobra.OnInitialize(initConfig)
Command.PersistentFlags().StringVar(&outputFormat, "format", output.DefaultFormat.String(), "output text format ["+output.FormatOptions()+"]")
Command.PersistentFlags().StringVar(&config.FlagProfileName, "profile", "", "the authentication profile to use")
Command.PersistentFlags().BoolVar(&outputPlain, "plain", false, "output compact text")
Command.PersistentFlags().BoolVar(&config.FlagDebug, "debug", false, "debug level logging")
Command.PersistentFlags().BoolVar(&config.FlagTrace, "trace", false, "trace level logging")
Command.PersistentFlags().IntVarP(&config.FlagAccountID, "accountId", "a", 0, "the account ID to use. Can be overridden by setting NEW_RELIC_ACCOUNT_ID")
}
func initConfig() {
utils.LogIfError(output.SetFormat(output.ParseFormat(outputFormat)))
utils.LogIfError(output.SetPrettyPrint(!outputPlain))
}