Fall back to USER env var when detecting OS user

This commit is contained in:
Dan Sosedoff 2015-01-09 22:55:00 -06:00
parent c6793cc454
commit 1ba7bee5bf

View File

@ -3,10 +3,25 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"os/user" "os/user"
"strings" "strings"
) )
func currentUser() (string, error) {
u, err := user.Current()
if err == nil {
return u.Username, nil
}
name := os.Getenv("USER")
if name != "" {
return name, nil
}
return "", errors.New("Unable to detect OS user")
}
func formatConnectionUrl(opts Options) (string, error) { func formatConnectionUrl(opts Options) (string, error) {
url := opts.Url url := opts.Url
@ -45,12 +60,10 @@ func buildConnectionString(opts Options) (string, error) {
} }
// Try to detect user from current OS user // Try to detect user from current OS user
// TODO: remove os/user dependency
if opts.User == "" { if opts.User == "" {
user, err := user.Current() u, err := currentUser()
if err == nil { if err == nil {
opts.User = user.Username opts.User = u
} }
} }