feat: Add users create and list commands (#1111)

This allows for *extremely basic* user management.
This commit is contained in:
Kyle Carberry
2022-04-24 20:08:26 -05:00
committed by GitHub
parent 7496c3da81
commit be974cf280
21 changed files with 245 additions and 127 deletions

90
cli/usercreate.go Normal file
View File

@ -0,0 +1,90 @@
package cli
import (
"fmt"
"github.com/go-playground/validator/v10"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/cryptorand"
)
func userCreate() *cobra.Command {
var (
email string
username string
password string
)
cmd := &cobra.Command{
Use: "create",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
if username == "" {
username, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Username:",
})
if err != nil {
return err
}
}
if email == "" {
email, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Email:",
Validate: func(s string) error {
err := validator.New().Var(s, "email")
if err != nil {
return xerrors.New("That's not a valid email address!")
}
return err
},
})
if err != nil {
return err
}
}
if password == "" {
password, err = cryptorand.StringCharset(cryptorand.Human, 12)
if err != nil {
return err
}
}
_, err = client.CreateUser(cmd.Context(), codersdk.CreateUserRequest{
Email: email,
Username: username,
Password: password,
OrganizationID: organization.ID,
})
if err != nil {
return err
}
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), `A new user has been created!
Share the instructions below to get them started.
`+cliui.Styles.Placeholder.Render("—————————————————————————————————————————————————")+`
Download the Coder command line for your operating system:
https://github.com/coder/coder/releases
Run `+cliui.Styles.Code.Render("coder login "+client.URL.String())+` to authenticate.
Your email is: `+cliui.Styles.Field.Render(email)+`
Your password is: `+cliui.Styles.Field.Render(password)+`
Create a workspace `+cliui.Styles.Code.Render("coder workspaces create")+`!`)
return nil
},
}
cmd.Flags().StringVarP(&email, "email", "e", "", "Specifies an email address for the new user.")
cmd.Flags().StringVarP(&username, "username", "u", "", "Specifies a username for the new user.")
cmd.Flags().StringVarP(&password, "password", "p", "", "Specifies a password for the new user.")
return cmd
}

42
cli/usercreate_test.go Normal file
View File

@ -0,0 +1,42 @@
package cli_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/pty/ptytest"
)
func TestUserCreate(t *testing.T) {
t.Parallel()
t.Run("Prompts", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
cmd, root := clitest.New(t, "users", "create")
clitest.SetupConfig(t, client, root)
doneChan := make(chan struct{})
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
go func() {
defer close(doneChan)
err := cmd.Execute()
require.NoError(t, err)
}()
matches := []string{
"Username", "dean",
"Email", "dean@coder.com",
}
for i := 0; i < len(matches); i += 2 {
match := matches[i]
value := matches[i+1]
pty.ExpectMatch(match)
pty.WriteLine(value)
}
<-doneChan
})
}

46
cli/userlist.go Normal file
View File

@ -0,0 +1,46 @@
package cli
import (
"fmt"
"sort"
"time"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"github.com/coder/coder/codersdk"
)
func userList() *cobra.Command {
return &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
return err
}
users, err := client.Users(cmd.Context(), codersdk.UsersRequest{})
if err != nil {
return err
}
sort.Slice(users, func(i, j int) bool {
return users[i].Username < users[j].Username
})
tableWriter := table.NewWriter()
tableWriter.SetStyle(table.StyleLight)
tableWriter.Style().Options.SeparateColumns = false
tableWriter.AppendHeader(table.Row{"Username", "Email", "Created At"})
for _, user := range users {
tableWriter.AppendRow(table.Row{
user.Username,
user.Email,
user.CreatedAt.Format(time.Stamp),
})
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), tableWriter.Render())
return err
},
}
}

30
cli/userlist_test.go Normal file
View File

@ -0,0 +1,30 @@
package cli_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/pty/ptytest"
)
func TestUserList(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
cmd, root := clitest.New(t, "users", "list")
clitest.SetupConfig(t, client, root)
doneChan := make(chan struct{})
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
go func() {
defer close(doneChan)
err := cmd.Execute()
require.NoError(t, err)
}()
pty.ExpectMatch("coder.com")
<-doneChan
}

View File

@ -6,5 +6,6 @@ func users() *cobra.Command {
cmd := &cobra.Command{
Use: "users",
}
cmd.AddCommand(userCreate(), userList())
return cmd
}