fix: respect global `--url` flag in `coder login` (#5613)

This commit is contained in:
Colin Adler 2023-01-06 20:57:25 -06:00 committed by GitHub
parent 9b602f55e0
commit 888766c10d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 4 deletions

View File

@ -49,9 +49,18 @@ func login() *cobra.Command {
cmd := &cobra.Command{
Use: "login <url>",
Short: "Authenticate with Coder deployment",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
rawURL := args[0]
rawURL := ""
if len(args) == 0 {
var err error
rawURL, err = cmd.Flags().GetString(varURL)
if err != nil {
return xerrors.Errorf("get global url flag")
}
} else {
rawURL = args[0]
}
if !strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://") {
scheme := "https"

View File

@ -68,13 +68,45 @@ func TestLogin(t *testing.T) {
<-doneChan
})
t.Run("InitialUserFlags", func(t *testing.T) {
t.Run("InitialUserTTYFlag", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
// The --force-tty flag is required on Windows, because the `isatty` library does not
// accurately detect Windows ptys when they are not attached to a process:
// https://github.com/mattn/go-isatty/issues/59
doneChan := make(chan struct{})
root, _ := clitest.New(t, "--url", client.URL.String(), "login", "--force-tty")
pty := ptytest.New(t)
root.SetIn(pty.Input())
root.SetOut(pty.Output())
go func() {
defer close(doneChan)
err := root.Execute()
assert.NoError(t, err)
}()
matches := []string{
"first user?", "yes",
"username", "testuser",
"email", "user@coder.com",
"password", "password",
"password", "password", // Confirm.
"trial", "yes",
}
for i := 0; i < len(matches); i += 2 {
match := matches[i]
value := matches[i+1]
pty.ExpectMatch(match)
pty.WriteLine(value)
}
pty.ExpectMatch("Welcome to Coder")
<-doneChan
})
t.Run("InitialUserFlags", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
doneChan := make(chan struct{})
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")
pty := ptytest.New(t)
root.SetIn(pty.Input())

View File

@ -97,8 +97,8 @@ func Core() []*cobra.Command {
update(),
users(),
versionCmd(),
workspaceAgent(),
vscodeipcCmd(),
workspaceAgent(),
}
}