@@ -395,11 +395,12 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
395
395
396
396
config := r .createConfig ()
397
397
398
- if err := validatePostgresFlags (vals ); err != nil {
398
+ useIndividualFlags , err := validatePostgresFlags (vals )
399
+ if err != nil {
399
400
return xerrors .Errorf ("validate postgres configuration: %w" , err )
400
401
}
401
402
402
- if vals . PostgresURL == "" && vals . PostgresHost != "" && vals . PostgresUsername != "" && vals . PostgresPassword != "" && vals . PostgresDatabase != "" {
403
+ if useIndividualFlags {
403
404
err = vals .PostgresURL .Set ((& url.URL {
404
405
Scheme : "postgres" ,
405
406
User : url .UserPassword (vals .PostgresUsername .String (), vals .PostgresPassword .String ()),
@@ -1268,17 +1269,25 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
1268
1269
}
1269
1270
1270
1271
// 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 ) {
1275
1277
// 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 " +
1279
1284
"(--postgres-host, --postgres-username, etc), please specify only one connection method" )
1280
1285
}
1281
1286
1287
+ if ! hasIndividualFlags {
1288
+ return false , nil
1289
+ }
1290
+
1282
1291
// Check for incomplete individual flag configuration
1283
1292
if vals .PostgresHost != "" || vals .PostgresUsername != "" || vals .PostgresPassword != "" || vals .PostgresDatabase != "" {
1284
1293
missingFlags := []string {}
@@ -1296,15 +1305,15 @@ func validatePostgresFlags(vals *codersdk.DeploymentValues) error {
1296
1305
}
1297
1306
1298
1307
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: " +
1300
1309
"--postgres-host, --postgres-username, --postgres-password, --postgres-database\n " +
1301
1310
"Missing flags: %s\n " +
1302
1311
"Alternatively, use --postgres-url to specify the full connection URL" ,
1303
1312
strings .Join (missingFlags , ", " ))
1304
1313
}
1305
1314
}
1306
1315
1307
- return nil
1316
+ return true , nil
1308
1317
}
1309
1318
1310
1319
// templateHelpers builds a set of functions which can be called in templates.
0 commit comments