From 41825ec38525ce950bad754180c10dd3b5e3fae3 Mon Sep 17 00:00:00 2001 From: Danny Olson Date: Wed, 15 May 2024 09:50:01 -0700 Subject: [PATCH 1/4] Support project in config file --- cmd/cliflags/flags.go | 2 ++ cmd/config/config.go | 1 + cmd/config/testdata/help.golden | 1 + internal/config/config.go | 6 ++++++ internal/config/config_test.go | 13 +++++++++++++ 5 files changed, 23 insertions(+) diff --git a/cmd/cliflags/flags.go b/cmd/cliflags/flags.go index f5a325c3..6269097a 100644 --- a/cmd/cliflags/flags.go +++ b/cmd/cliflags/flags.go @@ -18,6 +18,7 @@ const ( AnalyticsOptOutDescription = "Opt out of analytics tracking" BaseURIFlagDescription = "LaunchDarkly base URI" OutputFlagDescription = "Command response output format in either JSON or plain text" + ProjectFlagDescription = "Default project key" ) func AllFlagsHelp() map[string]string { @@ -26,5 +27,6 @@ func AllFlagsHelp() map[string]string { AnalyticsOptOut: AnalyticsOptOutDescription, BaseURIFlag: BaseURIFlagDescription, OutputFlag: OutputFlagDescription, + ProjectFlag: ProjectFlagDescription, } } diff --git a/cmd/config/config.go b/cmd/config/config.go index 1b34e334..2b44c530 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -69,6 +69,7 @@ func NewConfigCmd(analyticsTrackerFn analytics.TrackerFn) *ConfigCmd { cliflags.AnalyticsOptOut, cliflags.BaseURIFlag, cliflags.OutputFlag, + cliflags.ProjectFlag, } { sb.WriteString(fmt.Sprintf("- `%s`: %s\n", s, cliflags.AllFlagsHelp()[s])) } diff --git a/cmd/config/testdata/help.golden b/cmd/config/testdata/help.golden index 3b1300eb..6238b5d3 100644 --- a/cmd/config/testdata/help.golden +++ b/cmd/config/testdata/help.golden @@ -5,6 +5,7 @@ Supported settings: - `analytics-opt-out`: Opt out of analytics tracking - `base-uri`: LaunchDarkly base URI - `output`: Command response output format in either JSON or plain text +- `project`: Default project key Usage: ldcli config [flags] diff --git a/internal/config/config.go b/internal/config/config.go index dfc1e353..b7715dcb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -21,6 +21,7 @@ type ConfigFile struct { AnalyticsOptOut *bool `json:"analytics-opt-out,omitempty" yaml:"analytics-opt-out,omitempty"` BaseURI string `json:"base-uri,omitempty" yaml:"base-uri,omitempty"` Output string `json:"output,omitempty" yaml:"output,omitempty"` + Project string `json:"project,omitempty" yaml:"project,omitempty"` } func NewConfig(rawConfig map[string]interface{}) (ConfigFile, error) { @@ -30,6 +31,7 @@ func NewConfig(rawConfig map[string]interface{}) (ConfigFile, error) { baseURI string err error outputKind output.OutputKind + project string ) if rawConfig[cliflags.AccessTokenFlag] != nil { accessToken = rawConfig[cliflags.AccessTokenFlag].(string) @@ -50,12 +52,16 @@ func NewConfig(rawConfig map[string]interface{}) (ConfigFile, error) { return ConfigFile{}, err } } + if rawConfig[cliflags.ProjectFlag] != nil { + project = rawConfig[cliflags.ProjectFlag].(string) + } return ConfigFile{ AccessToken: accessToken, AnalyticsOptOut: &analyticsOptOut, BaseURI: baseURI, Output: outputKind.String(), + Project: project, }, nil } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 33d09eca..459cd007 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -92,4 +92,17 @@ func TestNewConfig(t *testing.T) { assert.EqualError(t, err, "output is invalid, use 'json' or 'plaintext'") }) + + t.Run("project", func(t *testing.T) { + t.Run("sets the given value", func(t *testing.T) { + rawConfig := map[string]interface{}{ + "project": "test-project-key", + } + + configFile, err := config.NewConfig(rawConfig) + + require.NoError(t, err) + assert.Equal(t, "test-project-key", configFile.Project) + }) + }) } From 7458e012eb00a3101bf58242e0d2d1b3468ec232 Mon Sep 17 00:00:00 2001 From: Danny Olson Date: Wed, 15 May 2024 09:54:47 -0700 Subject: [PATCH 2/4] Support flag in config file --- cmd/cliflags/flags.go | 4 ++++ cmd/config/config.go | 2 ++ cmd/config/testdata/help.golden | 2 ++ internal/config/config.go | 12 ++++++++++++ internal/config/config_test.go | 30 ++++++++++++++++++++++++++++-- 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/cmd/cliflags/flags.go b/cmd/cliflags/flags.go index 6269097a..baab681f 100644 --- a/cmd/cliflags/flags.go +++ b/cmd/cliflags/flags.go @@ -17,6 +17,8 @@ const ( AccessTokenFlagDescription = "LaunchDarkly access token with write-level access" AnalyticsOptOutDescription = "Opt out of analytics tracking" BaseURIFlagDescription = "LaunchDarkly base URI" + EnvironmentFlagDescription = "Default environment key" + FlagFlagDescription = "Default feature flag key" OutputFlagDescription = "Command response output format in either JSON or plain text" ProjectFlagDescription = "Default project key" ) @@ -26,6 +28,8 @@ func AllFlagsHelp() map[string]string { AccessTokenFlag: AccessTokenFlagDescription, AnalyticsOptOut: AnalyticsOptOutDescription, BaseURIFlag: BaseURIFlagDescription, + EnvironmentFlag: EnvironmentFlagDescription, + FlagFlag: FlagFlagDescription, OutputFlag: OutputFlagDescription, ProjectFlag: ProjectFlagDescription, } diff --git a/cmd/config/config.go b/cmd/config/config.go index 2b44c530..3135bf26 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -68,6 +68,8 @@ func NewConfigCmd(analyticsTrackerFn analytics.TrackerFn) *ConfigCmd { cliflags.AccessTokenFlag, cliflags.AnalyticsOptOut, cliflags.BaseURIFlag, + cliflags.EnvironmentFlag, + cliflags.FlagFlag, cliflags.OutputFlag, cliflags.ProjectFlag, } { diff --git a/cmd/config/testdata/help.golden b/cmd/config/testdata/help.golden index 6238b5d3..ea1b4541 100644 --- a/cmd/config/testdata/help.golden +++ b/cmd/config/testdata/help.golden @@ -4,6 +4,8 @@ Supported settings: - `access-token`: LaunchDarkly access token with write-level access - `analytics-opt-out`: Opt out of analytics tracking - `base-uri`: LaunchDarkly base URI +- `environment`: Default environment key +- `flag`: Default feature flag key - `output`: Command response output format in either JSON or plain text - `project`: Default project key diff --git a/internal/config/config.go b/internal/config/config.go index b7715dcb..6a0193d1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -20,6 +20,8 @@ type ConfigFile struct { AccessToken string `json:"access-token,omitempty" yaml:"access-token,omitempty"` AnalyticsOptOut *bool `json:"analytics-opt-out,omitempty" yaml:"analytics-opt-out,omitempty"` BaseURI string `json:"base-uri,omitempty" yaml:"base-uri,omitempty"` + Flag string `json:"flag,omitempty" yaml:"flag,omitempty"` + Environment string `json:"environment,omitempty" yaml:"environment,omitempty"` Output string `json:"output,omitempty" yaml:"output,omitempty"` Project string `json:"project,omitempty" yaml:"project,omitempty"` } @@ -29,7 +31,9 @@ func NewConfig(rawConfig map[string]interface{}) (ConfigFile, error) { accessToken string analyticsOptOut bool baseURI string + environment string err error + flag string outputKind output.OutputKind project string ) @@ -46,6 +50,12 @@ func NewConfig(rawConfig map[string]interface{}) (ConfigFile, error) { if rawConfig[cliflags.BaseURIFlag] != nil { baseURI = rawConfig[cliflags.BaseURIFlag].(string) } + if rawConfig[cliflags.EnvironmentFlag] != nil { + environment = rawConfig[cliflags.EnvironmentFlag].(string) + } + if rawConfig[cliflags.FlagFlag] != nil { + flag = rawConfig[cliflags.FlagFlag].(string) + } if rawConfig[cliflags.OutputFlag] != nil { outputKind, err = output.NewOutputKind(rawConfig[cliflags.OutputFlag].(string)) if err != nil { @@ -60,6 +70,8 @@ func NewConfig(rawConfig map[string]interface{}) (ConfigFile, error) { AccessToken: accessToken, AnalyticsOptOut: &analyticsOptOut, BaseURI: baseURI, + Environment: environment, + Flag: flag, Output: outputKind.String(), Project: project, }, nil diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 459cd007..14a591f2 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -93,16 +93,42 @@ func TestNewConfig(t *testing.T) { assert.EqualError(t, err, "output is invalid, use 'json' or 'plaintext'") }) + t.Run("environment", func(t *testing.T) { + t.Run("sets the given value", func(t *testing.T) { + rawConfig := map[string]interface{}{ + "environment": "test-key", + } + + configFile, err := config.NewConfig(rawConfig) + + require.NoError(t, err) + assert.Equal(t, "test-key", configFile.Environment) + }) + }) + + t.Run("flag", func(t *testing.T) { + t.Run("sets the given value", func(t *testing.T) { + rawConfig := map[string]interface{}{ + "flag": "test-key", + } + + configFile, err := config.NewConfig(rawConfig) + + require.NoError(t, err) + assert.Equal(t, "test-key", configFile.Flag) + }) + }) + t.Run("project", func(t *testing.T) { t.Run("sets the given value", func(t *testing.T) { rawConfig := map[string]interface{}{ - "project": "test-project-key", + "project": "test-key", } configFile, err := config.NewConfig(rawConfig) require.NoError(t, err) - assert.Equal(t, "test-project-key", configFile.Project) + assert.Equal(t, "test-key", configFile.Project) }) }) } From 590c8b475e7bb3c0552f7ab9067b55af7ae66c7a Mon Sep 17 00:00:00 2001 From: Danny Olson Date: Wed, 15 May 2024 10:05:47 -0700 Subject: [PATCH 3/4] Refactor --- cmd/config/config.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/cmd/config/config.go b/cmd/config/config.go index 3135bf26..07ca2620 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -64,16 +64,9 @@ func NewConfigCmd(analyticsTrackerFn analytics.TrackerFn) *ConfigCmd { var sb strings.Builder sb.WriteString("\n\nSupported settings:\n") - for _, s := range []string{ - cliflags.AccessTokenFlag, - cliflags.AnalyticsOptOut, - cliflags.BaseURIFlag, - cliflags.EnvironmentFlag, - cliflags.FlagFlag, - cliflags.OutputFlag, - cliflags.ProjectFlag, - } { - sb.WriteString(fmt.Sprintf("- `%s`: %s\n", s, cliflags.AllFlagsHelp()[s])) + + for flag, description := range cliflags.AllFlagsHelp() { + sb.WriteString(fmt.Sprintf("- `%s`: %s\n", flag, description)) } cmd.Long += sb.String() From 9b90ef7ddb2c9e36de078394e4e277a14f726629 Mon Sep 17 00:00:00 2001 From: Danny Olson Date: Wed, 15 May 2024 10:12:18 -0700 Subject: [PATCH 4/4] Make sure list of config flags is alphabetized --- cmd/config/config.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/cmd/config/config.go b/cmd/config/config.go index 07ca2620..e797c7b3 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "os" + "sort" "strings" "github.com/spf13/cobra" @@ -64,10 +65,7 @@ func NewConfigCmd(analyticsTrackerFn analytics.TrackerFn) *ConfigCmd { var sb strings.Builder sb.WriteString("\n\nSupported settings:\n") - - for flag, description := range cliflags.AllFlagsHelp() { - sb.WriteString(fmt.Sprintf("- `%s`: %s\n", flag, description)) - } + writeAlphabetizedFlags(&sb) cmd.Long += sb.String() analyticsTrackerFn( @@ -296,3 +294,14 @@ func newErr(flag string) error { return errors.NewError(output.CmdOutputError(flag, err)) } + +func writeAlphabetizedFlags(sb *strings.Builder) { + flags := make([]string, 0, len(cliflags.AllFlagsHelp())) + for f := range cliflags.AllFlagsHelp() { + flags = append(flags, f) + } + sort.Strings(flags) + for _, flag := range flags { + sb.WriteString(fmt.Sprintf("- `%s`: %s\n", flag, cliflags.AllFlagsHelp()[flag])) + } +}