-
Notifications
You must be signed in to change notification settings - Fork 66
/
config.go
143 lines (124 loc) · 3.34 KB
/
config.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
package config
import (
"fmt"
"path/filepath"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-client-go/v2/pkg/region"
)
const (
APIKey FieldKey = "apiKey"
Region FieldKey = "region"
AccountID FieldKey = "accountID"
LicenseKey FieldKey = "licenseKey"
LogLevel FieldKey = "loglevel"
PluginDir FieldKey = "plugindir"
PreReleaseFeatures FieldKey = "prereleasefeatures"
SendUsageData FieldKey = "sendUsageData"
DefaultProfileName = "default"
DefaultProfileFileName = "default-profile.json"
ConfigFileName = "config.json"
CredentialsFileName = "credentials.json"
DefaultPluginDir = "plugins"
DefaultPostRetryDelaySec = 5
DefaultPostMaxRetries = 20
DefaultMaxTimeoutSeconds = 300 // 5 minutes
)
var (
ConfigStore *JSONStore
CredentialsProvider *JSONStore
BasePath = configBasePath()
FlagProfileName string
FlagDebug bool
FlagTrace bool
FlagAccountID int
)
func init() {
Init(configBasePath())
}
func Init(basePath string) {
BasePath = basePath
InitializeConfigStore()
InitializeCredentialsStore()
}
func InitializeCredentialsStore() {
p, err := NewJSONStore(
PersistToFile(filepath.Join(BasePath, CredentialsFileName)),
EnforceStrictFields(),
ConfigureFields(
FieldDefinition{
Key: APIKey,
EnvVar: "NEW_RELIC_API_KEY",
Sensitive: true,
},
FieldDefinition{
Key: Region,
EnvVar: "NEW_RELIC_REGION",
SetValidationFunc: StringInStrings(false,
region.Staging.String(),
region.US.String(),
region.EU.String(),
),
Default: region.US.String(),
SetValueFunc: ToLower(),
},
FieldDefinition{
Key: AccountID,
EnvVar: "NEW_RELIC_ACCOUNT_ID",
SetValidationFunc: IntGreaterThan(0),
},
FieldDefinition{
Key: LicenseKey,
EnvVar: "NEW_RELIC_LICENSE_KEY",
Sensitive: true,
},
),
)
if err != nil {
log.Fatalf("could not create credentials provider: %s", err)
}
CredentialsProvider = p
}
func InitializeConfigStore() {
p, err := NewJSONStore(
PersistToFile(filepath.Join(BasePath, ConfigFileName)),
UseGlobalScope("*"),
EnforceStrictFields(),
ConfigureFields(
FieldDefinition{
Key: LogLevel,
EnvVar: "NEW_RELIC_CLI_LOG_LEVEL",
Default: DefaultLogLevel,
SetValidationFunc: StringInStrings(false, "Info", "Debug", "Trace", "Warn", "Error"),
},
FieldDefinition{
Key: PluginDir,
EnvVar: "NEW_RELIC_CLI_PLUGIN_DIR",
Default: filepath.Join(BasePath, DefaultPluginDir),
},
FieldDefinition{
Key: PreReleaseFeatures,
EnvVar: "NEW_RELIC_CLI_PRERELEASEFEATURES",
SetValidationFunc: IsTernary(),
Default: TernaryValues.Unknown,
},
FieldDefinition{
Key: SendUsageData,
EnvVar: "NEW_RELIC_CLI_SENDUSAGEDATA",
SetValidationFunc: IsTernary(),
Default: TernaryValues.Unknown,
},
),
)
if err != nil {
log.Fatalf("could not create configuration provider: %s", err)
}
ConfigStore = p
}
func configBasePath() string {
home, err := homedir.Dir()
if err != nil {
log.Fatalf("cannot locate user's home directory: %s", err)
}
return fmt.Sprintf("%s/.newrelic", home)
}