feat: Output username and password for code server --dev (#1193)

Fixes #825
This commit is contained in:
Mathias Fredriksson
2022-04-27 17:59:37 +03:00
committed by GitHub
parent 0b1ee3303d
commit 8661f92a10
2 changed files with 23 additions and 8 deletions

View File

@ -47,6 +47,13 @@ import (
"github.com/coder/coder/provisionersdk/proto" "github.com/coder/coder/provisionersdk/proto"
) )
var defaultDevUser = codersdk.CreateFirstUserRequest{
Email: "admin@coder.com",
Username: "developer",
Password: "password",
OrganizationName: "acme-corp",
}
// nolint:gocyclo // nolint:gocyclo
func server() *cobra.Command { func server() *cobra.Command {
var ( var (
@ -275,6 +282,9 @@ func server() *cobra.Command {
if err != nil { if err != nil {
return xerrors.Errorf("create first user: %w", err) return xerrors.Errorf("create first user: %w", err)
} }
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "email: %s\n", defaultDevUser.Email)
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "password: %s\n", defaultDevUser.Password)
_, _ = fmt.Fprintln(cmd.ErrOrStderr())
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), cliui.Styles.Wrap.Render(`Started in dev mode. All data is in-memory! `+cliui.Styles.Bold.Render("Do not use in production")+`. Press `+ _, _ = fmt.Fprintf(cmd.ErrOrStderr(), cliui.Styles.Wrap.Render(`Started in dev mode. All data is in-memory! `+cliui.Styles.Bold.Render("Do not use in production")+`. Press `+
cliui.Styles.Field.Render("ctrl+c")+` to clean up provisioned infrastructure.`)+"\n\n") cliui.Styles.Field.Render("ctrl+c")+` to clean up provisioned infrastructure.`)+"\n\n")
@ -441,18 +451,13 @@ func server() *cobra.Command {
} }
func createFirstUser(cmd *cobra.Command, client *codersdk.Client, cfg config.Root) error { func createFirstUser(cmd *cobra.Command, client *codersdk.Client, cfg config.Root) error {
_, err := client.CreateFirstUser(cmd.Context(), codersdk.CreateFirstUserRequest{ _, err := client.CreateFirstUser(cmd.Context(), defaultDevUser)
Email: "admin@coder.com",
Username: "developer",
Password: "password",
OrganizationName: "acme-corp",
})
if err != nil { if err != nil {
return xerrors.Errorf("create first user: %w", err) return xerrors.Errorf("create first user: %w", err)
} }
token, err := client.LoginWithPassword(cmd.Context(), codersdk.LoginWithPasswordRequest{ token, err := client.LoginWithPassword(cmd.Context(), codersdk.LoginWithPasswordRequest{
Email: "admin@coder.com", Email: defaultDevUser.Email,
Password: "password", Password: defaultDevUser.Password,
}) })
if err != nil { if err != nil {
return xerrors.Errorf("login with first user: %w", err) return xerrors.Errorf("login with first user: %w", err)

View File

@ -1,6 +1,7 @@
package cli_test package cli_test
import ( import (
"bytes"
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
@ -18,6 +19,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/goleak" "go.uber.org/goleak"
@ -73,9 +75,17 @@ func TestServer(t *testing.T) {
ctx, cancelFunc := context.WithCancel(context.Background()) ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc() defer cancelFunc()
root, cfg := clitest.New(t, "server", "--dev", "--skip-tunnel", "--address", ":0") root, cfg := clitest.New(t, "server", "--dev", "--skip-tunnel", "--address", ":0")
var stdoutBuf bytes.Buffer
root.SetOutput(&stdoutBuf)
go func() { go func() {
err := root.ExecuteContext(ctx) err := root.ExecuteContext(ctx)
require.ErrorIs(t, err, context.Canceled) require.ErrorIs(t, err, context.Canceled)
// Verify that credentials were output to the terminal.
wantEmail := "email: admin@coder.com"
wantPassword := "password: password"
assert.Contains(t, stdoutBuf.String(), wantEmail, "expected output %q; got no match", wantEmail)
assert.Contains(t, stdoutBuf.String(), wantPassword, "expected output %q; got no match", wantPassword)
}() }()
var token string var token string
require.Eventually(t, func() bool { require.Eventually(t, func() bool {