-
Notifications
You must be signed in to change notification settings - Fork 34
/
db_util.go
125 lines (103 loc) · 2.97 KB
/
db_util.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package gonymizer
import (
"database/sql"
"fmt"
"net/url"
"strings"
_ "github.com/lib/pq" // make sure we load the driver
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// PGConfig is the main configuration structure for different PostgreSQL server configurations.
type PGConfig struct {
Username string
Pass string
Host string
DefaultDBName string
SSLMode string
}
// LoadFromCLI will load the PostgreSQL configuration using the function input variables.
func (conf *PGConfig) LoadFromCLI(host, username, password, database string, port int32, disableSSL bool) {
conf.Username = username
conf.DefaultDBName = database
conf.Pass = password
conf.Host = fmt.Sprintf("%s:%d", host, port)
// Set SSL Mode
if disableSSL {
conf.SSLMode = "disable"
} else {
conf.SSLMode = "require"
}
}
// LoadFromEnv uses environment variables to load the PGConfig.
func (conf *PGConfig) LoadFromEnv(debugNum int64, prefix, suffix string) {
conf.Username = viper.GetString(prefix + "USER" + suffix)
conf.Pass = viper.GetString(prefix + "PASS" + suffix)
conf.Host = viper.GetString(prefix + "HOST" + suffix)
port := viper.GetString(prefix + "PORT" + suffix)
conf.DefaultDBName = viper.GetString(prefix + "NAME" + suffix)
conf.SSLMode = viper.GetString(prefix + "SSL" + suffix)
// Set Port
if port != "" {
conf.Host = strings.Join([]string{conf.Host, port}, ":")
}
if conf.Host == "" {
log.Fatal("no database host provided")
}
}
// DSN will construct the data source name from the supplied data in the PGConfig.
// See: https://en.wikipedia.org/wiki/Data_source_name
func (conf *PGConfig) DSN() string {
dsn := conf.queryBaseDsn()
if len(conf.DefaultDBName) > 0 {
dsn += "/" + conf.DefaultDBName
}
if len(conf.SSLMode) > 0 {
if len(conf.DefaultDBName) == 0 {
dsn += "/"
}
dsn += "?sslmode=" + conf.SSLMode
}
return dsn
}
// BaseDSN will return the base of the DSN in string form.
func (conf *PGConfig) BaseDSN() string {
dsn := conf.queryBaseDsn()
if len(conf.SSLMode) > 0 {
dsn += "/?sslmode=" + conf.SSLMode
}
return dsn
}
// queryBaseDSN will return the base DSN constructed from the data in the PGConfig.
func (conf *PGConfig) queryBaseDsn() string {
var u *url.Userinfo
if conf.Pass != "" {
u = url.UserPassword(conf.Username, conf.Pass)
} else if conf.Username != "" {
u = url.User(conf.Username)
}
return (&url.URL{
Scheme: "postgres",
User: u,
Host: conf.Host,
}).String()
}
// URI returns a URI constructed from the supplied PGConfig.
func (conf *PGConfig) URI() string {
return conf.DSN()
}
// BaseURI will return the BaseDSN for the supplied PGConfig.
func (conf *PGConfig) BaseURI() string {
return conf.BaseDSN()
}
// OpenDB will open the database set in the PGConfig and return a pointer to the database connection.
func OpenDB(conf PGConfig) (*sql.DB, error) {
dburl := conf.URI()
db, err := sql.Open("postgres", dburl)
if err != nil {
log.Error(err)
log.Debug("dburl: ", dburl)
return nil, err
}
return db, nil
}