Skip to content

feat(cli): make url optional for login command (#10925) #12466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,26 @@ func (r *RootCmd) login() *clibase.Cmd {
useTokenForSession bool
)
cmd := &clibase.Cmd{
Use: "login <url>",
Use: "login [<url>]",
Short: "Authenticate with Coder deployment",
Middleware: clibase.RequireRangeArgs(0, 1),
Handler: func(inv *clibase.Invocation) error {
ctx := inv.Context()
rawURL := ""
if len(inv.Args) == 0 {
rawURL = r.clientURL.String()
if rawURL != "" && rawURL == inv.Environ.Get(envURL) {
_, _ = fmt.Fprintf(inv.Stdout, "Attempting to authenticate with environment URL: %s\n", rawURL)
}
} else {
rawURL = inv.Args[0]
}

if url, err := r.createConfig().URL().Read(); rawURL == "" && err == nil {
_, _ = fmt.Fprintf(inv.Stdout, "Attempting to authenticate with config URL: %s\n", url)
rawURL = url
}

if rawURL == "" {
return xerrors.Errorf("no url argument provided")
}
Expand Down
56 changes: 56 additions & 0 deletions cli/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,62 @@ func TestLogin(t *testing.T) {
<-doneChan
})

t.Run("ExistingUserURLSavedInConfig", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
url := client.URL.String()
coderdtest.CreateFirstUser(t, client)

inv, root := clitest.New(t, "login", "--no-open")
clitest.SetupConfig(t, client, root)

doneChan := make(chan struct{})
pty := ptytest.New(t).Attach(inv)
go func() {
defer close(doneChan)
err := inv.Run()
assert.NoError(t, err)
}()

pty.ExpectMatch(fmt.Sprintf("Attempting to authenticate with config URL: %s", url))
pty.ExpectMatch("Paste your token here:")
pty.WriteLine(client.SessionToken())
if runtime.GOOS != "windows" {
// For some reason, the match does not show up on Windows.
pty.ExpectMatch(client.SessionToken())
}
pty.ExpectMatch("Welcome to Coder")
<-doneChan
})

t.Run("ExistingUserURLSavedInEnv", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
url := client.URL.String()
coderdtest.CreateFirstUser(t, client)

inv, _ := clitest.New(t, "login", "--no-open")
inv.Environ.Set("CODER_URL", url)

doneChan := make(chan struct{})
pty := ptytest.New(t).Attach(inv)
go func() {
defer close(doneChan)
err := inv.Run()
assert.NoError(t, err)
}()

pty.ExpectMatch(fmt.Sprintf("Attempting to authenticate with environment URL: %s", url))
pty.ExpectMatch("Paste your token here:")
pty.WriteLine(client.SessionToken())
if runtime.GOOS != "windows" {
// For some reason, the match does not show up on Windows.
pty.ExpectMatch(client.SessionToken())
}
pty.ExpectMatch("Welcome to Coder")
<-doneChan
})

t.Run("ExistingUserInvalidTokenTTY", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
Expand Down
2 changes: 1 addition & 1 deletion cli/logout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestLogout(t *testing.T) {
go func() {
defer close(logoutChan)
err = logout.Run()
assert.ErrorContains(t, err, "You are not logged in. Try logging in using 'coder login <url>'.")
assert.ErrorContains(t, err, "You are not logged in. Try logging in using 'coder login'.")
}()

<-logoutChan
Expand Down
11 changes: 8 additions & 3 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ const (
varVerbose = "verbose"
varOrganizationSelect = "organization"
varDisableDirect = "disable-direct-connections"
notLoggedInMessage = "You are not logged in. Try logging in using 'coder login <url>'."

notLoggedInMessage = "You are not logged in. Try logging in using 'coder login <url>'."
notLoggedInURLSavedMessage = "You are not logged in. Try logging in using 'coder login'."

envNoVersionCheck = "CODER_NO_VERSION_WARNING"
envNoFeatureWarning = "CODER_NO_FEATURE_WARNING"
Expand All @@ -77,7 +79,10 @@ const (
envURL = "CODER_URL"
)

var errUnauthenticated = xerrors.New(notLoggedInMessage)
var (
errUnauthenticated = xerrors.New(notLoggedInMessage)
errUnauthenticatedURLSaved = xerrors.New(notLoggedInURLSavedMessage)
)

func (r *RootCmd) Core() []*clibase.Cmd {
// Please re-sort this list alphabetically if you change it!
Expand Down Expand Up @@ -574,7 +579,7 @@ func (r *RootCmd) initClientInternal(client *codersdk.Client, allowTokenMissing
// If the configuration files are absent, the user is logged out
if os.IsNotExist(err) {
if !allowTokenMissing {
return errUnauthenticated
return errUnauthenticatedURLSaved
}
} else if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cli/testdata/coder_login_--help.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
coder v0.0.0-devel

USAGE:
coder login [flags] <url>
coder login [flags] [<url>]

Authenticate with Coder deployment

Expand Down