Fix error when automatic user detection fails

This commit is contained in:
Dan Sosedoff 2019-01-28 14:03:45 -06:00
parent e6f6dbbddd
commit d48172986e
3 changed files with 32 additions and 8 deletions

View File

@ -133,7 +133,7 @@ func Connect(c *gin.Context) {
} }
opts := command.Options{Url: url} opts := command.Options{Url: url}
url, err := connection.FormatUrl(opts) url, err := connection.FormatURL(opts)
if err != nil { if err != nil {
badRequest(c, err) badRequest(c, err)

View File

@ -3,6 +3,7 @@ package command
import ( import (
"errors" "errors"
"os" "os"
"os/user"
"strings" "strings"
"github.com/jessevdk/go-flags" "github.com/jessevdk/go-flags"
@ -58,6 +59,14 @@ func ParseOptions(args []string) (Options, error) {
opts.Prefix = os.Getenv("URL_PREFIX") opts.Prefix = os.Getenv("URL_PREFIX")
} }
if (opts.Host == "localhost" || opts.Host == "127.0.0.1") && opts.User == "" {
if username := GetCurrentUser(); username != "" {
opts.User = username
} else {
opts.Host = ""
}
}
if os.Getenv("SESSIONS") != "" { if os.Getenv("SESSIONS") != "" {
opts.Sessions = true opts.Sessions = true
} }
@ -105,6 +114,7 @@ func ParseOptions(args []string) (Options, error) {
return opts, nil return opts, nil
} }
// SetDefaultOptions parses and assigns the options
func SetDefaultOptions() error { func SetDefaultOptions() error {
opts, err := ParseOptions([]string{}) opts, err := ParseOptions([]string{})
if err != nil { if err != nil {
@ -113,3 +123,12 @@ func SetDefaultOptions() error {
Opts = opts Opts = opts
return nil return nil
} }
// GetCurrentUser returns a current user name
func GetCurrentUser() string {
u, _ := user.Current()
if u != nil {
return u.Username
}
return os.Getenv("USER")
}

View File

@ -11,10 +11,13 @@ import (
"github.com/sosedoff/pgweb/pkg/command" "github.com/sosedoff/pgweb/pkg/command"
) )
// Common errors
var ( var (
formatError = errors.New("Invalid URL. Valid format: postgres://user:password@host:port/db?sslmode=mode") errCantDetectUser = errors.New("Could not detect default username")
errInvalidURLFormat = errors.New("Invalid URL. Valid format: postgres://user:password@host:port/db?sslmode=mode")
) )
// currentUser returns a current user name
func currentUser() (string, error) { func currentUser() (string, error) {
u, err := user.Current() u, err := user.Current()
if err == nil { if err == nil {
@ -26,7 +29,7 @@ func currentUser() (string, error) {
return name, nil return name, nil
} }
return "", errors.New("Unable to detect OS user") return "", errCantDetectUser
} }
// Check if connection url has a correct postgres prefix // Check if connection url has a correct postgres prefix
@ -43,18 +46,19 @@ func valsFromQuery(vals neturl.Values) map[string]string {
return result return result
} }
func FormatUrl(opts command.Options) (string, error) { // FormatURL reformats the existing connection string
func FormatURL(opts command.Options) (string, error) {
url := opts.Url url := opts.Url
// Validate connection string prefix // Validate connection string prefix
if !hasValidPrefix(url) { if !hasValidPrefix(url) {
return "", formatError return "", errInvalidURLFormat
} }
// Validate the URL // Validate the URL
uri, err := neturl.Parse(url) uri, err := neturl.Parse(url)
if err != nil { if err != nil {
return "", formatError return "", errInvalidURLFormat
} }
// Get query params // Get query params
@ -82,15 +86,16 @@ func FormatUrl(opts command.Options) (string, error) {
return uri.String(), nil return uri.String(), nil
} }
// IsBlank returns true if command options do not contain connection details
func IsBlank(opts command.Options) bool { func IsBlank(opts command.Options) bool {
return opts.Host == "" && opts.User == "" && opts.DbName == "" && opts.Url == "" return opts.Host == "" && opts.User == "" && opts.DbName == "" && opts.Url == ""
} }
// Build a new database connection string for the CLI options // BuildStringFromOptions returns a new connection string built from options
func BuildStringFromOptions(opts command.Options) (string, error) { func BuildStringFromOptions(opts command.Options) (string, error) {
// If connection string is provided we just use that // If connection string is provided we just use that
if opts.Url != "" { if opts.Url != "" {
return FormatUrl(opts) return FormatURL(opts)
} }
// Try to detect user from current OS user // Try to detect user from current OS user