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

Skip to content

Commit 7e8573c

Browse files
committed
return bool from validatePostgresFlags
1 parent f50963b commit 7e8573c

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

cli/server.go

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

396396
config := r.createConfig()
397397

398-
if err := validatePostgresFlags(vals); err != nil {
398+
useIndividualFlags, err := validatePostgresFlags(vals)
399+
if err != nil {
399400
return xerrors.Errorf("validate postgres configuration: %w", err)
400401
}
401402

402-
if vals.PostgresURL == "" && vals.PostgresHost != "" && vals.PostgresUsername != "" && vals.PostgresPassword != "" && vals.PostgresDatabase != "" {
403+
if useIndividualFlags {
403404
err = vals.PostgresURL.Set((&url.URL{
404405
Scheme: "postgres",
405406
User: url.UserPassword(vals.PostgresUsername.String(), vals.PostgresPassword.String()),
@@ -1268,17 +1269,25 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
12681269
}
12691270

12701271
// validatePostgresFlags checks if the provided PostgreSQL connection flags are valid.
1271-
// It returns an error if:
1272-
// - Both postgres-url and individual flags are specified
1273-
// - Individual flags are used but some required flags are missing
1274-
func validatePostgresFlags(vals *codersdk.DeploymentValues) error {
1272+
// and returns whether individual connection flags should be used.
1273+
// Returns:
1274+
// - useIndividualFlags: true if individual connection flags should be used
1275+
// - error: non-nil if the configuration is invalid
1276+
func validatePostgresFlags(vals *codersdk.DeploymentValues) (useIndividualFlags bool, err error) {
12751277
// Check for conflicting connection methods
1276-
if vals.PostgresURL != "" && (vals.PostgresHost != "" || vals.PostgresUsername != "" ||
1277-
vals.PostgresPassword != "" || vals.PostgresDatabase != "") {
1278-
return xerrors.New("cannot specify both --postgres-url and individual postgres connection flags " +
1278+
hasURL := vals.PostgresURL != ""
1279+
hasIndividualFlags := vals.PostgresHost != "" || vals.PostgresUsername != "" ||
1280+
vals.PostgresPassword != "" || vals.PostgresDatabase != ""
1281+
1282+
if hasURL && hasIndividualFlags {
1283+
return false, xerrors.New("cannot specify both --postgres-url and individual postgres connection flags " +
12791284
"(--postgres-host, --postgres-username, etc), please specify only one connection method")
12801285
}
12811286

1287+
if !hasIndividualFlags {
1288+
return false, nil
1289+
}
1290+
12821291
// Check for incomplete individual flag configuration
12831292
if vals.PostgresHost != "" || vals.PostgresUsername != "" || vals.PostgresPassword != "" || vals.PostgresDatabase != "" {
12841293
missingFlags := []string{}
@@ -1296,15 +1305,15 @@ func validatePostgresFlags(vals *codersdk.DeploymentValues) error {
12961305
}
12971306

12981307
if len(missingFlags) > 0 {
1299-
return xerrors.Errorf("incomplete postgres connection details - when using individual flags, all of the following are required: "+
1308+
return false, xerrors.Errorf("incomplete postgres connection details - when using individual flags, all of the following are required: "+
13001309
"--postgres-host, --postgres-username, --postgres-password, --postgres-database\n"+
13011310
"Missing flags: %s\n"+
13021311
"Alternatively, use --postgres-url to specify the full connection URL",
13031312
strings.Join(missingFlags, ", "))
13041313
}
13051314
}
13061315

1307-
return nil
1316+
return true, nil
13081317
}
13091318

13101319
// templateHelpers builds a set of functions which can be called in templates.

0 commit comments

Comments
 (0)