From d3ba4b5726942184dfd51944fae9ba205aa0f0b2 Mon Sep 17 00:00:00 2001 From: Chris Trombley Date: Thu, 8 Jul 2021 09:35:02 -0700 Subject: [PATCH] refactor: add override capable getters to config provider --- cmd/newrelic/command.go | 26 ++------ internal/apm/command_application.go | 2 +- internal/client/client.go | 2 +- internal/configuration/config.go | 65 +++++++++++++++++-- internal/configuration/config_provider.go | 42 +++++++++++- internal/diagnose/config_validator.go | 6 +- internal/edge/command_trace_observer.go | 6 +- internal/events/command_post.go | 2 +- internal/install/command.go | 2 +- .../execution/nerdstorage_status_reporter.go | 2 +- .../execution/platform_link_generator.go | 2 +- .../install/packs/service_packs_installer.go | 2 +- internal/nerdstorage/command_collection.go | 4 +- internal/nerdstorage/command_document.go | 6 +- internal/nrql/command_query.go | 2 +- internal/profile/command.go | 28 ++++---- internal/reporting/command_junit.go | 2 +- .../validation/polling_nrql_validator.go | 2 +- internal/workload/command_workload.go | 6 +- 19 files changed, 143 insertions(+), 66 deletions(-) diff --git a/cmd/newrelic/command.go b/cmd/newrelic/command.go index 2df899342..585d9e2cb 100644 --- a/cmd/newrelic/command.go +++ b/cmd/newrelic/command.go @@ -2,7 +2,6 @@ package main import ( "os" - "strconv" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -17,9 +16,6 @@ import ( var ( outputFormat string outputPlain bool - debug bool - trace bool - accountID int ) // Command represents the base command when called without any subcommands @@ -33,19 +29,7 @@ var Command = &cobra.Command{ } func initializeCLI(cmd *cobra.Command, args []string) { - if debug { - os.Setenv("NEW_RELIC_CLI_LOG_LEVEL", "debug") - } - - if trace { - os.Setenv("NEW_RELIC_CLI_LOG_LEVEL", "trace") - } - - if accountID != 0 { - os.Setenv("NEW_RELIC_ACCOUNT_ID", strconv.Itoa(accountID)) - } - - logLevel := configuration.GetConfigString(configuration.LogLevel) + logLevel := configuration.GetLogLevelWithFlagOverride() configuration.InitLogger(logLevel) if client.NRClient == nil { @@ -82,11 +66,11 @@ func init() { cobra.OnInitialize(initConfig) Command.PersistentFlags().StringVar(&outputFormat, "format", output.DefaultFormat.String(), "output text format ["+output.FormatOptions()+"]") - Command.PersistentFlags().StringVar(&configuration.SelectedProfileName, "profileName", configuration.DefaultProfileName, "the authentication profile to use") + Command.PersistentFlags().StringVar(&configuration.FlagProfileName, "profileName", configuration.DefaultProfileName, "the authentication profile to use") Command.PersistentFlags().BoolVar(&outputPlain, "plain", false, "output compact text") - Command.PersistentFlags().BoolVar(&debug, "debug", false, "debug level logging") - Command.PersistentFlags().BoolVar(&trace, "trace", false, "trace level logging") - Command.PersistentFlags().IntVarP(&accountID, "accountId", "a", 0, "trace level logging") + Command.PersistentFlags().BoolVar(&configuration.FlagDebug, "debug", false, "debug level logging") + Command.PersistentFlags().BoolVar(&configuration.FlagTrace, "trace", false, "trace level logging") + Command.PersistentFlags().IntVarP(&configuration.FlagAccountID, "accountId", "a", 0, "trace level logging") } func initConfig() { diff --git a/internal/apm/command_application.go b/internal/apm/command_application.go index cfabc99a4..ee09dbf7c 100644 --- a/internal/apm/command_application.go +++ b/internal/apm/command_application.go @@ -37,7 +37,7 @@ The search command performs a query for an APM application name and/or account I Example: "newrelic apm application search --name ", PreRun: client.RequireClient, Run: func(cmd *cobra.Command, args []string) { - accountID := configuration.GetActiveProfileInt(configuration.AccountID) + accountID := configuration.GetActiveProfileAccountIDWithFlagOverride() if appGUID == "" && appName == "" && accountID == 0 { utils.LogIfError(cmd.Help()) diff --git a/internal/client/client.go b/internal/client/client.go index 0024b8a12..25dbd63fb 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -28,7 +28,7 @@ func NewClient(profileName string) (*newrelic.NewRelic, error) { } region := configuration.GetProfileString(profileName, configuration.Region) - logLevel := configuration.GetConfigString(configuration.LogLevel) + logLevel := configuration.GetLogLevelWithFlagOverride() userAgent := fmt.Sprintf("newrelic-cli/%s (https://github.com/newrelic/newrelic-cli)", version) cfgOpts := []newrelic.ConfigOption{ diff --git a/internal/configuration/config.go b/internal/configuration/config.go index 6638a8227..441bdd730 100644 --- a/internal/configuration/config.go +++ b/internal/configuration/config.go @@ -35,7 +35,11 @@ var ( configProvider *ConfigProvider credentialsProvider *ConfigProvider BasePath string = configBasePath() - SelectedProfileName string + + FlagProfileName string + FlagDebug bool + FlagTrace bool + FlagAccountID int ) func init() { @@ -131,8 +135,8 @@ func initializeConfigProvider() { } func GetActiveProfileName() string { - if SelectedProfileName != "" { - return SelectedProfileName + if FlagProfileName != "" { + return FlagProfileName } profileName, err := GetDefaultProfileName() @@ -177,6 +181,41 @@ func GetProfileString(profileName string, key ConfigKey) string { return v } +func GetLogLevelWithFlagOverride() string { + var override string + if FlagDebug { + override = "debug" + } + + if FlagTrace { + override = "trace" + } + + return GetConfigStringWithOverride(LogLevel, override) +} + +func RequireActiveProfileAccountIDWithFlagOverride() int { + v := GetActiveProfileAccountIDWithFlagOverride() + if v == 0 { + log.Fatalf("%s is required", AccountID) + } + + return v +} + +func GetActiveProfileAccountIDWithFlagOverride() int { + return GetActiveProfileIntWithOverride(AccountID, FlagAccountID) +} + +func RequireActiveProfileIntWithOverride(key ConfigKey, override int) int { + v := GetProfileIntWithOverride(GetActiveProfileName(), key, override) + if v == 0 { + log.Fatalf("%s is required", key) + } + + return v +} + func RequireActiveProfileInt(key ConfigKey) int { v := GetProfileInt(GetActiveProfileName(), key) if v == 0 { @@ -190,6 +229,10 @@ func GetActiveProfileInt(key ConfigKey) int { return GetProfileInt(GetActiveProfileName(), key) } +func GetActiveProfileIntWithOverride(key ConfigKey, override int) int { + return GetProfileIntWithOverride(GetActiveProfileName(), key, override) +} + func GetProfileInt(profileName string, key ConfigKey) int { v, err := credentialsProvider.GetIntWithScope(GetActiveProfileName(), key) if err != nil { @@ -199,8 +242,22 @@ func GetProfileInt(profileName string, key ConfigKey) int { return int(v) } +func GetProfileIntWithOverride(profileName string, key ConfigKey, override int) int { + o := int64(override) + v, err := credentialsProvider.GetIntWithScopeAndOverride(GetActiveProfileName(), key, &o) + if err != nil { + log.Fatalf("could not load value %s from config: %s", key, err) + } + + return int(v) +} + func GetConfigString(key ConfigKey) string { - v, err := configProvider.GetString(key) + return GetConfigStringWithOverride(key, "") +} + +func GetConfigStringWithOverride(key ConfigKey, override string) string { + v, err := configProvider.GetStringWithOverride(key, &override) if err != nil { log.Fatalf("could not load value %s from config: %s", key, err) } diff --git a/internal/configuration/config_provider.go b/internal/configuration/config_provider.go index 20b0c003f..6bc433d8a 100644 --- a/internal/configuration/config_provider.go +++ b/internal/configuration/config_provider.go @@ -148,7 +148,11 @@ func (p *ConfigProvider) GetInt(key ConfigKey) (int64, error) { } func (p *ConfigProvider) GetString(key ConfigKey) (string, error) { - return p.GetStringWithScope("", key) + return p.GetStringWithScopeAndOverride("", key, nil) +} + +func (p *ConfigProvider) GetStringWithOverride(key ConfigKey, override *string) (string, error) { + return p.GetStringWithScopeAndOverride("", key, override) } func (p *ConfigProvider) GetTernary(key ConfigKey) (Ternary, error) { @@ -172,7 +176,19 @@ func (p *ConfigProvider) GetTernaryWithScope(scope string, key ConfigKey) (Terna } func (p *ConfigProvider) GetIntWithScope(scope string, key ConfigKey) (int64, error) { - v, err := p.GetWithScope(scope, key) + return p.GetIntWithScopeAndOverride(scope, key, nil) +} + +func (p *ConfigProvider) GetIntWithScopeAndOverride(scope string, key ConfigKey, override *int64) (int64, error) { + var v interface{} + var err error + + if override == nil { + v, err = p.GetWithScopeAndOverride(scope, key, nil) + } else { + v, err = p.GetWithScopeAndOverride(scope, key, *override) + } + if err != nil { return 0, err } @@ -200,7 +216,19 @@ func (p *ConfigProvider) GetIntWithScope(scope string, key ConfigKey) (int64, er } func (p *ConfigProvider) GetStringWithScope(scope string, key ConfigKey) (string, error) { - v, err := p.GetWithScope(scope, key) + return p.GetStringWithScopeAndOverride(scope, key, nil) +} + +func (p *ConfigProvider) GetStringWithScopeAndOverride(scope string, key ConfigKey, override *string) (string, error) { + var v interface{} + var err error + + if override == nil || *override == "" { + v, err = p.GetWithScope(scope, key) + } else { + v, err = p.GetWithScopeAndOverride(scope, key, *override) + } + if err != nil { return "", err } @@ -228,6 +256,10 @@ func (p *ConfigProvider) Get(key ConfigKey) (interface{}, error) { } func (p *ConfigProvider) GetWithScope(scope string, key ConfigKey) (interface{}, error) { + return p.GetWithScopeAndOverride(scope, key, nil) +} + +func (p *ConfigProvider) GetWithScopeAndOverride(scope string, key ConfigKey, overridePtr interface{}) (interface{}, error) { d := p.getFieldDefinition(key) if d != nil { @@ -241,6 +273,10 @@ func (p *ConfigProvider) GetWithScope(scope string, key ConfigKey) (interface{}, } } + if overridePtr != nil { + return overridePtr, nil + } + res, err := p.getFromConfig(p.getPath(scope, key)) if err != nil { if d != nil && d.Default != nil { diff --git a/internal/diagnose/config_validator.go b/internal/diagnose/config_validator.go index 8d95f5130..0858383d6 100644 --- a/internal/diagnose/config_validator.go +++ b/internal/diagnose/config_validator.go @@ -50,7 +50,7 @@ func NewConfigValidator(client *newrelic.NewRelic) *ConfigValidator { } func (c *ConfigValidator) Validate(ctx context.Context) error { - accountID := configuration.GetActiveProfileInt(configuration.AccountID) + accountID := configuration.GetActiveProfileAccountIDWithFlagOverride() if err := c.validateKeys(ctx); err != nil { return err @@ -119,7 +119,7 @@ func (c *ConfigValidator) validateKeys(ctx context.Context) error { } func (c *ConfigValidator) validateInsightsInsertKey(ctx context.Context) error { - accountID := configuration.GetActiveProfileInt(configuration.AccountID) + accountID := configuration.GetActiveProfileAccountIDWithFlagOverride() insightsInsertKey := configuration.GetActiveProfileString(configuration.InsightsInsertKey) insightsInsertKeys, err := c.client.APIAccess.ListInsightsInsertKeysWithContext(ctx, accountID) if err != nil { @@ -136,7 +136,7 @@ func (c *ConfigValidator) validateInsightsInsertKey(ctx context.Context) error { } func (c *ConfigValidator) validateLicenseKey(ctx context.Context) error { - accountID := configuration.GetActiveProfileInt(configuration.AccountID) + accountID := configuration.GetActiveProfileAccountIDWithFlagOverride() licenseKey := configuration.GetActiveProfileString(configuration.LicenseKey) params := apiaccess.APIAccessKeySearchQuery{ Scope: apiaccess.APIAccessKeySearchScope{ diff --git a/internal/edge/command_trace_observer.go b/internal/edge/command_trace_observer.go index 6ca948419..6f7056083 100644 --- a/internal/edge/command_trace_observer.go +++ b/internal/edge/command_trace_observer.go @@ -43,7 +43,7 @@ The list command retrieves the trace observers for the given account ID. Example: `newrelic edge trace-observer list --accountId 12345678`, PreRun: client.RequireClient, Run: func(cmd *cobra.Command, args []string) { - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() traceObservers, err := client.NRClient.Edge.ListTraceObserversWithContext(utils.SignalCtx, accountID) utils.LogIfFatal(err) @@ -72,7 +72,7 @@ Valid provider regions are AWS_US_EAST_1 and AWS_US_EAST_2. Example: `newrelic edge trace-observer create --name 'My Observer' --accountId 12345678 --providerRegion AWS_US_EAST_1`, PreRun: client.RequireClient, Run: func(cmd *cobra.Command, args []string) { - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() if ok := isValidProviderRegion(providerRegion); !ok { log.Fatalf("%s is not a valid provider region, valid values are %s", providerRegion, validProviderRegions) } @@ -95,7 +95,7 @@ The delete command accepts a trace observer's ID. Example: `newrelic edge trace-observer delete --accountId 12345678 --id 1234`, PreRun: client.RequireClient, Run: func(cmd *cobra.Command, args []string) { - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() _, err := client.NRClient.Edge.DeleteTraceObserver(accountID, id) utils.LogIfFatal(err) diff --git a/internal/events/command_post.go b/internal/events/command_post.go index dfd7f2393..e61a3b2da 100644 --- a/internal/events/command_post.go +++ b/internal/events/command_post.go @@ -29,7 +29,7 @@ represents the custom event's type. Example: `newrelic events post --accountId 12345 --event '{ "eventType": "Payment", "amount": 123.45 }'`, PreRun: client.RequireClient, Run: func(cmd *cobra.Command, args []string) { - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() if configuration.GetActiveProfileString(configuration.InsightsInsertKey) == "" { log.Fatal("an Insights insert key is required, set one in your default profile or use the NEW_RELIC_INSIGHTS_INSERT_KEY environment variable") } diff --git a/internal/install/command.go b/internal/install/command.go index e166d8bff..7e40f4c3b 100644 --- a/internal/install/command.go +++ b/internal/install/command.go @@ -59,7 +59,7 @@ var Command = &cobra.Command{ } func assertProfileIsValid() error { - if configuration.GetActiveProfileInt(configuration.AccountID) == 0 { + if configuration.GetActiveProfileAccountIDWithFlagOverride() == 0 { return fmt.Errorf("accountID is required") } diff --git a/internal/install/execution/nerdstorage_status_reporter.go b/internal/install/execution/nerdstorage_status_reporter.go index a2d08ba36..2d60ad56a 100644 --- a/internal/install/execution/nerdstorage_status_reporter.go +++ b/internal/install/execution/nerdstorage_status_reporter.go @@ -120,7 +120,7 @@ func (r NerdstorageStatusReporter) writeStatus(status *InstallStatus) error { log.Debug("no entity GUIDs available, skipping entity-scoped status updates") } - accountID := configuration.GetActiveProfileInt(configuration.AccountID) + accountID := configuration.GetActiveProfileAccountIDWithFlagOverride() _, err = r.client.WriteDocumentWithAccountScope(accountID, i) if err != nil { log.Debug("failed to write to account scoped nerd storage") diff --git a/internal/install/execution/platform_link_generator.go b/internal/install/execution/platform_link_generator.go index d5e89dcc0..10c1e6063 100644 --- a/internal/install/execution/platform_link_generator.go +++ b/internal/install/execution/platform_link_generator.go @@ -54,7 +54,7 @@ func generateExplorerLink(filter string) string { return fmt.Sprintf("https://%s/launcher/nr1-core.explorer?platform[filters]=%s&platform[accountId]=%d", nrPlatformHostname(), utils.Base64Encode(filter), - configuration.GetActiveProfileInt(configuration.AccountID), + configuration.GetActiveProfileAccountIDWithFlagOverride(), ) } diff --git a/internal/install/packs/service_packs_installer.go b/internal/install/packs/service_packs_installer.go index c4eb56149..734b5ffb1 100644 --- a/internal/install/packs/service_packs_installer.go +++ b/internal/install/packs/service_packs_installer.go @@ -69,7 +69,7 @@ func (p *ServicePacksInstaller) Install(ctx context.Context, packs []types.OpenI } func (p *ServicePacksInstaller) createObservabilityPackDashboard(ctx context.Context, d types.OpenInstallationObservabilityPackDashboard) (*dashboards.DashboardCreateResult, error) { - accountID := configuration.GetActiveProfileInt(configuration.AccountID) + accountID := configuration.GetActiveProfileAccountIDWithFlagOverride() body, err := getJSONfromURL(d.URL) if err != nil { diff --git a/internal/nerdstorage/command_collection.go b/internal/nerdstorage/command_collection.go index b95db7c32..da1ee42ca 100644 --- a/internal/nerdstorage/command_collection.go +++ b/internal/nerdstorage/command_collection.go @@ -51,7 +51,7 @@ GUID. A valid Nerdpack package ID is required. switch strings.ToLower(scope) { case "account": - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() resp, err = client.NRClient.NerdStorage.GetCollectionWithAccountScopeWithContext(utils.SignalCtx, accountID, input) case "entity": resp, err = client.NRClient.NerdStorage.GetCollectionWithEntityScopeWithContext(utils.SignalCtx, entityGUID, input) @@ -99,7 +99,7 @@ GUID. A valid Nerdpack package ID is required. switch strings.ToLower(scope) { case "account": - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() _, err = client.NRClient.NerdStorage.DeleteCollectionWithAccountScopeWithContext(utils.SignalCtx, accountID, input) case "entity": _, err = client.NRClient.NerdStorage.DeleteCollectionWithEntityScopeWithContext(utils.SignalCtx, entityGUID, input) diff --git a/internal/nerdstorage/command_document.go b/internal/nerdstorage/command_document.go index 91dfe74af..bbbac03d4 100644 --- a/internal/nerdstorage/command_document.go +++ b/internal/nerdstorage/command_document.go @@ -53,7 +53,7 @@ GUID. A valid Nerdpack package ID is required. switch strings.ToLower(scope) { case "account": - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() document, err = client.NRClient.NerdStorage.GetDocumentWithAccountScopeWithContext(utils.SignalCtx, accountID, input) case "entity": document, err = client.NRClient.NerdStorage.GetDocumentWithEntityScopeWithContext(utils.SignalCtx, entityGUID, input) @@ -107,7 +107,7 @@ GUID. A valid Nerdpack package ID is required. switch strings.ToLower(scope) { case "account": - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() _, err = client.NRClient.NerdStorage.WriteDocumentWithAccountScopeWithContext(utils.SignalCtx, accountID, input) case "entity": _, err = client.NRClient.NerdStorage.WriteDocumentWithEntityScopeWithContext(utils.SignalCtx, entityGUID, input) @@ -155,7 +155,7 @@ GUID. A valid Nerdpack package ID is required. switch strings.ToLower(scope) { case "account": - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() _, err = client.NRClient.NerdStorage.DeleteDocumentWithAccountScopeWithContext(utils.SignalCtx, accountID, input) case "entity": _, err = client.NRClient.NerdStorage.DeleteDocumentWithEntityScopeWithContext(utils.SignalCtx, entityGUID, input) diff --git a/internal/nrql/command_query.go b/internal/nrql/command_query.go index 4189b43c6..2b87335c5 100644 --- a/internal/nrql/command_query.go +++ b/internal/nrql/command_query.go @@ -28,7 +28,7 @@ issue the query against. Example: `newrelic nrql query --accountId 12345678 --query 'SELECT count(*) FROM Transaction TIMESERIES'`, PreRun: client.RequireClient, Run: func(cmd *cobra.Command, args []string) { - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() result, err := client.NRClient.Nrdb.QueryWithContext(utils.SignalCtx, accountID, nrdb.NRQL(query)) if err != nil { log.Fatal(err) diff --git a/internal/profile/command.go b/internal/profile/command.go index c95363b2b..f789b118d 100644 --- a/internal/profile/command.go +++ b/internal/profile/command.go @@ -60,11 +60,11 @@ for posting custom events with the ` + "`newrelic events`" + `command. Example: "newrelic profile add --profileName --region --apiKey --insightsInsertKey --accountId --licenseKey ", PreRun: requireProfileName, Run: func(cmd *cobra.Command, args []string) { - addStringValueToProfile(configuration.SelectedProfileName, apiKey, configuration.APIKey, "User API Key", nil, nil) - addStringValueToProfile(configuration.SelectedProfileName, flagRegion, configuration.Region, "Region", nil, []string{"US", "EU"}) - addIntValueToProfile(configuration.SelectedProfileName, accountID, configuration.AccountID, "Account ID", fetchAccountIDs) - addStringValueToProfile(configuration.SelectedProfileName, insightsInsertKey, configuration.InsightsInsertKey, "Insights Insert Key", fetchInsightsInsertKey, nil) - addStringValueToProfile(configuration.SelectedProfileName, licenseKey, configuration.LicenseKey, "License Key", fetchLicenseKey, nil) + addStringValueToProfile(configuration.FlagProfileName, apiKey, configuration.APIKey, "User API Key", nil, nil) + addStringValueToProfile(configuration.FlagProfileName, flagRegion, configuration.Region, "Region", nil, []string{"US", "EU"}) + addIntValueToProfile(configuration.FlagProfileName, accountID, configuration.AccountID, "Account ID", fetchAccountIDs) + addStringValueToProfile(configuration.FlagProfileName, insightsInsertKey, configuration.InsightsInsertKey, "Insights Insert Key", fetchInsightsInsertKey, nil) + addStringValueToProfile(configuration.FlagProfileName, licenseKey, configuration.LicenseKey, "License Key", fetchLicenseKey, nil) profile, err := configuration.GetDefaultProfileName() if err != nil { @@ -72,7 +72,7 @@ for posting custom events with the ` + "`newrelic events`" + `command. } if profile == "" { - if err := configuration.SetDefaultProfile(configuration.SelectedProfileName); err != nil { + if err := configuration.SetDefaultProfile(configuration.FlagProfileName); err != nil { log.Fatal(err) } } @@ -172,8 +172,8 @@ func addIntValueToProfile(profileName string, val int, key configuration.ConfigK } func fetchLicenseKey() (string, error) { - accountID = configuration.GetProfileInt(configuration.SelectedProfileName, configuration.AccountID) - client, err := client.NewClient(configuration.SelectedProfileName) + accountID = configuration.GetProfileInt(configuration.FlagProfileName, configuration.AccountID) + client, err := client.NewClient(configuration.FlagProfileName) if err != nil { return "", err } @@ -222,8 +222,8 @@ func execLicenseKeyRequest(ctx context.Context, client *newrelic.NewRelic, accou } func fetchInsightsInsertKey() (string, error) { - accountID = configuration.GetProfileInt(configuration.SelectedProfileName, configuration.AccountID) - client, err := client.NewClient(configuration.SelectedProfileName) + accountID = configuration.GetProfileInt(configuration.FlagProfileName, configuration.AccountID) + client, err := client.NewClient(configuration.FlagProfileName) if err != nil { return "", err } @@ -250,7 +250,7 @@ func fetchInsightsInsertKey() (string, error) { // fetchAccountID will try and retrieve the available account IDs for the given user. func fetchAccountIDs() (ids []int, err error) { - client, err := client.NewClient(configuration.SelectedProfileName) + client, err := client.NewClient(configuration.FlagProfileName) if err != nil { return nil, err } @@ -280,7 +280,7 @@ The default command sets the profile to use by default using the specified name. `, Example: "newrelic profile default --name ", Run: func(cmd *cobra.Command, args []string) { - err := configuration.SetDefaultProfile(configuration.SelectedProfileName) + err := configuration.SetDefaultProfile(configuration.FlagProfileName) if err != nil { log.Fatal(err) } @@ -338,7 +338,7 @@ The delete command removes the profile specified by name. `, Example: "newrelic profile delete --name ", Run: func(cmd *cobra.Command, args []string) { - err := configuration.RemoveProfile(configuration.SelectedProfileName) + err := configuration.RemoveProfile(configuration.FlagProfileName) if err != nil { log.Fatal(err) } @@ -352,7 +352,7 @@ The delete command removes the profile specified by name. } func requireProfileName(cmd *cobra.Command, args []string) { - if configuration.SelectedProfileName == "" { + if configuration.FlagProfileName == "" { log.Fatal("the --profileName argument is required") } } diff --git a/internal/reporting/command_junit.go b/internal/reporting/command_junit.go index 20c2f07c7..1d8226ba7 100644 --- a/internal/reporting/command_junit.go +++ b/internal/reporting/command_junit.go @@ -31,7 +31,7 @@ var cmdJUnit = &cobra.Command{ Example: `newrelic reporting junit --accountId 12345678 --path unit.xml`, PreRun: client.RequireClient, Run: func(cmd *cobra.Command, args []string) { - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() if configuration.GetActiveProfileString(configuration.InsightsInsertKey) == "" { log.Fatal("an Insights insert key is required, set one in your default profile or use the NEW_RELIC_INSIGHTS_INSERT_KEY environment variable") diff --git a/internal/utils/validation/polling_nrql_validator.go b/internal/utils/validation/polling_nrql_validator.go index af06824f5..9eaaa5354 100644 --- a/internal/utils/validation/polling_nrql_validator.go +++ b/internal/utils/validation/polling_nrql_validator.go @@ -115,7 +115,7 @@ func (m *PollingNRQLValidator) tryValidate(ctx context.Context, query string) (b } func (m *PollingNRQLValidator) executeQuery(ctx context.Context, query string) ([]nrdb.NRDBResult, error) { - accountID := configuration.GetActiveProfileInt(configuration.AccountID) + accountID := configuration.GetActiveProfileAccountIDWithFlagOverride() nrql := nrdb.NRQL(query) diff --git a/internal/workload/command_workload.go b/internal/workload/command_workload.go index 8470eb04a..7aad79024 100644 --- a/internal/workload/command_workload.go +++ b/internal/workload/command_workload.go @@ -49,7 +49,7 @@ The list command retrieves the workloads for the given account ID. Example: `newrelic workload list --accountId 12345678`, PreRun: client.RequireClient, Run: func(cmd *cobra.Command, args []string) { - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() builder := entities.EntitySearchQueryBuilder{ Type: entities.EntitySearchQueryBuilderTypeTypes.WORKLOAD, Tags: []entities.EntitySearchQueryBuilderTag{ @@ -81,7 +81,7 @@ you also have access to. Example: `newrelic workload create --name 'Example workload' --accountId 12345678 --entitySearchQuery "name like 'Example application'"`, PreRun: client.RequireClient, Run: func(cmd *cobra.Command, args []string) { - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() createInput := workloads.WorkloadCreateInput{ Name: name, @@ -171,7 +171,7 @@ compose the new name. Example: `newrelic workload duplicate --guid 'MjUyMDUyOHxBOE28QVBQTElDQVRDT058MjE1MDM3Nzk1' --accountId 12345678 --name 'New Workload'`, PreRun: client.RequireClient, Run: func(cmd *cobra.Command, args []string) { - accountID := configuration.RequireActiveProfileInt(configuration.AccountID) + accountID := configuration.RequireActiveProfileAccountIDWithFlagOverride() duplicateInput := workloads.WorkloadDuplicateInput{ Name: name,