Skip to content

Commit

Permalink
feat: Open browser automatically during login command (#351)
Browse files Browse the repository at this point in the history
Open browser automatically during login command
  • Loading branch information
dbolson authored Jun 28, 2024
1 parent 58dc226 commit 846c5c3
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 9 deletions.
13 changes: 6 additions & 7 deletions cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

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

Expand Down Expand Up @@ -68,18 +69,16 @@ func run(client login.Client) func(*cobra.Command, []string) error {
return err
}

fullURL := fmt.Sprintf("%s/%s", viper.GetString(cliflags.BaseURIFlag), deviceAuthorization.VerificationURI)
var b strings.Builder
b.WriteString(fmt.Sprintf("Your code is %s\n", deviceAuthorization.UserCode))
b.WriteString("This code verifies your authentication with LaunchDarkly.\n")
b.WriteString(
fmt.Sprintf(
"Press Enter to open the browser or visit %s/%s (^C to quit)\n",
viper.GetString(cliflags.BaseURIFlag),
deviceAuthorization.VerificationURI,
),
)
b.WriteString(fmt.Sprintf("Opening your browser to %s to finish verifying your login.\n", fullURL))
b.WriteString("If your browser does not open automatically, you can paste the above URL into your browser.")
fmt.Fprintln(cmd.OutOrStdout(), b.String())

_ = browser.OpenURL(fullURL)

deviceAuthorizationToken, err := login.FetchToken(
client,
deviceAuthorization.DeviceCode,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/launchdarkly/api-client-go/v14 v14.0.0
github.com/mitchellh/go-homedir v1.1.0
github.com/muesli/reflow v0.3.0
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
2 changes: 1 addition & 1 deletion internal/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func FetchToken(
case "expired_token":
return DeviceAuthorizationToken{}, errors.NewError("Your request has expired. Please try logging in again.")
default:
return DeviceAuthorizationToken{}, errors.NewErrorWrapped("We cannot complete your request.", err)
return DeviceAuthorizationToken{}, errors.NewErrorWrapped(fmt.Sprintf("We cannot complete your request: %s", e.Message), err)
}
time.Sleep(interval)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/login/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestFetchToken_WithError(t *testing.T) {
},
"with an error response": {
errCode: "error_code",
expectedErr: "We cannot complete your request.",
expectedErr: "We cannot complete your request: error message",
},
}
for name, tt := range tests {
Expand Down
23 changes: 23 additions & 0 deletions vendor/github.com/pkg/browser/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions vendor/github.com/pkg/browser/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions vendor/github.com/pkg/browser/browser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/pkg/browser/browser_darwin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions vendor/github.com/pkg/browser/browser_freebsd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/pkg/browser/browser_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions vendor/github.com/pkg/browser/browser_netbsd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions vendor/github.com/pkg/browser/browser_openbsd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/pkg/browser/browser_unsupported.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/pkg/browser/browser_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ github.com/pelletier/go-toml/v2/unstable
# github.com/perimeterx/marshmallow v1.1.5
## explicit; go 1.17
github.com/perimeterx/marshmallow
# github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
## explicit; go 1.14
github.com/pkg/browser
# github.com/pkg/errors v0.9.1
## explicit
github.com/pkg/errors
Expand Down

0 comments on commit 846c5c3

Please sign in to comment.