-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
105 lines (89 loc) · 2.52 KB
/
conf.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
package main
import (
"flag"
"fmt"
"os"
"strings"
"./db"
)
// ConfigurationError - custom error for configuration
type ConfigurationError struct {
errors []error
}
func (c *ConfigurationError) Error() string {
messages := []string{}
for _, err := range c.errors {
messages = append(messages, err.Error())
}
return strings.Join(messages, "\n")
}
// declare command-line flags to set the database configuration
var (
dbHost = flag.String("h", "", "Host")
dbPort = flag.String("p", "", "Port")
dbName = flag.String("db", "", "Database")
dbUser = flag.String("u", "", "Login Name")
dbPwd = flag.String("pwd", "", "Password")
dbSSL = flag.String("ssl", "", "SSL Mode (default: require)")
)
// read configuration for PostgreSQL database
func readConfiguration() (*db.Settings, error) {
// parse the provided command-line flags
flag.Parse()
errors := []error{}
// host
host, found := getOption(dbHost, "DB_HOST")
if !found {
errors = append(errors, fmt.Errorf("Please specify host by using commandline flag -h or set it as environment variable DB_HOST"))
}
// port
port, found := getOption(dbPort, "DB_PORT")
if !found {
errors = append(errors, fmt.Errorf("Please specify port by using commandline flag -p or set it as environment variable DB_PORT"))
}
// database name
database, found := getOption(dbName, "DB_NAME")
if !found {
errors = append(errors, fmt.Errorf("Please specify database name by using commandline flag -db or set it as environment variable DB_NAME"))
}
// user
user, found := getOption(dbUser, "DB_USER")
if !found {
errors = append(errors, fmt.Errorf("Please specify user by using commandline flag -u or set it as environment variable DB_USER"))
}
// password
pwd, found := getOption(dbPwd, "DB_PWD")
if !found {
errors = append(errors, fmt.Errorf("Please specify password by using commandline flag -pwd or set it as environment variable DB_PWD"))
}
// ssl
ssl, found := getOption(dbSSL, "DB_SSL")
if !found {
// set default ssl mode to require
ssl = "require"
}
// configuration is not successful
if len(errors) > 0 {
return nil, &ConfigurationError{errors}
}
return &db.Settings{
Host: host,
Port: port,
DBName: database,
User: user,
Pwd: pwd,
SSL: ssl,
}, nil
}
// Get option
// If command-line flag is not set the fallback
// is to check for an environment variable
func getOption(flag *string, env string) (string, bool) {
if *flag != "" {
return *flag, true
}
if value := os.Getenv(env); value != "" {
return value, true
}
return "", false
}