Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazier default username #104

Merged
merged 2 commits into from
May 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ code still exists in here.
* Brad Fitzpatrick (bradfitz)
* Daniel Farina (fdr)
* Everyone at The Go Team
* Ewan Chou (coocood)
* Federico Romero (federomero)
* Gary Burd (garyburd)
* Heroku (heroku)
Expand Down
22 changes: 12 additions & 10 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,24 @@ func Open(name string) (_ driver.Conn, err error) {
o.Set("host", "localhost")
o.Set("port", "5432")

// Default the username, but ignore errors, because a user
// passed in via environment variable or connection string
// would be okay. This can result in connections failing
// *sometimes* if the client relies on being able to determine
// the current username and there are intermittent problems.
u, err := user.Current()
if err == nil {
o.Set("user", u.Username)
}

for k, v := range parseEnviron(os.Environ()) {
o.Set(k, v)
}

parseOpts(name, o)

// If a user is not provided by any other means, the last
// resort is to use the current operating system provided user
// name.
if o.Get("user") == "" {
u, err := user.Current()
if err != nil {
return nil, err
} else {
o.Set("user", u.Username)
}
}

c, err := net.Dial(network(o))
if err != nil {
return nil, err
Expand Down