diff --git a/cli/login.go b/cli/login.go index 2c6498a63f..03332a0f8f 100644 --- a/cli/login.go +++ b/cli/login.go @@ -49,9 +49,18 @@ func login() *cobra.Command { cmd := &cobra.Command{ Use: "login ", 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" diff --git a/cli/login_test.go b/cli/login_test.go index 540adc13c2..bf4507b61b 100644 --- a/cli/login_test.go +++ b/cli/login_test.go @@ -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()) diff --git a/cli/root.go b/cli/root.go index cc9cae9488..eb1bc98cf1 100644 --- a/cli/root.go +++ b/cli/root.go @@ -97,8 +97,8 @@ func Core() []*cobra.Command { update(), users(), versionCmd(), - workspaceAgent(), vscodeipcCmd(), + workspaceAgent(), } }