-
Notifications
You must be signed in to change notification settings - Fork 73
/
command.go
179 lines (155 loc) · 4.52 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
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
package credentials
import (
"github.com/jedib0t/go-pretty/v6/text"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
// Display keys when printing output
showKeys bool
profileName string
flagRegion string
apiKey string
insightsInsertKey string
accountID int
licenseKey string
)
// Command is the base command for managing profiles
var Command = &cobra.Command{
Use: "profile",
Short: "Manage the authentication profiles for this tool",
Aliases: []string{
"profiles", // DEPRECATED: accept but not consistent with the rest of the singular usage
},
}
var cmdAdd = &cobra.Command{
Use: "add",
Short: "Add a new profile",
Long: `Add a new profile
The add command creates a new profile for use with the New Relic CLI.
API key and region are required. An Insights insert key is optional, but required
for posting custom events with the ` + "`newrelic events`" + `command.
`,
Example: "newrelic profile add --name <profileName> --region <region> --apiKey <apiKey> --insightsInsertKey <insightsInsertKey> --accountId <accountId> --licenseKey <licenseKey>",
Run: func(cmd *cobra.Command, args []string) {
WithCredentials(func(creds *Credentials) {
p := Profile{
Region: flagRegion,
APIKey: apiKey,
InsightsInsertKey: insightsInsertKey,
AccountID: accountID,
LicenseKey: licenseKey,
}
err := creds.AddProfile(profileName, p)
if err != nil {
log.Fatal(err)
}
log.Infof("profile %s added", text.FgCyan.Sprint(profileName))
if len(creds.Profiles) == 1 {
err := creds.SetDefaultProfile(profileName)
if err != nil {
log.Fatal(err)
}
log.Infof("setting %s as default profile", text.FgCyan.Sprint(profileName))
}
})
},
}
var cmdDefault = &cobra.Command{
Use: "default",
Short: "Set the default profile name",
Long: `Set the default profile name
The default command sets the profile to use by default using the specified name.
`,
Example: "newrelic profile default --name <profileName>",
Run: func(cmd *cobra.Command, args []string) {
WithCredentials(func(creds *Credentials) {
err := creds.SetDefaultProfile(profileName)
if err != nil {
log.Fatal(err)
}
log.Info("success")
})
},
}
var cmdList = &cobra.Command{
Use: "list",
Short: "List the profiles available",
Long: `List the profiles available
The list command prints out the available profiles' credentials.
`,
Example: "newrelic profile list",
Run: func(cmd *cobra.Command, args []string) {
WithCredentials(func(creds *Credentials) {
if creds != nil {
creds.List()
} else {
log.Info("no profiles found")
}
})
},
Aliases: []string{
"ls",
},
}
var cmdDelete = &cobra.Command{
Use: "delete",
Short: "Delete a profile",
Long: `Delete a profile
The delete command removes the profile specified by name.
`,
Example: "newrelic profile delete --name <profileName>",
Run: func(cmd *cobra.Command, args []string) {
WithCredentials(func(creds *Credentials) {
err := creds.RemoveProfile(profileName)
if err != nil {
log.Fatal(err)
}
log.Info("success")
})
},
Aliases: []string{
"remove",
"rm",
},
}
func init() {
var err error
// Add
Command.AddCommand(cmdAdd)
cmdAdd.Flags().StringVarP(&profileName, "name", "n", "", "unique profile name to add")
cmdAdd.Flags().StringVarP(&flagRegion, "region", "r", "", "the US or EU region")
cmdAdd.Flags().StringVarP(&apiKey, "apiKey", "", "", "your personal API key")
cmdAdd.Flags().StringVarP(&insightsInsertKey, "insightsInsertKey", "", "", "your Insights insert key")
cmdAdd.Flags().StringVarP(&licenseKey, "licenseKey", "", "", "your license key")
cmdAdd.Flags().IntVarP(&accountID, "accountId", "", 0, "your account ID")
err = cmdAdd.MarkFlagRequired("name")
if err != nil {
log.Error(err)
}
err = cmdAdd.MarkFlagRequired("region")
if err != nil {
log.Error(err)
}
err = cmdAdd.MarkFlagRequired("apiKey")
if err != nil {
log.Error(err)
}
// Default
Command.AddCommand(cmdDefault)
cmdDefault.Flags().StringVarP(&profileName, "name", "n", "", "the profile name to set as default")
err = cmdDefault.MarkFlagRequired("name")
if err != nil {
log.Error(err)
}
// List
Command.AddCommand(cmdList)
cmdList.Flags().BoolVarP(&showKeys, "show-keys", "s", false, "list the profiles on your keychain")
// Remove
Command.AddCommand(cmdDelete)
cmdDelete.Flags().StringVarP(&profileName, "name", "n", "", "the profile name to delete")
err = cmdDelete.MarkFlagRequired("name")
if err != nil {
log.Error(err)
}
}