Skip to content
Open
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
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
logincmd "github.com/launchdarkly/ldcli/cmd/login"
memberscmd "github.com/launchdarkly/ldcli/cmd/members"
resourcecmd "github.com/launchdarkly/ldcli/cmd/resources"
signupcmd "github.com/launchdarkly/ldcli/cmd/signup"
sourcemapscmd "github.com/launchdarkly/ldcli/cmd/sourcemaps"
"github.com/launchdarkly/ldcli/internal/analytics"
"github.com/launchdarkly/ldcli/internal/config"
Expand Down Expand Up @@ -100,6 +101,7 @@ func NewRootCommand(
"config",
"help",
"login",
"signup",
} {
if cmd.HasParent() && cmd.Parent().Name() == name {
cmd.DisableFlagParsing = true
Expand Down Expand Up @@ -209,6 +211,7 @@ func NewRootCommand(
cmd.AddCommand(configCmd.Cmd())
cmd.AddCommand(NewQuickStartCmd(analyticsTrackerFn, clients.EnvironmentsClient, clients.FlagsClient))
cmd.AddCommand(logincmd.NewLoginCmd(clients.ResourcesClient))
cmd.AddCommand(signupcmd.NewSignupCmd(analyticsTrackerFn))
cmd.AddCommand(resourcecmd.NewResourcesCmd())
cmd.AddCommand(devcmd.NewDevServerCmd(clients.ResourcesClient, analyticsTrackerFn, clients.DevClient))
cmd.AddCommand(sourcemapscmd.NewSourcemapsCmd(clients.ResourcesClient, analyticsTrackerFn))
Expand Down
52 changes: 52 additions & 0 deletions cmd/signup/signup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package signup

import (
"fmt"
"net/url"

"github.com/pkg/browser"
"github.com/spf13/cobra"
"github.com/spf13/viper"

cmdAnalytics "github.com/launchdarkly/ldcli/cmd/analytics"
"github.com/launchdarkly/ldcli/cmd/cliflags"
"github.com/launchdarkly/ldcli/internal/analytics"
)

func NewSignupCmd(analyticsTrackerFn analytics.TrackerFn) *cobra.Command {
cmd := &cobra.Command{
Long: "Open your browser to create a new LaunchDarkly account",
PreRun: func(cmd *cobra.Command, args []string) {
analyticsTrackerFn(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
).SendCommandRunEvent(cmdAnalytics.CmdRunEventProperties(cmd, "signup", nil))
},
RunE: run,
Short: "Create a new LaunchDarkly account",
Use: "signup",
}

return cmd
}

func run(cmd *cobra.Command, args []string) error {
signupURL, err := url.JoinPath(
viper.GetString(cliflags.BaseURIFlag),
"/signup",
)
if err != nil {
return fmt.Errorf("failed to construct signup URL: %w", err)
}

fmt.Fprintf(cmd.OutOrStdout(), "Opening your browser to %s to create a new LaunchDarkly account.\n", signupURL)
fmt.Fprintln(cmd.OutOrStdout(), "If your browser does not open automatically, you can paste the above URL into your browser.")

err = browser.OpenURL(signupURL)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Warning: failed to open browser automatically: %v\n", err)
}

return nil
}
26 changes: 26 additions & 0 deletions cmd/signup/signup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package signup

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/launchdarkly/ldcli/internal/analytics"
)

func TestSignupCmd(t *testing.T) {
t.Run("creates signup command with correct attributes", func(t *testing.T) {
mockTracker := &analytics.MockTracker{}
analyticsTrackerFn := func(accessToken string, baseURI string, optOut bool) analytics.Tracker {
return mockTracker
}

cmd := NewSignupCmd(analyticsTrackerFn)

assert.Equal(t, "signup", cmd.Use)
assert.Equal(t, "Create a new LaunchDarkly account", cmd.Short)
assert.Equal(t, "Open your browser to create a new LaunchDarkly account", cmd.Long)
assert.NotNil(t, cmd.RunE)
assert.NotNil(t, cmd.PreRun)
})
}
1 change: 1 addition & 0 deletions cmd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Commands:
{{rpad "config" 29}} View and modify specific configuration values
{{rpad "completion" 29}} Enable command autocompletion within supported shells
{{rpad "login" 29}} Log in to your LaunchDarkly account
{{rpad "signup" 29}} Create a new LaunchDarkly account
{{rpad "dev-server" 29}} Run a development server to serve flags locally

Common resource commands:
Expand Down
Loading