From 0a7de05892072cb970b8b01de6f846b77ae692a3 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Tue, 15 Nov 2022 18:02:30 -0600 Subject: [PATCH 1/4] Update github.com/jessevdk/go-flags package --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 8bd0bb2..4aff634 100644 --- a/go.mod +++ b/go.mod @@ -33,7 +33,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/ugorji/go/codec v1.2.6 // indirect - golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7 // indirect + golang.org/x/sys v0.2.0 // indirect golang.org/x/text v0.3.7 // indirect google.golang.org/protobuf v1.27.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index 6871565..98211ef 100644 --- a/go.sum +++ b/go.sum @@ -111,6 +111,8 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7 h1:c20P3CcPbopVp2f7099WLOqSNKURf30Z0uq66HpijZY= golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= From fa0efb1597fdf2895f53532a4c220d440e46acd9 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Tue, 22 Nov 2022 14:39:06 -0600 Subject: [PATCH 2/4] Make getCurrentUser method private --- pkg/command/options.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/command/options.go b/pkg/command/options.go index 18d2093..354cad8 100644 --- a/pkg/command/options.go +++ b/pkg/command/options.go @@ -67,7 +67,7 @@ func ParseOptions(args []string) (Options, error) { // Handle edge case where pgweb is started with a default host `localhost` and no user. // When user is not set the `lib/pq` connection will fail and cause pgweb's termination. if (opts.Host == "localhost" || opts.Host == "127.0.0.1") && opts.User == "" { - if username := GetCurrentUser(); username != "" { + if username := getCurrentUser(); username != "" { opts.User = username } else { opts.Host = "" @@ -131,8 +131,8 @@ func SetDefaultOptions() error { return nil } -// GetCurrentUser returns a current user name -func GetCurrentUser() string { +// getCurrentUser returns a current user name +func getCurrentUser() string { u, _ := user.Current() if u != nil { return u.Username From b1023cdcfd20fc9918a159a1f561988248f4bb44 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Tue, 22 Nov 2022 14:50:58 -0600 Subject: [PATCH 3/4] Add support for PGWEB_ prefixed env vars --- pkg/command/options.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/pkg/command/options.go b/pkg/command/options.go index 354cad8..c0f164d 100644 --- a/pkg/command/options.go +++ b/pkg/command/options.go @@ -9,6 +9,11 @@ import ( "github.com/jessevdk/go-flags" ) +const ( + // Prefix to use for all pgweb env vars, ie PGWEB_HOST, PGWEB_PORT, etc + envVarPrefix = "PGWEB_" +) + type Options struct { Version bool `short:"v" long:"version" description:"Print version"` Debug bool `short:"d" long:"debug" description:"Enable debugging mode"` @@ -57,11 +62,11 @@ func ParseOptions(args []string) (Options, error) { } if opts.URL == "" { - opts.URL = os.Getenv("DATABASE_URL") + opts.URL = getPrefixedEnvVar("DATABASE_URL") } if opts.Prefix == "" { - opts.Prefix = os.Getenv("URL_PREFIX") + opts.Prefix = getPrefixedEnvVar("URL_PREFIX") } // Handle edge case where pgweb is started with a default host `localhost` and no user. @@ -74,11 +79,11 @@ func ParseOptions(args []string) (Options, error) { } } - if os.Getenv("SESSIONS") != "" { + if getPrefixedEnvVar("SESSIONS") != "" { opts.Sessions = true } - if os.Getenv("LOCK_SESSION") != "" { + if getPrefixedEnvVar("LOCK_SESSION") != "" { opts.LockSession = true opts.Sessions = false } @@ -97,12 +102,12 @@ func ParseOptions(args []string) (Options, error) { opts.Prefix = opts.Prefix + "/" } - if opts.AuthUser == "" && os.Getenv("AUTH_USER") != "" { - opts.AuthUser = os.Getenv("AUTH_USER") + if opts.AuthUser == "" { + opts.AuthUser = getPrefixedEnvVar("AUTH_USER") } - if opts.AuthPass == "" && os.Getenv("AUTH_PASS") != "" { - opts.AuthPass = os.Getenv("AUTH_PASS") + if opts.AuthPass == "" { + opts.AuthPass = getPrefixedEnvVar("AUTH_PASS") } if opts.ConnectBackend != "" { @@ -139,3 +144,12 @@ func getCurrentUser() string { } return os.Getenv("USER") } + +// getPrefixedEnvVar returns env var with prefix, or falls back to unprefixed one +func getPrefixedEnvVar(name string) string { + val := os.Getenv(envVarPrefix + name) + if val == "" { + val = os.Getenv(name) + } + return val +} From a2a5415acef41715cb0e67dd9f13c3332cbc0a6e Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Tue, 22 Nov 2022 15:20:49 -0600 Subject: [PATCH 4/4] Print deprecation notice when using unprefixed env var --- pkg/command/options.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/command/options.go b/pkg/command/options.go index c0f164d..391b91b 100644 --- a/pkg/command/options.go +++ b/pkg/command/options.go @@ -2,6 +2,7 @@ package command import ( "errors" + "fmt" "os" "os/user" "strings" @@ -150,6 +151,9 @@ func getPrefixedEnvVar(name string) string { val := os.Getenv(envVarPrefix + name) if val == "" { val = os.Getenv(name) + if val != "" { + fmt.Printf("[DEPRECATION] Usage of %s env var is deprecated, please use PGWEB_%s variable instead\n", name, name) + } } return val }