Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support default resources in config #286

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
}
Loading