Skip to content

Commit 7df9697

Browse files
committed
Open browser at login URL
1 parent ce55cee commit 7df9697

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

cmd/lakectl/cmd/login.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ import (
88
"time"
99

1010
"github.com/cenkalti/backoff/v4"
11+
"github.com/skratchdot/open-golang/open"
1112
"github.com/spf13/cobra"
1213
"github.com/treeverse/lakefs/pkg/api/apigen"
1314
)
1415

1516
const (
1617
// TODO(ariels): Underline the link?
17-
webLoginTemplate = `Opening {{.RedirectURL|bold|blue}} where you should log in.
18-
19-
If that URL does not open automatically, please open it manually and log in.
18+
webLoginTemplate = `Opening {{.RedirectURL | blue | underline}} where you should log in.
19+
If it does not open automatically, please try to open it manually and log in.
20+
`
21+
loggedInTemplate = `
22+
[{{.Time | green}}] {{"Logged in." | green | bold}}
2023
`
21-
loggedInTemplate = `Logged in as {{.Username}}.`
2224
)
2325

2426
type webLoginParams struct {
@@ -56,8 +58,11 @@ var loginCmd = &cobra.Command{
5658
redirectURL := serverURL.ResolveReference(relativeLocation)
5759

5860
Write(webLoginTemplate, webLoginParams{RedirectURL: redirectURL.String()})
59-
// TODO(ariels): Open redirectURL (xdg-open on Linux, something on MacOS, maybe
60-
// a library does it?)
61+
err = open.Run(redirectURL.String())
62+
if err != nil {
63+
Warning(fmt.Sprintf("Failed to open URL: %s", err.Error()))
64+
// Keep going, user can manually use the URL.
65+
}
6166

6267
loginToken, err := backoff.RetryWithData(
6368
func() (*apigen.AuthenticationToken, error) {
@@ -94,6 +99,8 @@ var loginCmd = &cobra.Command{
9499
if err != nil {
95100
DieErr(fmt.Errorf("save login token: %w", err))
96101
}
102+
103+
Write(loggedInTemplate, struct{ Time string }{Time: time.Now().Format(time.DateTime)})
97104
},
98105
}
99106

cmd/lakectl/cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,9 @@ func getClientOptions(awsIAMparams *awsiam.IAMAuthParams, serverEndpoint string)
668668
logger := logging.ContextUnavailable().WithField("component", "client_auth")
669669

670670
if awsIAMparams == nil {
671+
if token == nil {
672+
return nil
673+
}
671674
return []apigen.ClientOption{
672675
internalidp.WithLoginTokenAuth(logger, internalidp.NewFixedLoginClient(token.Token)),
673676
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ require (
186186
github.com/sagikazarmark/locafero v0.7.0 // indirect
187187
github.com/samber/mo v1.11.0 // indirect
188188
github.com/shopspring/decimal v1.3.1 // indirect
189+
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
189190
github.com/sourcegraph/conc v0.3.0 // indirect
190191
github.com/spiffe/go-spiffe/v2 v2.5.0 // indirect
191192
github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
984984
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
985985
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
986986
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
987+
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
988+
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
987989
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
988990
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
989991
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=

0 commit comments

Comments
 (0)