From f6900db40a3742bc195b4d85f0e85ccaa236e94d Mon Sep 17 00:00:00 2001 From: Simon Zimmermann Date: Mon, 7 May 2012 12:40:01 +0200 Subject: [PATCH] add password and db to client --- exp/connection.go | 36 +++++++++++++++++++++++++++++++++--- exp/godis.go | 20 +++++++++++--------- schema/key.go | 2 +- schema/tag.go | 1 - 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/exp/connection.go b/exp/connection.go index 56d3dbe..e2346cb 100644 --- a/exp/connection.go +++ b/exp/connection.go @@ -31,15 +31,45 @@ type Conn struct { // NewConn then returns a Conn struct which implements the Connection // interface. It's easy to use this interface to create your own // redis client or to simply talk to the redis database. -func NewConn(addr, proto string) (*Conn, error) { - c, err := net.Dial(proto, addr) +func NewConn(addr, proto string, db int, password string) (*Conn, error) { + conn, err := net.Dial(proto, addr) if err != nil { return nil, err } ConnSum++ - return &Conn{bufin.NewReader(c), c}, nil + c := &Conn{bufin.NewReader(conn), conn} + + if password != "" { + e := c.Write("AUTH", password) + + if e != nil { + return nil, e + } + + _, e = c.Read() + + if e != nil { + return nil, e + } + } + + if db != 0 { + e := c.Write("SELECT", db) + + if e != nil { + return nil, e + } + + _, e = c.Read() + + if e != nil { + return nil, e + } + } + + return c, nil } // Read reads one reply of the socket connection. If there is no reply waiting diff --git a/exp/godis.go b/exp/godis.go index c3e06b0..081f8c3 100644 --- a/exp/godis.go +++ b/exp/godis.go @@ -65,20 +65,22 @@ import ( // in a pool. The size of the pool can be adjusted with by setting the // MaxConnections variable before creating a client. type Client struct { - Addr string - Proto string - pool *connPool + Addr string + Proto string + Db int + Password string + pool *connPool } // NewClient expects a addr like "tcp:127.0.0.1:6379" // It returns a new *Client. -func NewClient(addr string) *Client { +func NewClient(addr string, db int, password string) *Client { if addr == "" { addr = "tcp:127.0.0.1:6379" } na := strings.SplitN(addr, ":", 2) - return &Client{Addr: na[1], Proto: na[0], pool: newConnPool()} + return &Client{na[1], na[0], db, password, newConnPool()} } // Call is the canonical way of talking to Redis. It accepts any @@ -106,7 +108,7 @@ func (c *Client) connect() (conn Connection, err error) { conn = c.pool.pop() if conn == nil { - conn, err = NewConn(c.Addr, c.Proto) + conn, err = NewConn(c.Addr, c.Proto, c.Db, c.Password) if err != nil { return nil, err @@ -133,9 +135,9 @@ type AsyncClient struct { // NewAsyncClient expects a addr like "tcp:127.0.0.1:6379" // It returns a new *Client. -func NewAsyncClient(addr string) *AsyncClient { +func NewAsyncClient(addr string, db int, password string) *AsyncClient { return &AsyncClient{ - NewClient(addr), + NewClient(addr, db, password), bytes.NewBuffer(make([]byte, 0, 1024*16)), nil, 0, @@ -158,7 +160,7 @@ func (ac *AsyncClient) Call(args ...interface{}) (err error) { // Read returns a Reply or error. func (ac *AsyncClient) Read() (*Reply, error) { if ac.conn == nil { - conn, e := NewConn(ac.Addr, ac.Proto) + conn, e := NewConn(ac.Addr, ac.Proto, ac.Db, ac.Password) if e != nil { return nil, e diff --git a/schema/key.go b/schema/key.go index 1d00dcd..e57e61a 100644 --- a/schema/key.go +++ b/schema/key.go @@ -1,4 +1,4 @@ -package schema +package schema import ( "fmt" diff --git a/schema/tag.go b/schema/tag.go index c3de639..bc4e04e 100644 --- a/schema/tag.go +++ b/schema/tag.go @@ -38,4 +38,3 @@ func (o tagOptions) Contains(optionName string) bool { } return false } -