-
Notifications
You must be signed in to change notification settings - Fork 69
/
helpers.go
49 lines (41 loc) · 1.6 KB
/
helpers.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
package client
import (
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-cli/internal/config"
"github.com/newrelic/newrelic-cli/internal/credentials"
"github.com/newrelic/newrelic-client-go/newrelic"
)
// WithClient returns a New Relic client.
func WithClient(f func(c *newrelic.NewRelic)) {
WithClientFrom(config.DefaultConfigDirectory, f)
}
// WithClientFrom returns a New Relic client, initialized from configuration in the specified location.
func WithClientFrom(configDir string, f func(c *newrelic.NewRelic)) {
config.WithConfigFrom(configDir, func(cfg *config.Config) {
credentials.WithCredentialsFrom(configDir, func(creds *credentials.Credentials) {
nrClient, _, err := CreateNRClient(cfg, creds)
if err != nil {
log.Fatal(err)
}
f(nrClient)
})
})
}
// WithClientAndProfile returns a New Relic client and the profile used to initialize it,
// after environment oveerrides have been applied.
func WithClientAndProfile(f func(c *newrelic.NewRelic, p *credentials.Profile)) {
WithClientAndProfileFrom(config.DefaultConfigDirectory, f)
}
// WithClientAndProfileFrom returns a New Relic client and default profile used to initialize it,
// after environment oveerrides have been applied.
func WithClientAndProfileFrom(configDir string, f func(c *newrelic.NewRelic, p *credentials.Profile)) {
config.WithConfigFrom(configDir, func(cfg *config.Config) {
credentials.WithCredentialsFrom(configDir, func(creds *credentials.Credentials) {
nrClient, defaultProfile, err := CreateNRClient(cfg, creds)
if err != nil {
log.Fatal(err)
}
f(nrClient, defaultProfile)
})
})
}