forked from tg/pgpass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassword.go
64 lines (57 loc) · 1.46 KB
/
password.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package pgpass
import (
"io"
"net/url"
"strings"
)
// PasswordFrom reads password for given host and user from r, which should
// be in a valid pgpass format. Host should be of the form "hostname:port".
func PasswordFrom(host, user string, r io.Reader) (string, error) {
hp := strings.SplitN(host, ":", 2) // split to hostname:port
if len(hp) == 1 {
// Add default postgresql port
hp = append(hp, "5432")
}
er := NewEntryReader(r)
for er.Next() {
e := er.Entry()
if eq(hp[0], e.Hostname) && eq(hp[1], e.Port) && eq(user, e.Username) {
return e.Password, nil
}
}
return "", er.Err()
}
func eq(s, pattern string) bool {
return pattern == "*" || s == pattern
}
// Password reads password for given host and user from a default pgpass file.
// Host should be of the form "hostname:port".
func Password(host, user string) (string, error) {
f, err := OpenDefault()
if err != nil {
return "", err
}
defer f.Close()
return PasswordFrom(host, user, f)
}
// UpdateURL injects password into URL if not already provided.
// Password will be loaded from the default pgpass file.
func UpdateURL(dburl string) (string, error) {
u, err := url.Parse(dburl)
if err != nil {
return "", err
}
if user := u.User; user != nil {
if _, ok := user.Password(); !ok {
uname := user.Username()
pass, err := Password(u.Host, uname)
if err != nil {
return "", err
}
if pass != "" {
u.User = url.UserPassword(uname, pass)
}
}
}
return u.String(), nil
}