Connect as OS user if no user specified
This commit is contained in:
+13
-3
@@ -14,6 +14,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"os/user"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,7 +24,7 @@ type ConnectionParameters struct {
|
|||||||
Host string // url (e.g. localhost)
|
Host string // url (e.g. localhost)
|
||||||
Port uint16 // default: 5432
|
Port uint16 // default: 5432
|
||||||
Database string
|
Database string
|
||||||
User string
|
User string // default: OS user name
|
||||||
Password string
|
Password string
|
||||||
MsgBufSize int // Size of work buffer used for transcoding messages. For optimal performance, it should be large enough to store a single row from any result set. Default: 1024
|
MsgBufSize int // Size of work buffer used for transcoding messages. For optimal performance, it should be large enough to store a single row from any result set. Default: 1024
|
||||||
TLSConfig *tls.Config // config for TLS connection -- nil disables TLS
|
TLSConfig *tls.Config // config for TLS connection -- nil disables TLS
|
||||||
@@ -90,8 +91,8 @@ func (e ProtocolError) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connect establishes a connection with a PostgreSQL server using parameters. One
|
// Connect establishes a connection with a PostgreSQL server using parameters. One
|
||||||
// of parameters.Socket or parameters.Host must be specified. parameters.User must
|
// of parameters.Socket or parameters.Host must be specified. parameters.User
|
||||||
// also the included. Other parameters fields are optional.
|
// will default to the OS user name. Other parameters fields are optional.
|
||||||
func Connect(parameters ConnectionParameters) (c *Connection, err error) {
|
func Connect(parameters ConnectionParameters) (c *Connection, err error) {
|
||||||
c = new(Connection)
|
c = new(Connection)
|
||||||
|
|
||||||
@@ -102,6 +103,15 @@ func Connect(parameters ConnectionParameters) (c *Connection, err error) {
|
|||||||
c.logger = nullLogger("null")
|
c.logger = nullLogger("null")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.parameters.User == "" {
|
||||||
|
user, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c.logger.Debug("Using default User " + user.Username)
|
||||||
|
c.parameters.User = user.Username
|
||||||
|
}
|
||||||
|
|
||||||
if c.parameters.Port == 0 {
|
if c.parameters.Port == 0 {
|
||||||
c.logger.Debug("Using default Port")
|
c.logger.Debug("Using default Port")
|
||||||
c.parameters.Port = 5432
|
c.parameters.Port = 5432
|
||||||
|
|||||||
Reference in New Issue
Block a user