-
Notifications
You must be signed in to change notification settings - Fork 2
/
postgres.go
105 lines (93 loc) · 2.35 KB
/
postgres.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 common
import (
"errors"
"strconv"
"github.com/AlecAivazis/survey/v2"
)
type PostgresConfiguration struct {
InsideDocker bool
Host string
Port int
DBName string
User string
Password string
SSLRequired bool
}
func ConfigurePostgres(defaultDBName string) (PostgresConfiguration, error) {
options := []string{
"Install (new Docker container)",
"Provide your own connection info",
}
prompt := &survey.Select{
Message: "Do you want to install PostgreSQL or provide your own connection info?",
Options: options,
Default: options[0],
}
var postgresOption string
err := survey.AskOne(prompt, &postgresOption)
if err != nil {
return PostgresConfiguration{}, err
}
if postgresOption == options[0] {
return PostgresConfiguration{
InsideDocker: true,
Host: "db",
Port: 5432,
DBName: defaultDBName,
User: "postgres",
Password: GeneratePassword(16),
SSLRequired: false,
}, nil
}
var host, user, password, dbName string
var port int
err = survey.AskOne(&survey.Input{
Message: "PostgreSQL Host",
}, &host, survey.WithValidator(survey.Required))
if err != nil {
return PostgresConfiguration{}, err
}
err = survey.AskOne(&survey.Input{
Message: "PostgreSQL Port",
Default: "5432",
}, &port, survey.WithValidator(survey.Required), survey.WithValidator(func(v interface{}) error {
value, _ := v.(string)
_, err := strconv.Atoi(value)
if err != nil {
return errors.New("not a number")
}
return nil
}))
if err != nil {
return PostgresConfiguration{}, err
}
err = survey.AskOne(&survey.Input{
Message: "PostgreSQL User",
Default: "postgres",
}, &user, survey.WithValidator(survey.Required))
if err != nil {
return PostgresConfiguration{}, err
}
err = survey.AskOne(&survey.Password{
Message: "PostgreSQL Password",
}, &password, survey.WithValidator(survey.Required))
if err != nil {
return PostgresConfiguration{}, err
}
err = survey.AskOne(&survey.Input{
Message: "PostgreSQL Database",
Default: defaultDBName,
}, &dbName, survey.WithValidator(survey.Required))
if err != nil {
return PostgresConfiguration{}, err
}
return PostgresConfiguration{
InsideDocker: false,
Host: host,
Port: port,
User: user,
Password: password,
SSLRequired: true,
DBName: dbName,
}, nil
}