-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
49 lines (40 loc) · 936 Bytes
/
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
package gopg
import (
"github.com/boxgo/box/pkg/config"
"github.com/boxgo/box/pkg/logger"
)
type (
// Config 配置
Config struct {
path string
URI string `config:"uri" desc:"pg connection url. example: postgres://user:pass@localhost:5432/db_name?k=v"`
}
// OptionFunc 选项信息
OptionFunc func(*Config)
)
// StdConfig 标准配置
func StdConfig(key string, optionFunc ...OptionFunc) *Config {
cfg := DefaultConfig(key)
for _, fn := range optionFunc {
fn(cfg)
}
if err := config.Scan(cfg); err != nil {
logger.Panicf("PostgreSQL build error: %s", err)
}
return cfg
}
// DefaultConfig 默认配置
func DefaultConfig(key string) *Config {
return &Config{
path: "pg." + key,
URI: "postgres://user:pass@localhost:5432/db_name",
}
}
// Build 构建实例
func (c *Config) Build() *PostgreSQL {
return newPostgreSQL(c)
}
// Path 实例配置目录
func (c *Config) Path() string {
return c.path
}