Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6dfd6cb

Browse files
committed
fix(cli): rename url to dbURL to avoid import shadowing
1 parent7cff673 commit6dfd6cb

File tree

4 files changed

+77
-62
lines changed

4 files changed

+77
-62
lines changed

‎cli/resetpassword.go‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"cdr.dev/slog"
1111
"cdr.dev/slog/sloggers/sloghuman"
12-
"github.com/coder/coder/v2/coderd/database/awsiamrds"
1312
"github.com/coder/coder/v2/codersdk"
1413
"github.com/coder/pretty"
1514
"github.com/coder/serpent"
@@ -38,25 +37,13 @@ func (*RootCmd) resetPassword() *serpent.Command {
3837
logger=logger.Leveled(slog.LevelDebug)
3938
}
4039

41-
// Read the postgres URL from a file, if specified.
42-
ifpostgresURLFile!="" {
43-
ifpostgresURL!="" {
44-
returnxerrors.Errorf("cannot specify both --postgres-url and --postgres-url-file")
45-
}
46-
varerrerror
47-
postgresURL,err=ReadPostgresURLFromFile(postgresURLFile)
48-
iferr!=nil {
49-
returnerr
50-
}
51-
}
52-
53-
sqlDriver:="postgres"
54-
ifcodersdk.PostgresAuth(postgresAuth)==codersdk.PostgresAuthAWSIAMRDS {
55-
varerrerror
56-
sqlDriver,err=awsiamrds.Register(inv.Context(),sqlDriver)
57-
iferr!=nil {
58-
returnxerrors.Errorf("register aws rds iam auth: %w",err)
59-
}
40+
sqlDriver,postgresURL,err:=ResolvePostgresParams(inv.Context(),PostgresParams{
41+
URL:postgresURL,
42+
URLFile:postgresURLFile,
43+
Auth:postgresAuth,
44+
},"postgres")
45+
iferr!=nil {
46+
returnerr
6047
}
6148

6249
sqlDB,err:=ConnectToPostgres(inv.Context(),logger,sqlDriver,postgresURL,nil)

‎cli/server.go‎

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import (
7676
"github.com/coder/coder/v2/coderd/database/dbmetrics"
7777
"github.com/coder/coder/v2/coderd/database/dbpurge"
7878
"github.com/coder/coder/v2/coderd/database/migrations"
79+
"github.com/coder/coder/v2/coderd/database/pgfileurl"
7980
"github.com/coder/coder/v2/coderd/database/pubsub"
8081
"github.com/coder/coder/v2/coderd/devtunnel"
8182
"github.com/coder/coder/v2/coderd/entitlements"
@@ -433,18 +434,29 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
433434
}
434435
config:=r.createConfig()
435436

436-
// If a postgres URL file is specified, read the URL from the file.
437+
// If a postgres URL file is specified, register a driver that
438+
// re-reads the file on each connection attempt. This supports
439+
// credential rotation without restarting the server.
437440
ifvals.PostgresURLFile!="" {
438441
ifvals.PostgresURL!="" {
439-
returnxerrors.Errorf("cannot specify both --postgres-url and --postgres-url-file")
442+
returnxerrors.Errorf("cannot specify both --postgres-url and --postgres-url-file flags")
440443
}
444+
// Read the URL once for initial validation and migrations.
445+
logger.Debug(ctx,"database connection URL sourced from file")
441446
postgresURL,err:=ReadPostgresURLFromFile(vals.PostgresURLFile.String())
442447
iferr!=nil {
443448
returnerr
444449
}
445450
iferr:=vals.PostgresURL.Set(postgresURL);err!=nil {
446-
returnxerrors.Errorf("setpostgres URL from file: %w",err)
451+
returnxerrors.Errorf("setdatabase URL from file: %w",err)
447452
}
453+
// Register a driver that re-reads the file on each new connection.
454+
sqlDriver,err=pgfileurl.Register(sqlDriver,vals.PostgresURLFile.String(),logger)
455+
iferr!=nil {
456+
returnxerrors.Errorf("register database file URL driver: %w",err)
457+
}
458+
}elseifvals.PostgresURL!="" {
459+
logger.Debug(ctx,"database connection URL sourced from environment variable")
448460
}
449461

450462
builtinPostgres:=false
@@ -2845,6 +2857,47 @@ func ReadPostgresURLFromFile(filePath string) (string, error) {
28452857
returnstrings.TrimSpace(string(content)),nil
28462858
}
28472859

2860+
// PostgresParams holds parameters for resolving a postgres connection.
2861+
typePostgresParamsstruct {
2862+
// URL is the direct connection URL (from --postgres-url or CODER_PG_CONNECTION_URL).
2863+
URLstring
2864+
// URLFile is the path to a file containing the connection URL
2865+
// (from --postgres-url-file or CODER_PG_CONNECTION_URL_FILE).
2866+
URLFilestring
2867+
// Auth is the authentication method (e.g., "password" or "awsiamrds").
2868+
Authstring
2869+
}
2870+
2871+
// ResolvePostgresParams resolves the postgres connection URL and SQL driver.
2872+
// It handles mutual exclusion between URL and URLFile, reads the URL from file
2873+
// if specified, and registers the appropriate SQL driver based on the auth type.
2874+
// Returns the driver name and the resolved URL.
2875+
funcResolvePostgresParams(ctx context.Context,paramsPostgresParams,baseDriverstring) (driverstring,dbURLstring,errerror) {
2876+
dbURL=params.URL
2877+
2878+
// Handle mutual exclusion and file reading.
2879+
ifparams.URLFile!="" {
2880+
ifparams.URL!="" {
2881+
return"","",xerrors.Errorf("cannot specify both --postgres-url and --postgres-url-file")
2882+
}
2883+
dbURL,err=ReadPostgresURLFromFile(params.URLFile)
2884+
iferr!=nil {
2885+
return"","",err
2886+
}
2887+
}
2888+
2889+
// Register the appropriate driver.
2890+
driver=baseDriver
2891+
ifcodersdk.PostgresAuth(params.Auth)==codersdk.PostgresAuthAWSIAMRDS {
2892+
driver,err=awsiamrds.Register(ctx,driver)
2893+
iferr!=nil {
2894+
return"","",xerrors.Errorf("register aws rds iam auth: %w",err)
2895+
}
2896+
}
2897+
2898+
returndriver,dbURL,nil
2899+
}
2900+
28482901
funcgetAndMigratePostgresDB(ctx context.Context,logger slog.Logger,postgresURLstring,auth codersdk.PostgresAuth,sqlDriverstring) (*sql.DB,string,error) {
28492902
dbURL,err:=escapePostgresURLUserInfo(postgresURL)
28502903
iferr!=nil {

‎cli/server_createadminuser.go‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"cdr.dev/slog/sloggers/sloghuman"
1414
"github.com/coder/coder/v2/cli/cliui"
1515
"github.com/coder/coder/v2/coderd/database"
16-
"github.com/coder/coder/v2/coderd/database/awsiamrds"
1716
"github.com/coder/coder/v2/coderd/database/dbtime"
1817
"github.com/coder/coder/v2/coderd/gitsshkey"
1918
"github.com/coder/coder/v2/coderd/httpapi"
@@ -53,16 +52,13 @@ func (r *RootCmd) newCreateAdminUserCommand() *serpent.Command {
5352
ctx,cancel:=inv.SignalNotifyContext(ctx,StopSignals...)
5453
defercancel()
5554

56-
// Read the postgres URL from a file, if specified.
57-
ifnewUserDBURLFile!="" {
58-
ifnewUserDBURL!="" {
59-
returnxerrors.Errorf("cannot specify both --postgres-url and --postgres-url-file")
60-
}
61-
varerrerror
62-
newUserDBURL,err=ReadPostgresURLFromFile(newUserDBURLFile)
63-
iferr!=nil {
64-
returnerr
65-
}
55+
sqlDriver,newUserDBURL,err:=ResolvePostgresParams(ctx,PostgresParams{
56+
URL:newUserDBURL,
57+
URLFile:newUserDBURLFile,
58+
Auth:newUserPgAuth,
59+
},"postgres")
60+
iferr!=nil {
61+
returnerr
6662
}
6763

6864
ifnewUserDBURL=="" {
@@ -77,14 +73,6 @@ func (r *RootCmd) newCreateAdminUserCommand() *serpent.Command {
7773
newUserDBURL=url
7874
}
7975

80-
sqlDriver:="postgres"
81-
ifcodersdk.PostgresAuth(newUserPgAuth)==codersdk.PostgresAuthAWSIAMRDS {
82-
sqlDriver,err=awsiamrds.Register(inv.Context(),sqlDriver)
83-
iferr!=nil {
84-
returnxerrors.Errorf("register aws rds iam auth: %w",err)
85-
}
86-
}
87-
8876
sqlDB,err:=ConnectToPostgres(ctx,logger,sqlDriver,newUserDBURL,nil)
8977
iferr!=nil {
9078
returnxerrors.Errorf("connect to postgres: %w",err)

‎cli/server_regenerate_vapid_keypair.go‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/coder/coder/v2/cli/cliui"
1414
"github.com/coder/coder/v2/coderd/database"
15-
"github.com/coder/coder/v2/coderd/database/awsiamrds"
1615
"github.com/coder/coder/v2/coderd/webpush"
1716
"github.com/coder/coder/v2/codersdk"
1817
"github.com/coder/serpent"
@@ -40,16 +39,13 @@ func (r *RootCmd) newRegenerateVapidKeypairCommand() *serpent.Command {
4039

4140
defercancel()
4241

43-
// Read the postgres URL from a file, if specified.
44-
ifregenVapidKeypairDBURLFile!="" {
45-
ifregenVapidKeypairDBURL!="" {
46-
returnxerrors.Errorf("cannot specify both --postgres-url and --postgres-url-file")
47-
}
48-
varerrerror
49-
regenVapidKeypairDBURL,err=ReadPostgresURLFromFile(regenVapidKeypairDBURLFile)
50-
iferr!=nil {
51-
returnerr
52-
}
42+
sqlDriver,regenVapidKeypairDBURL,err:=ResolvePostgresParams(ctx,PostgresParams{
43+
URL:regenVapidKeypairDBURL,
44+
URLFile:regenVapidKeypairDBURLFile,
45+
Auth:regenVapidKeypairPgAuth,
46+
},"postgres")
47+
iferr!=nil {
48+
returnerr
5349
}
5450

5551
ifregenVapidKeypairDBURL=="" {
@@ -64,15 +60,6 @@ func (r *RootCmd) newRegenerateVapidKeypairCommand() *serpent.Command {
6460
regenVapidKeypairDBURL=url
6561
}
6662

67-
sqlDriver:="postgres"
68-
varerrerror
69-
ifcodersdk.PostgresAuth(regenVapidKeypairPgAuth)==codersdk.PostgresAuthAWSIAMRDS {
70-
sqlDriver,err=awsiamrds.Register(inv.Context(),sqlDriver)
71-
iferr!=nil {
72-
returnxerrors.Errorf("register aws rds iam auth: %w",err)
73-
}
74-
}
75-
7663
sqlDB,err:=ConnectToPostgres(ctx,logger,sqlDriver,regenVapidKeypairDBURL,nil)
7764
iferr!=nil {
7865
returnxerrors.Errorf("connect to postgres: %w",err)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp