Skip to content
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
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"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved these to constants to remove duplication.

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) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This runs the existing help fn after updating the command's Long value.

var sb strings.Builder
sb.WriteString("\n\nSupported settings:\n")
for _, s := range []string{
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This uses a slice to preserve order since a map doesn't.

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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor (and I know, I'm tagging this on a test file) but I noticed that --unset is marked as taking a string parameter whereas --set isn't marked to have any parameter. It should also have string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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