Skip to content

Commit 76f113b

Browse files
sosedoffmarkottt
authored andcommitted
Fallback to USER env var if user.Current failed
Also expose the error emitted by lookup failures.
1 parent c0cbb32 commit 76f113b

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

conn.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var (
3030
ErrInFailedTransaction = errors.New("pq: Could not complete operation in a failed transaction")
3131
ErrSSLNotSupported = errors.New("pq: SSL is not enabled on the server")
3232
ErrSSLKeyHasWorldPermissions = errors.New("pq: Private key file has group or world access. Permissions should be u=rw (0600) or less.")
33+
ErrCouldNotDetectUsername = errors.New("pq: Could not detect default username. Please provide one explicitly.")
3334
)
3435

3536
type drv struct{}

user_posix.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44

55
package pq
66

7-
import "os/user"
7+
import (
8+
"os"
9+
"os/user"
10+
)
811

912
func userCurrent() (string, error) {
1013
u, err := user.Current()
11-
if err != nil {
12-
return "", err
14+
if err == nil {
15+
return u.Username, nil
1316
}
14-
return u.Username, nil
17+
18+
name := os.Getenv("USER")
19+
if name != "" {
20+
return name, nil
21+
}
22+
23+
return "", ErrCouldNotDetectUsername
1524
}

user_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func userCurrent() (string, error) {
1919
pwname_size := uint32(len(pw_name)) - 1
2020
err := syscall.GetUserNameEx(syscall.NameSamCompatible, &pw_name[0], &pwname_size)
2121
if err != nil {
22-
return "", err
22+
return "", ErrCouldNotDetectUsername
2323
}
2424
s := syscall.UTF16ToString(pw_name)
2525
u := filepath.Base(s)

0 commit comments

Comments
 (0)