Merge pull request #585 from sosedoff/cli-env-vars-support-with-prefix

Add support for PGWEB_ prefix environment variables
This commit is contained in:
Dan Sosedoff 2022-11-22 15:25:07 -06:00 committed by GitHub
commit ee6898f243
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 12 deletions

2
go.mod
View File

@ -33,7 +33,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ugorji/go/codec v1.2.6 // 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 golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect

2
go.sum
View File

@ -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-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 h1:c20P3CcPbopVp2f7099WLOqSNKURf30Z0uq66HpijZY=
golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 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 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 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= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View File

@ -2,6 +2,7 @@ package command
import ( import (
"errors" "errors"
"fmt"
"os" "os"
"os/user" "os/user"
"strings" "strings"
@ -9,6 +10,11 @@ import (
"github.com/jessevdk/go-flags" "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 { type Options struct {
Version bool `short:"v" long:"version" description:"Print version"` Version bool `short:"v" long:"version" description:"Print version"`
Debug bool `short:"d" long:"debug" description:"Enable debugging mode"` Debug bool `short:"d" long:"debug" description:"Enable debugging mode"`
@ -57,28 +63,28 @@ func ParseOptions(args []string) (Options, error) {
} }
if opts.URL == "" { if opts.URL == "" {
opts.URL = os.Getenv("DATABASE_URL") opts.URL = getPrefixedEnvVar("DATABASE_URL")
} }
if opts.Prefix == "" { 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. // 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. // 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 (opts.Host == "localhost" || opts.Host == "127.0.0.1") && opts.User == "" {
if username := GetCurrentUser(); username != "" { if username := getCurrentUser(); username != "" {
opts.User = username opts.User = username
} else { } else {
opts.Host = "" opts.Host = ""
} }
} }
if os.Getenv("SESSIONS") != "" { if getPrefixedEnvVar("SESSIONS") != "" {
opts.Sessions = true opts.Sessions = true
} }
if os.Getenv("LOCK_SESSION") != "" { if getPrefixedEnvVar("LOCK_SESSION") != "" {
opts.LockSession = true opts.LockSession = true
opts.Sessions = false opts.Sessions = false
} }
@ -97,12 +103,12 @@ func ParseOptions(args []string) (Options, error) {
opts.Prefix = opts.Prefix + "/" opts.Prefix = opts.Prefix + "/"
} }
if opts.AuthUser == "" && os.Getenv("AUTH_USER") != "" { if opts.AuthUser == "" {
opts.AuthUser = os.Getenv("AUTH_USER") opts.AuthUser = getPrefixedEnvVar("AUTH_USER")
} }
if opts.AuthPass == "" && os.Getenv("AUTH_PASS") != "" { if opts.AuthPass == "" {
opts.AuthPass = os.Getenv("AUTH_PASS") opts.AuthPass = getPrefixedEnvVar("AUTH_PASS")
} }
if opts.ConnectBackend != "" { if opts.ConnectBackend != "" {
@ -131,11 +137,23 @@ func SetDefaultOptions() error {
return nil return nil
} }
// GetCurrentUser returns a current user name // getCurrentUser returns a current user name
func GetCurrentUser() string { func getCurrentUser() string {
u, _ := user.Current() u, _ := user.Current()
if u != nil { if u != nil {
return u.Username return u.Username
} }
return os.Getenv("USER") 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
}