Skip to content

Commit 888766c

Browse files
authored
fix: respect global --url flag in coder login (#5613)
1 parent 9b602f5 commit 888766c

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

cli/login.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,18 @@ func login() *cobra.Command {
4949
cmd := &cobra.Command{
5050
Use: "login <url>",
5151
Short: "Authenticate with Coder deployment",
52-
Args: cobra.ExactArgs(1),
52+
Args: cobra.MaximumNArgs(1),
5353
RunE: func(cmd *cobra.Command, args []string) error {
54-
rawURL := args[0]
54+
rawURL := ""
55+
if len(args) == 0 {
56+
var err error
57+
rawURL, err = cmd.Flags().GetString(varURL)
58+
if err != nil {
59+
return xerrors.Errorf("get global url flag")
60+
}
61+
} else {
62+
rawURL = args[0]
63+
}
5564

5665
if !strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://") {
5766
scheme := "https"

cli/login_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,45 @@ func TestLogin(t *testing.T) {
6868
<-doneChan
6969
})
7070

71-
t.Run("InitialUserFlags", func(t *testing.T) {
71+
t.Run("InitialUserTTYFlag", func(t *testing.T) {
7272
t.Parallel()
7373
client := coderdtest.New(t, nil)
7474
// The --force-tty flag is required on Windows, because the `isatty` library does not
7575
// accurately detect Windows ptys when they are not attached to a process:
7676
// https://github.com/mattn/go-isatty/issues/59
7777
doneChan := make(chan struct{})
78+
root, _ := clitest.New(t, "--url", client.URL.String(), "login", "--force-tty")
79+
pty := ptytest.New(t)
80+
root.SetIn(pty.Input())
81+
root.SetOut(pty.Output())
82+
go func() {
83+
defer close(doneChan)
84+
err := root.Execute()
85+
assert.NoError(t, err)
86+
}()
87+
88+
matches := []string{
89+
"first user?", "yes",
90+
"username", "testuser",
91+
"email", "user@coder.com",
92+
"password", "password",
93+
"password", "password", // Confirm.
94+
"trial", "yes",
95+
}
96+
for i := 0; i < len(matches); i += 2 {
97+
match := matches[i]
98+
value := matches[i+1]
99+
pty.ExpectMatch(match)
100+
pty.WriteLine(value)
101+
}
102+
pty.ExpectMatch("Welcome to Coder")
103+
<-doneChan
104+
})
105+
106+
t.Run("InitialUserFlags", func(t *testing.T) {
107+
t.Parallel()
108+
client := coderdtest.New(t, nil)
109+
doneChan := make(chan struct{})
78110
root, _ := clitest.New(t, "login", client.URL.String(), "--first-user-username", "testuser", "--first-user-email", "user@coder.com", "--first-user-password", "password", "--first-user-trial")
79111
pty := ptytest.New(t)
80112
root.SetIn(pty.Input())

cli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ func Core() []*cobra.Command {
9797
update(),
9898
users(),
9999
versionCmd(),
100-
workspaceAgent(),
101100
vscodeipcCmd(),
101+
workspaceAgent(),
102102
}
103103
}
104104

0 commit comments

Comments
 (0)