Skip to content

Commit 2f0042f

Browse files
authored
feat(REL-12779): Add a command to sign up that directs the user to the sign up page (#665)
Add signup command
1 parent a7206fd commit 2f0042f

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

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

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.Equal(t, "Open your browser to create a new LaunchDarkly account", cmd.Long)
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)