/
config.go
84 lines (77 loc) · 1.99 KB
/
config.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
package aspect
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
)
// DatabaseConfig contains the fields needed to connect to a database.
type DatabaseConfig struct {
Driver string `json:"driver"`
Host string `json:"host"`
Port int64 `json:"port"`
Name string `json:"name"`
User string `json:"user"`
Password string `json:"password"`
SSLMode string `json:"ssl_mode"`
}
// Credentials with return a string of credentials appropriate for Go's
// sql.Open function
func (db DatabaseConfig) Credentials() string {
// Only add the key if there is a value
var values []string
if db.Host != "" {
values = append(values, fmt.Sprintf("host=%s", db.Host))
}
if db.Port != 0 {
values = append(values, fmt.Sprintf("port=%d", db.Port))
}
if db.Name != "" {
values = append(values, fmt.Sprintf("dbname=%s", db.Name))
}
if db.User != "" {
values = append(values, fmt.Sprintf("user=%s", db.User))
}
if db.Password != "" {
values = append(values, fmt.Sprintf("password=%s", db.Password))
}
if db.SSLMode != "" {
values = append(values, fmt.Sprintf("sslmode=%s", db.SSLMode))
}
return strings.Join(values, " ")
}
var travisCI = DatabaseConfig{
Driver: "postgres",
Host: "localhost",
Port: 5432,
Name: "travis_ci_test",
User: "postgres",
SSLMode: "disable",
}
// Parse will create a DatabaseConfig using the file at the given path.
func ParseConfig(filename string) (DatabaseConfig, error) {
f, err := os.Open(filename)
if err != nil {
return DatabaseConfig{}, err
}
return parseConfig(f)
}
func parseConfig(f io.Reader) (c DatabaseConfig, err error) {
b, err := ioutil.ReadAll(f)
if err != nil {
return
}
err = json.Unmarshal(b, &c)
return
}
// ParseTestConfig varies from the default ParseConfig by defaulting to the
// Travis CI credentials if the given config returned nothing.
func ParseTestConfig(filename string) (DatabaseConfig, error) {
f, err := os.Open(filename)
if err != nil {
return travisCI, nil
}
return parseConfig(f)
}