-
Notifications
You must be signed in to change notification settings - Fork 69
/
command.go
145 lines (116 loc) · 3.71 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
package command
import (
"strings"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-cli/internal/config"
configAPI "github.com/newrelic/newrelic-cli/internal/config/api"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/utils"
)
var (
// Display keys when printing output
key string
value string
)
// Command is the base command for managing profiles
var Command = &cobra.Command{
Use: "config",
Short: "Manage the configuration of the New Relic CLI",
}
var cmdSet = &cobra.Command{
Use: "set",
Short: "Set a configuration value",
Long: `Set a configuration value
The set command sets a persistent configuration value for the New Relic CLI.
`,
Example: "newrelic config set --key <key> --value <value>",
Run: func(cmd *cobra.Command, args []string) {
if !isValidFieldKey() {
log.Fatalf("%s is not a valid config field. valid values are %s", key, configAPI.GetValidConfigFieldKeys())
}
if err := configAPI.SetConfigValue(config.FieldKey(key), value); err != nil {
log.Fatal(err)
}
log.Info("success")
},
}
func isValidFieldKey() (valid bool) {
configAPI.ForEachConfigFieldDefinition(func(fd config.FieldDefinition) {
if strings.EqualFold(string(fd.Key), key) {
valid = true
}
})
return valid
}
var cmdGet = &cobra.Command{
Use: "get",
Short: "Get a configuration value",
Long: `Get a configuration value
The get command gets a persistent configuration value for the New Relic CLI.
`,
Example: "newrelic config get --key <key>",
Run: func(cmd *cobra.Command, args []string) {
if !isValidFieldKey() {
log.Fatalf("%s is not a valid config field. valid values are %s", key, configAPI.GetValidConfigFieldKeys())
}
output.Text(configAPI.GetConfigString(config.FieldKey(key)))
},
}
var cmdList = &cobra.Command{
Use: "list",
Short: "List the current configuration values",
Long: `List the current configuration values
The list command lists all persistent configuration values for the New Relic CLI.
`,
Example: "newrelic config list",
Run: func(cmd *cobra.Command, args []string) {
m := map[string]interface{}{}
configAPI.ForEachConfigFieldDefinition(func(fd config.FieldDefinition) {
m[string(fd.Key)] = configAPI.GetConfigString(fd.Key)
})
output.Text(m)
},
Aliases: []string{
"ls",
},
}
var cmdReset = &cobra.Command{
Use: "reset",
Short: "Reset a configuration value to its default",
Long: `Reset a configuration value
The reset command resets a configuration value to its default.
`,
Example: "newrelic config reset --key <key>",
Run: func(cmd *cobra.Command, args []string) {
if !isValidFieldKey() {
log.Fatalf("%s is not a valid config field. valid values are %s", key, configAPI.GetValidConfigFieldKeys())
}
fd := configAPI.GetConfigFieldDefinition(config.FieldKey(key))
if fd.Default == nil {
log.Fatalf("key %s cannot be reset to a default value since no default exists", fd.Key)
}
if err := configAPI.SetConfigValue(config.FieldKey(key), fd.Default); err != nil {
log.Fatal(err)
}
log.Info("success")
},
Aliases: []string{
"rm",
"delete",
},
}
func init() {
Command.AddCommand(cmdList)
Command.AddCommand(cmdSet)
cmdSet.Flags().StringVarP(&key, "key", "k", "", "the key to set")
cmdSet.Flags().StringVarP(&value, "value", "v", "", "the value to set")
utils.LogIfError(cmdSet.MarkFlagRequired("key"))
utils.LogIfError(cmdSet.MarkFlagRequired("value"))
Command.AddCommand(cmdGet)
cmdGet.Flags().StringVarP(&key, "key", "k", "", "the key to get")
utils.LogIfError(cmdGet.MarkFlagRequired("key"))
Command.AddCommand(cmdReset)
cmdReset.Flags().StringVarP(&key, "key", "k", "", "the key to delete")
utils.LogIfError(cmdReset.MarkFlagRequired("key"))
}