...
1 package embeddedpostgres
2
3 import (
4 "fmt"
5 "io"
6 "os"
7 "time"
8 )
9
10
11 type Config struct {
12 version PostgresVersion
13 port uint32
14 database string
15 username string
16 password string
17 cachePath string
18 runtimePath string
19 dataPath string
20 binariesPath string
21 locale string
22 startParameters map[string]string
23 binaryRepositoryURL string
24 startTimeout time.Duration
25 logger io.Writer
26 }
27
28
29
30
31
32
33
34
35
36 func DefaultConfig() Config {
37 return Config{
38 version: V15,
39 port: 5432,
40 database: "postgres",
41 username: "postgres",
42 password: "postgres",
43 startTimeout: 15 * time.Second,
44 logger: os.Stdout,
45 binaryRepositoryURL: "https://repo1.maven.org/maven2",
46 }
47 }
48
49
50 func (c Config) Version(version PostgresVersion) Config {
51 c.version = version
52 return c
53 }
54
55
56 func (c Config) Port(port uint32) Config {
57 c.port = port
58 return c
59 }
60
61
62 func (c Config) Database(database string) Config {
63 c.database = database
64 return c
65 }
66
67
68 func (c Config) Username(username string) Config {
69 c.username = username
70 return c
71 }
72
73
74 func (c Config) Password(password string) Config {
75 c.password = password
76 return c
77 }
78
79
80
81 func (c Config) RuntimePath(path string) Config {
82 c.runtimePath = path
83 return c
84 }
85
86
87
88 func (c Config) CachePath(path string) Config {
89 c.cachePath = path
90 return c
91 }
92
93
94
95 func (c Config) DataPath(path string) Config {
96 c.dataPath = path
97 return c
98 }
99
100
101
102 func (c Config) BinariesPath(path string) Config {
103 c.binariesPath = path
104 return c
105 }
106
107
108 func (c Config) Locale(locale string) Config {
109 c.locale = locale
110 return c
111 }
112
113
114
115
116
117 func (c Config) StartParameters(parameters map[string]string) Config {
118 c.startParameters = parameters
119 return c
120 }
121
122
123 func (c Config) StartTimeout(timeout time.Duration) Config {
124 c.startTimeout = timeout
125 return c
126 }
127
128
129 func (c Config) Logger(logger io.Writer) Config {
130 c.logger = logger
131 return c
132 }
133
134
135 func (c Config) BinaryRepositoryURL(binaryRepositoryURL string) Config {
136 c.binaryRepositoryURL = binaryRepositoryURL
137 return c
138 }
139
140 func (c Config) GetConnectionURL() string {
141 return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s", c.username, c.password, "localhost", c.port, c.database)
142 }
143
144
145 type PostgresVersion string
146
147
148 const (
149 V15 = PostgresVersion("15.3.0")
150 V14 = PostgresVersion("14.8.0")
151 V13 = PostgresVersion("13.11.0")
152 V12 = PostgresVersion("12.15.0")
153 V11 = PostgresVersion("11.20.0")
154 V10 = PostgresVersion("10.23.0")
155 V9 = PostgresVersion("9.6.24")
156 )
157
View as plain text