Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31b3b9b

Browse files
committed
fix: addressed review comments
1 parent 5deb4da commit 31b3b9b

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

cli/server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
395395

396396
config := r.createConfig()
397397

398+
if vals.PostgresURL != "" && (vals.PostgresHost != "" || vals.PostgresUsername != "" ||
399+
vals.PostgresPassword != "" || vals.PostgresDatabase != "") {
400+
return xerrors.New("cannot specify both --postgres-url and individual postgres connection flags " +
401+
"(--postgres-host, --postgres-username, etc). Please use only one connection method")
402+
}
403+
398404
if vals.PostgresURL == "" && vals.PostgresHost != "" && vals.PostgresUsername != "" && vals.PostgresPassword != "" && vals.PostgresDatabase != "" {
399405
pgURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?%s",
400406
vals.PostgresUsername.String(),
@@ -408,7 +414,6 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
408414
if err != nil {
409415
return err
410416
}
411-
cliui.Infof(inv.Stdout, "Created PostgreSQL URL from provided flags")
412417
}
413418

414419
builtinPostgres := false

cli/server_test.go

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,28 +157,68 @@ func TestServer(t *testing.T) {
157157
t.Run("PostgresURLFromFlags", func(t *testing.T) {
158158
t.Parallel()
159159

160+
if runtime.GOOS != "linux" || testing.Short() {
161+
// Skip on non-Linux because it spawns a PostgreSQL instance.
162+
t.SkipNow()
163+
}
164+
connectionURL, err := dbtestutil.Open(t)
165+
require.NoError(t, err)
166+
167+
pgURL, err := url.Parse(connectionURL)
168+
require.NoError(t, err)
169+
password, _ := pgURL.User.Password()
170+
database := strings.TrimPrefix(pgURL.Path, "/")
171+
172+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitSuperLong*3)
173+
defer cancelFunc()
174+
160175
inv, cfg := clitest.New(t,
176+
"server",
177+
"--http-address", ":0",
178+
"--access-url", "https://foobarbaz.mydomain",
179+
"--cache-dir", t.TempDir(),
180+
"--postgres-host", pgURL.Hostname(),
181+
"--postgres-port", pgURL.Port(),
182+
"--postgres-username", pgURL.User.Username(),
183+
"--postgres-password", password,
184+
"--postgres-database", database,
185+
"--postgres-options", pgURL.Query().Encode(),
186+
)
187+
188+
clitest.Start(t, inv.WithContext(ctx))
189+
accessURL := waitAccessURL(t, cfg)
190+
client := codersdk.New(accessURL)
191+
192+
_, err = client.CreateFirstUser(ctx, coderdtest.FirstUserParams)
193+
require.NoError(t, err)
194+
})
195+
196+
t.Run("InvalidPostgresConnectionFlags", func(t *testing.T) {
197+
t.Parallel()
198+
199+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitSuperLong*3)
200+
defer cancelFunc()
201+
202+
inv, _ := clitest.New(t,
161203
"server",
162204
"--http-address", ":0",
163205
"--access-url", "https://foobarbaz.mydomain",
164206
"--cache-dir", t.TempDir(),
165207
"--postgres-host", "localhost",
208+
"--postgres-host", "localhost",
166209
"--postgres-port", "5432",
167210
"--postgres-username", "coder",
168211
"--postgres-password", "password",
169212
"--postgres-database", "coder",
170213
"--postgres-options", "sslmode=disable",
214+
"--postgres-url", "postgres://coder:password@localhost:5432/coder?sslmode=disable",
171215
)
172216

173-
pty := ptytest.New(t).Attach(inv)
174-
175-
clitest.Start(t, inv)
176-
177-
// Just wait for startup
178-
_ = waitAccessURL(t, cfg)
179-
180-
pty.ExpectMatch("Created PostgreSQL URL from provided flags")
217+
err := inv.WithContext(ctx).Run()
218+
require.Error(t, err)
219+
require.ErrorContains(t, err, "cannot specify both --postgres-url and individual postgres connection flags (--postgres-host, --postgres-username, etc). Please use only one connection method")
181220
})
221+
182222
t.Run("BuiltinPostgres", func(t *testing.T) {
183223
t.Parallel()
184224
if testing.Short() {

0 commit comments

Comments
 (0)