Skip to content

Commit 7434bbb

Browse files
committed
Add a command to sign up that directs the user to the sign up page
1 parent 65d1bc3 commit 7434bbb

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
logincmd "github.com/launchdarkly/ldcli/cmd/login"
2323
memberscmd "github.com/launchdarkly/ldcli/cmd/members"
2424
resourcecmd "github.com/launchdarkly/ldcli/cmd/resources"
25+
signupcmd "github.com/launchdarkly/ldcli/cmd/signup"
2526
sourcemapscmd "github.com/launchdarkly/ldcli/cmd/sourcemaps"
2627
"github.com/launchdarkly/ldcli/internal/analytics"
2728
"github.com/launchdarkly/ldcli/internal/config"
@@ -100,6 +101,7 @@ func NewRootCommand(
100101
"config",
101102
"help",
102103
"login",
104+
"signup",
103105
} {
104106
if cmd.HasParent() && cmd.Parent().Name() == name {
105107
cmd.DisableFlagParsing = true
@@ -209,6 +211,7 @@ func NewRootCommand(
209211
cmd.AddCommand(configCmd.Cmd())
210212
cmd.AddCommand(NewQuickStartCmd(analyticsTrackerFn, clients.EnvironmentsClient, clients.FlagsClient))
211213
cmd.AddCommand(logincmd.NewLoginCmd(clients.ResourcesClient))
214+
cmd.AddCommand(signupcmd.NewSignupCmd(analyticsTrackerFn))
212215
cmd.AddCommand(resourcecmd.NewResourcesCmd())
213216
cmd.AddCommand(devcmd.NewDevServerCmd(clients.ResourcesClient, analyticsTrackerFn, clients.DevClient))
214217
cmd.AddCommand(sourcemapscmd.NewSourcemapsCmd(clients.ResourcesClient, analyticsTrackerFn))

cmd/signup/signup.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package signup
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/pkg/browser"
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
9+
10+
cmdAnalytics "github.com/launchdarkly/ldcli/cmd/analytics"
11+
"github.com/launchdarkly/ldcli/cmd/cliflags"
12+
"github.com/launchdarkly/ldcli/internal/analytics"
13+
)
14+
15+
const signupURL = "https://app.launchdarkly.com/signup"
16+
17+
func NewSignupCmd(analyticsTrackerFn analytics.TrackerFn) *cobra.Command {
18+
cmd := &cobra.Command{
19+
Long: "Open your browser to create a new LaunchDarkly account at " + signupURL,
20+
PreRun: func(cmd *cobra.Command, args []string) {
21+
analyticsTrackerFn(
22+
viper.GetString(cliflags.AccessTokenFlag),
23+
viper.GetString(cliflags.BaseURIFlag),
24+
viper.GetBool(cliflags.AnalyticsOptOut),
25+
).SendCommandRunEvent(cmdAnalytics.CmdRunEventProperties(cmd, "signup", nil))
26+
},
27+
RunE: run,
28+
Short: "Create a new LaunchDarkly account",
29+
Use: "signup",
30+
}
31+
32+
return cmd
33+
}
34+
35+
func run(cmd *cobra.Command, args []string) error {
36+
fmt.Fprintf(cmd.OutOrStdout(), "Opening your browser to %s to create a new LaunchDarkly account.\n", signupURL)
37+
fmt.Fprintln(cmd.OutOrStdout(), "If your browser does not open automatically, you can paste the above URL into your browser.")
38+
39+
err := browser.OpenURL(signupURL)
40+
if err != nil {
41+
return fmt.Errorf("failed to open browser: %w", err)
42+
}
43+
44+
return nil
45+
}

cmd/signup/signup_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package signup
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/launchdarkly/ldcli/internal/analytics"
9+
)
10+
11+
func TestSignupCmd(t *testing.T) {
12+
t.Run("creates signup command with correct attributes", func(t *testing.T) {
13+
mockTracker := &analytics.MockTracker{}
14+
analyticsTrackerFn := func(accessToken string, baseURI string, optOut bool) analytics.Tracker {
15+
return mockTracker
16+
}
17+
18+
cmd := NewSignupCmd(analyticsTrackerFn)
19+
20+
assert.Equal(t, "signup", cmd.Use)
21+
assert.Equal(t, "Create a new LaunchDarkly account", cmd.Short)
22+
assert.Contains(t, cmd.Long, signupURL)
23+
assert.NotNil(t, cmd.RunE)
24+
assert.NotNil(t, cmd.PreRun)
25+
})
26+
}

cmd/templates.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Commands:
1515
{{rpad "config" 29}} View and modify specific configuration values
1616
{{rpad "completion" 29}} Enable command autocompletion within supported shells
1717
{{rpad "login" 29}} Log in to your LaunchDarkly account
18+
{{rpad "signup" 29}} Create a new LaunchDarkly account
1819
{{rpad "dev-server" 29}} Run a development server to serve flags locally
1920
2021
Common resource commands:

0 commit comments

Comments
 (0)