Skip to content

feat: Add "coder" CLI #221

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 8 commits into from
Feb 10, 2022
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
Prev Previous commit
Next Next commit
Fix requested changes
  • Loading branch information
kylecarbs committed Feb 10, 2022
commit aed4a621f90b3673638e3cf0517e8e9c5210e8c8
27 changes: 15 additions & 12 deletions cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cli
import (
"fmt"
"net/url"
"os"
"os/user"
"strings"

"github.com/fatih/color"
Expand Down Expand Up @@ -31,7 +31,7 @@ func login() *cobra.Command {
}
serverURL, err := url.Parse(rawURL)
if err != nil {
return err
return xerrors.Errorf("parse raw url %q: %w", rawURL, err)
}
// Default to HTTPs. Enables simple URLs like: master.cdr.dev
if serverURL.Scheme == "" {
Expand All @@ -41,37 +41,40 @@ func login() *cobra.Command {
client := codersdk.New(serverURL)
hasInitialUser, err := client.HasInitialUser(cmd.Context())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's cool that it guides you through set-up the first time you login, if there isn't a user available yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! I wanted the full flow to be available. I added tests now too!

if err != nil {
return err
return xerrors.Errorf("has initial user: %w", err)
}
if !hasInitialUser {
if !isTTY(cmd.InOrStdin()) {
return xerrors.New("the initial user cannot be created in non-interactive mode. use the API")
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Your Coder deployment hasn't been setup!\n", color.HiBlackString(">"))
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Your Coder deployment hasn't been set up!\n", color.HiBlackString(">"))

_, err := runPrompt(cmd, &promptui.Prompt{
Label: "Would you like to create the first user?",
IsConfirm: true,
Default: "y",
})
if err != nil {
return err
return xerrors.Errorf("create user prompt: %w", err)
}
currentUser, err := user.Current()
if err != nil {
return xerrors.Errorf("get current user: %w", err)
}

username, err := runPrompt(cmd, &promptui.Prompt{
Label: "What username would you like?",
Default: os.Getenv("USER"),
Default: currentUser.Username,
})
if err != nil {
return err
return xerrors.Errorf("pick username prompt: %w", err)
}

organization, err := runPrompt(cmd, &promptui.Prompt{
Label: "What is the name of your organization?",
Default: "acme-corp",
})
if err != nil {
return err
return xerrors.Errorf("pick organization prompt: %w", err)
}

email, err := runPrompt(cmd, &promptui.Prompt{
Expand All @@ -85,15 +88,15 @@ func login() *cobra.Command {
},
})
if err != nil {
return err
return xerrors.Errorf("specify email prompt: %w", err)
}

password, err := runPrompt(cmd, &promptui.Prompt{
Label: "Enter a password:",
Mask: '*',
})
if err != nil {
return err
return xerrors.Errorf("specify password prompt: %w", err)
}

_, err = client.CreateInitialUser(cmd.Context(), coderd.CreateInitialUserRequest{
Expand Down Expand Up @@ -122,7 +125,7 @@ func login() *cobra.Command {
return xerrors.Errorf("write server url: %w", err)
}

_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Welcome to Coder, %s! You're logged in.\n", color.HiBlackString(">"), color.HiCyanString(username))
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Welcome to Coder, %s! You're authenticated.\n", color.HiBlackString(">"), color.HiCyanString(username))
return nil
}

Expand Down
10 changes: 4 additions & 6 deletions coderd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"fmt"
"io"
"io/ioutil"
"net"
Expand Down Expand Up @@ -46,11 +45,10 @@ func Root() *cobra.Command {
}
defer listener.Close()

serverURL, err := url.Parse(fmt.Sprintf("http://%s", address))
if err != nil {
return xerrors.Errorf("parse %q: %w", address, err)
}
client := codersdk.New(serverURL)
client := codersdk.New(&url.URL{
Scheme: "http",
Host: address,
})
closer, err := newProvisionerDaemon(cmd.Context(), client, logger)
if err != nil {
return xerrors.Errorf("create provisioner daemon: %w", err)
Expand Down