Skip to content

Commit

Permalink
feat: Add valid config fields to its help (#217)
Browse files Browse the repository at this point in the history
Add valid config fields to its help
  • Loading branch information
dbolson authored Apr 30, 2024
1 parent a469c0c commit ffa9fb3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ ldcli --help

The LaunchDarkly CLI allows you to save preferred settings, either within a config file using the `config` commands, or set as environment variables.

Current respected settings:
Supported settings:

* `access-token` A LaunchDarkly API token with write-level access
* `analytics-optout` Opt out of analytics tracking (default false)
* `access-token` A LaunchDarkly access token with write-level access
* `analytics-opt-out` Opt out of analytics tracking (default false)
* `base-uri` LaunchDarkly base URI (default "https://app.launchdarkly.com")
* `output` Command response output format in either JSON or plain text

To set a value as an environment variable, prepend the variable name with `LD`. For example:
```shell
Expand Down
14 changes: 14 additions & 0 deletions cmd/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,18 @@ const (
OutputFlag = "output"
ProjectFlag = "project"
RoleFlag = "role"

AccessTokenFlagDescription = "LaunchDarkly access token with write-level access"
AnalyticsOptOutDescription = "Opt out of analytics tracking"
BaseURIFlagDescription = "LaunchDarkly base URI"
OutputFlagDescription = "Command response output format in either JSON or plain text"
)

func AllFlagsHelp() map[string]string {
return map[string]string{
AccessTokenFlag: AccessTokenFlagDescription,
AnalyticsOptOut: AnalyticsOptOutDescription,
BaseURIFlag: BaseURIFlagDescription,
OutputFlag: OutputFlagDescription,
}
}
17 changes: 17 additions & 0 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"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -39,6 +40,22 @@ func NewConfigCmd(analyticsTracker analytics.Tracker) *cobra.Command {
},
}

helpFun := cmd.HelpFunc()
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
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]))
}
cmd.Long += sb.String()
helpFun(cmd, args)
})

cmd.Flags().Bool(ListFlag, false, "List configs")
_ = viper.BindPFlag(ListFlag, cmd.Flags().Lookup(ListFlag))
cmd.Flags().Bool(SetFlag, false, "Set a config field to a value")
Expand Down
5 changes: 3 additions & 2 deletions cmd/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package config_test

import (
"ldcli/cmd"
"ldcli/internal/analytics"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"ldcli/cmd"
"ldcli/internal/analytics"
)

func TestNoFlag(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion cmd/config/testdata/help.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
View and modify specific configuration values

Supported settings:
- `access-token`: LaunchDarkly access token with write-level access
- `analytics-opt-out`: Opt out of analytics tracking
- `base-uri`: LaunchDarkly base URI
- `output`: Command response output format in either JSON or plain text

Usage:
ldcli config [flags]

Expand All @@ -10,7 +16,7 @@ Flags:
--unset string Unset a config field

Global Flags:
--access-token string LaunchDarkly API token with write-level access
--access-token string LaunchDarkly access token with write-level access
--analytics-opt-out Opt out of analytics tracking
--base-uri string LaunchDarkly base URI (default "https://app.launchdarkly.com")
-o, --output string Command response output format in either JSON or plain text (default "plaintext")
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewRootCommand(
cmd.PersistentFlags().String(
cliflags.AccessTokenFlag,
"",
"LaunchDarkly API token with write-level access",
cliflags.AccessTokenFlagDescription,
)
err := cmd.MarkPersistentFlagRequired(cliflags.AccessTokenFlag)
if err != nil {
Expand All @@ -92,7 +92,7 @@ func NewRootCommand(
cmd.PersistentFlags().String(
cliflags.BaseURIFlag,
cliflags.BaseURIDefault,
"LaunchDarkly base URI",
cliflags.BaseURIFlagDescription,
)
err = viper.BindPFlag(cliflags.BaseURIFlag, cmd.PersistentFlags().Lookup(cliflags.BaseURIFlag))
if err != nil {
Expand All @@ -102,7 +102,7 @@ func NewRootCommand(
cmd.PersistentFlags().Bool(
cliflags.AnalyticsOptOut,
false,
"Opt out of analytics tracking",
cliflags.AnalyticsOptOutDescription,
)
err = viper.BindPFlag(cliflags.AnalyticsOptOut, cmd.PersistentFlags().Lookup(cliflags.AnalyticsOptOut))
if err != nil {
Expand All @@ -113,7 +113,7 @@ func NewRootCommand(
cliflags.OutputFlag,
"o",
"plaintext",
"Command response output format in either JSON or plain text",
cliflags.OutputFlagDescription,
)
err = viper.BindPFlag(cliflags.OutputFlag, cmd.PersistentFlags().Lookup(cliflags.OutputFlag))
if err != nil {
Expand Down

0 comments on commit ffa9fb3

Please sign in to comment.