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

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