Skip to content

Commit 8661f92

Browse files
authored
feat: Output username and password for code server --dev (#1193)
Fixes #825
1 parent 0b1ee33 commit 8661f92

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

cli/server.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ import (
4747
"github.com/coder/coder/provisionersdk/proto"
4848
)
4949

50+
var defaultDevUser = codersdk.CreateFirstUserRequest{
51+
Email: "admin@coder.com",
52+
Username: "developer",
53+
Password: "password",
54+
OrganizationName: "acme-corp",
55+
}
56+
5057
// nolint:gocyclo
5158
func server() *cobra.Command {
5259
var (
@@ -275,6 +282,9 @@ func server() *cobra.Command {
275282
if err != nil {
276283
return xerrors.Errorf("create first user: %w", err)
277284
}
285+
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "email: %s\n", defaultDevUser.Email)
286+
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "password: %s\n", defaultDevUser.Password)
287+
_, _ = fmt.Fprintln(cmd.ErrOrStderr())
278288

279289
_, _ = 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 `+
280290
cliui.Styles.Field.Render("ctrl+c")+` to clean up provisioned infrastructure.`)+"\n\n")
@@ -441,18 +451,13 @@ func server() *cobra.Command {
441451
}
442452

443453
func createFirstUser(cmd *cobra.Command, client *codersdk.Client, cfg config.Root) error {
444-
_, err := client.CreateFirstUser(cmd.Context(), codersdk.CreateFirstUserRequest{
445-
Email: "admin@coder.com",
446-
Username: "developer",
447-
Password: "password",
448-
OrganizationName: "acme-corp",
449-
})
454+
_, err := client.CreateFirstUser(cmd.Context(), defaultDevUser)
450455
if err != nil {
451456
return xerrors.Errorf("create first user: %w", err)
452457
}
453458
token, err := client.LoginWithPassword(cmd.Context(), codersdk.LoginWithPasswordRequest{
454-
Email: "admin@coder.com",
455-
Password: "password",
459+
Email: defaultDevUser.Email,
460+
Password: defaultDevUser.Password,
456461
})
457462
if err != nil {
458463
return xerrors.Errorf("login with first user: %w", err)

cli/server_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli_test
22

33
import (
4+
"bytes"
45
"context"
56
"crypto/ecdsa"
67
"crypto/elliptic"
@@ -18,6 +19,7 @@ import (
1819
"testing"
1920
"time"
2021

22+
"github.com/stretchr/testify/assert"
2123
"github.com/stretchr/testify/require"
2224
"go.uber.org/goleak"
2325

@@ -73,9 +75,17 @@ func TestServer(t *testing.T) {
7375
ctx, cancelFunc := context.WithCancel(context.Background())
7476
defer cancelFunc()
7577
root, cfg := clitest.New(t, "server", "--dev", "--skip-tunnel", "--address", ":0")
78+
var stdoutBuf bytes.Buffer
79+
root.SetOutput(&stdoutBuf)
7680
go func() {
7781
err := root.ExecuteContext(ctx)
7882
require.ErrorIs(t, err, context.Canceled)
83+
84+
// Verify that credentials were output to the terminal.
85+
wantEmail := "email: admin@coder.com"
86+
wantPassword := "password: password"
87+
assert.Contains(t, stdoutBuf.String(), wantEmail, "expected output %q; got no match", wantEmail)
88+
assert.Contains(t, stdoutBuf.String(), wantPassword, "expected output %q; got no match", wantPassword)
7989
}()
8090
var token string
8191
require.Eventually(t, func() bool {

0 commit comments

Comments
 (0)