|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "os/user" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/fatih/color" |
| 10 | + "github.com/go-playground/validator/v10" |
| 11 | + "github.com/manifoldco/promptui" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "golang.org/x/xerrors" |
| 14 | + |
| 15 | + "github.com/coder/coder/coderd" |
| 16 | + "github.com/coder/coder/codersdk" |
| 17 | +) |
| 18 | + |
| 19 | +func login() *cobra.Command { |
| 20 | + return &cobra.Command{ |
| 21 | + Use: "login <url>", |
| 22 | + Args: cobra.ExactArgs(1), |
| 23 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 24 | + rawURL := args[0] |
| 25 | + if !strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://") { |
| 26 | + scheme := "https" |
| 27 | + if strings.HasPrefix(rawURL, "localhost") { |
| 28 | + scheme = "http" |
| 29 | + } |
| 30 | + rawURL = fmt.Sprintf("%s://%s", scheme, rawURL) |
| 31 | + } |
| 32 | + serverURL, err := url.Parse(rawURL) |
| 33 | + if err != nil { |
| 34 | + return xerrors.Errorf("parse raw url %q: %w", rawURL, err) |
| 35 | + } |
| 36 | + // Default to HTTPs. Enables simple URLs like: master.cdr.dev |
| 37 | + if serverURL.Scheme == "" { |
| 38 | + serverURL.Scheme = "https" |
| 39 | + } |
| 40 | + |
| 41 | + client := codersdk.New(serverURL) |
| 42 | + hasInitialUser, err := client.HasInitialUser(cmd.Context()) |
| 43 | + if err != nil { |
| 44 | + return xerrors.Errorf("has initial user: %w", err) |
| 45 | + } |
| 46 | + if !hasInitialUser { |
| 47 | + if !isTTY(cmd.InOrStdin()) { |
| 48 | + return xerrors.New("the initial user cannot be created in non-interactive mode. use the API") |
| 49 | + } |
| 50 | + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Your Coder deployment hasn't been set up!\n", color.HiBlackString(">")) |
| 51 | + |
| 52 | + _, err := runPrompt(cmd, &promptui.Prompt{ |
| 53 | + Label: "Would you like to create the first user?", |
| 54 | + IsConfirm: true, |
| 55 | + Default: "y", |
| 56 | + }) |
| 57 | + if err != nil { |
| 58 | + return xerrors.Errorf("create user prompt: %w", err) |
| 59 | + } |
| 60 | + currentUser, err := user.Current() |
| 61 | + if err != nil { |
| 62 | + return xerrors.Errorf("get current user: %w", err) |
| 63 | + } |
| 64 | + username, err := runPrompt(cmd, &promptui.Prompt{ |
| 65 | + Label: "What username would you like?", |
| 66 | + Default: currentUser.Username, |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + return xerrors.Errorf("pick username prompt: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + organization, err := runPrompt(cmd, &promptui.Prompt{ |
| 73 | + Label: "What is the name of your organization?", |
| 74 | + Default: "acme-corp", |
| 75 | + }) |
| 76 | + if err != nil { |
| 77 | + return xerrors.Errorf("pick organization prompt: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + email, err := runPrompt(cmd, &promptui.Prompt{ |
| 81 | + Label: "What's your email?", |
| 82 | + Validate: func(s string) error { |
| 83 | + err := validator.New().Var(s, "email") |
| 84 | + if err != nil { |
| 85 | + return xerrors.New("That's not a valid email address!") |
| 86 | + } |
| 87 | + return err |
| 88 | + }, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + return xerrors.Errorf("specify email prompt: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + password, err := runPrompt(cmd, &promptui.Prompt{ |
| 95 | + Label: "Enter a password:", |
| 96 | + Mask: '*', |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + return xerrors.Errorf("specify password prompt: %w", err) |
| 100 | + } |
| 101 | + |
| 102 | + _, err = client.CreateInitialUser(cmd.Context(), coderd.CreateInitialUserRequest{ |
| 103 | + Email: email, |
| 104 | + Username: username, |
| 105 | + Password: password, |
| 106 | + Organization: organization, |
| 107 | + }) |
| 108 | + if err != nil { |
| 109 | + return xerrors.Errorf("create initial user: %w", err) |
| 110 | + } |
| 111 | + resp, err := client.LoginWithPassword(cmd.Context(), coderd.LoginWithPasswordRequest{ |
| 112 | + Email: email, |
| 113 | + Password: password, |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + return xerrors.Errorf("login with password: %w", err) |
| 117 | + } |
| 118 | + config := createConfig(cmd) |
| 119 | + err = config.Session().Write(resp.SessionToken) |
| 120 | + if err != nil { |
| 121 | + return xerrors.Errorf("write session token: %w", err) |
| 122 | + } |
| 123 | + err = config.URL().Write(serverURL.String()) |
| 124 | + if err != nil { |
| 125 | + return xerrors.Errorf("write server url: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Welcome to Coder, %s! You're authenticated.\n", color.HiBlackString(">"), color.HiCyanString(username)) |
| 129 | + return nil |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | + }, |
| 134 | + } |
| 135 | +} |
0 commit comments