|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/go-playground/validator/v10" |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "golang.org/x/xerrors" |
| 9 | + |
| 10 | + "github.com/coder/coder/cli/cliui" |
| 11 | + "github.com/coder/coder/codersdk" |
| 12 | + "github.com/coder/coder/cryptorand" |
| 13 | +) |
| 14 | + |
| 15 | +func userCreate() *cobra.Command { |
| 16 | + var ( |
| 17 | + email string |
| 18 | + username string |
| 19 | + password string |
| 20 | + ) |
| 21 | + cmd := &cobra.Command{ |
| 22 | + Use: "create", |
| 23 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 24 | + client, err := createClient(cmd) |
| 25 | + if err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + organization, err := currentOrganization(cmd, client) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + if username == "" { |
| 33 | + username, err = cliui.Prompt(cmd, cliui.PromptOptions{ |
| 34 | + Text: "Username:", |
| 35 | + }) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + } |
| 40 | + if email == "" { |
| 41 | + email, err = cliui.Prompt(cmd, cliui.PromptOptions{ |
| 42 | + Text: "Email:", |
| 43 | + Validate: func(s string) error { |
| 44 | + err := validator.New().Var(s, "email") |
| 45 | + if err != nil { |
| 46 | + return xerrors.New("That's not a valid email address!") |
| 47 | + } |
| 48 | + return err |
| 49 | + }, |
| 50 | + }) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + } |
| 55 | + if password == "" { |
| 56 | + password, err = cryptorand.StringCharset(cryptorand.Human, 12) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + _, err = client.CreateUser(cmd.Context(), codersdk.CreateUserRequest{ |
| 63 | + Email: email, |
| 64 | + Username: username, |
| 65 | + Password: password, |
| 66 | + OrganizationID: organization.ID, |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + _, _ = fmt.Fprintln(cmd.ErrOrStderr(), `A new user has been created! |
| 72 | +Share the instructions below to get them started. |
| 73 | +`+cliui.Styles.Placeholder.Render("—————————————————————————————————————————————————")+` |
| 74 | +Download the Coder command line for your operating system: |
| 75 | +https://github.com/coder/coder/releases |
| 76 | +
|
| 77 | +Run `+cliui.Styles.Code.Render("coder login "+client.URL.String())+` to authenticate. |
| 78 | +
|
| 79 | +Your email is: `+cliui.Styles.Field.Render(email)+` |
| 80 | +Your password is: `+cliui.Styles.Field.Render(password)+` |
| 81 | +
|
| 82 | +Create a workspace `+cliui.Styles.Code.Render("coder workspaces create")+`!`) |
| 83 | + return nil |
| 84 | + }, |
| 85 | + } |
| 86 | + cmd.Flags().StringVarP(&email, "email", "e", "", "Specifies an email address for the new user.") |
| 87 | + cmd.Flags().StringVarP(&username, "username", "u", "", "Specifies a username for the new user.") |
| 88 | + cmd.Flags().StringVarP(&password, "password", "p", "", "Specifies a password for the new user.") |
| 89 | + return cmd |
| 90 | +} |
0 commit comments