Skip to content

Commit

Permalink
feat: support default resources in config (#286)
Browse files Browse the repository at this point in the history
Support environment, flag, project in config file
  • Loading branch information
dbolson authored May 15, 2024
1 parent 1c6f55b commit afa9142
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
6 changes: 6 additions & 0 deletions cmd/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ 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"
)

func AllFlagsHelp() map[string]string {
return map[string]string{
AccessTokenFlag: AccessTokenFlagDescription,
AnalyticsOptOut: AnalyticsOptOutDescription,
BaseURIFlag: BaseURIFlagDescription,
EnvironmentFlag: EnvironmentFlagDescription,
FlagFlag: FlagFlagDescription,
OutputFlag: OutputFlagDescription,
ProjectFlag: ProjectFlagDescription,
}
}
21 changes: 13 additions & 8 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -64,14 +65,7 @@ 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.OutputFlag,
} {
sb.WriteString(fmt.Sprintf("- `%s`: %s\n", s, cliflags.AllFlagsHelp()[s]))
}
writeAlphabetizedFlags(&sb)
cmd.Long += sb.String()

analyticsTrackerFn(
Expand Down Expand Up @@ -300,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]))
}
}
3 changes: 3 additions & 0 deletions cmd/config/testdata/help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ 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

Usage:
ldcli config [flags]
Expand Down
18 changes: 18 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ 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"`
}

func NewConfig(rawConfig map[string]interface{}) (ConfigFile, error) {
var (
accessToken string
analyticsOptOut bool
baseURI string
environment string
err error
flag string
outputKind output.OutputKind
project string
)
if rawConfig[cliflags.AccessTokenFlag] != nil {
accessToken = rawConfig[cliflags.AccessTokenFlag].(string)
Expand All @@ -44,18 +50,30 @@ 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 {
return ConfigFile{}, err
}
}
if rawConfig[cliflags.ProjectFlag] != nil {
project = rawConfig[cliflags.ProjectFlag].(string)
}

return ConfigFile{
AccessToken: accessToken,
AnalyticsOptOut: &analyticsOptOut,
BaseURI: baseURI,
Environment: environment,
Flag: flag,
Output: outputKind.String(),
Project: project,
}, nil
}

Expand Down
39 changes: 39 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,43 @@ 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-key",
}

configFile, err := config.NewConfig(rawConfig)

require.NoError(t, err)
assert.Equal(t, "test-key", configFile.Project)
})
})
}

0 comments on commit afa9142

Please sign in to comment.