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= diff --git a/pkg/command/options.go b/pkg/command/options.go index 18d2093..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" @@ -9,6 +10,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,28 +63,28 @@ 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. // 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 = "" } } - 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 +103,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 != "" { @@ -131,11 +137,23 @@ 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 } 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) + if val != "" { + fmt.Printf("[DEPRECATION] Usage of %s env var is deprecated, please use PGWEB_%s variable instead\n", name, name) + } + } + return val +}