Skip to content

Commit

Permalink
Fallback to USER env var if user.Current failed
Browse files Browse the repository at this point in the history
Also expose the error emitted by lookup failures.
  • Loading branch information
sosedoff authored and markottt committed Jan 9, 2015
1 parent c0cbb32 commit 76f113b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
ErrInFailedTransaction = errors.New("pq: Could not complete operation in a failed transaction")
ErrSSLNotSupported = errors.New("pq: SSL is not enabled on the server")
ErrSSLKeyHasWorldPermissions = errors.New("pq: Private key file has group or world access. Permissions should be u=rw (0600) or less.")
ErrCouldNotDetectUsername = errors.New("pq: Could not detect default username. Please provide one explicitly.")
)

type drv struct{}
Expand Down
17 changes: 13 additions & 4 deletions user_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@

package pq

import "os/user"
import (
"os"
"os/user"
)

func userCurrent() (string, error) {
u, err := user.Current()
if err != nil {
return "", err
if err == nil {
return u.Username, nil
}
return u.Username, nil

name := os.Getenv("USER")
if name != "" {
return name, nil
}

return "", ErrCouldNotDetectUsername
}
2 changes: 1 addition & 1 deletion user_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func userCurrent() (string, error) {
pwname_size := uint32(len(pw_name)) - 1
err := syscall.GetUserNameEx(syscall.NameSamCompatible, &pw_name[0], &pwname_size)
if err != nil {
return "", err
return "", ErrCouldNotDetectUsername
}
s := syscall.UTF16ToString(pw_name)
u := filepath.Base(s)
Expand Down

0 comments on commit 76f113b

Please sign in to comment.