-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
62 lines (55 loc) · 1.49 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
package postgres
import (
"encoding/json"
"fmt"
"net/url"
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/sshtunnel"
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/util"
)
// Config used to connect to SQL Database
type Config struct {
Host string `json:"host"`
Port int `json:"port"`
DBName string `json:"dbname"`
User string `json:"user"`
Password string `json:"password"`
SSLMode string `json:"sslmode"`
TunnelInfo *sshtunnel.Config `json:"tunnel_info,omitempty"`
// SkipHostValidation is used to skip host validation during tests
SkipHostValidation bool `json:"skipHostValidation"`
UseLegacyMappings bool `json:"useLegacyMappings"`
}
func (c Config) ConnectionString() string {
if c.Port == 0 {
c.Port = 5432
}
sslMode := "disable"
if c.SSLMode != "" {
sslMode = c.SSLMode
}
dsn := url.URL{
Scheme: DatabaseType,
User: url.UserPassword(c.User, c.Password),
Host: fmt.Sprintf("%s:%d", c.Host, c.Port),
Path: c.DBName,
}
values := dsn.Query()
values.Set("sslmode", sslMode)
dsn.RawQuery = values.Encode()
return dsn.String()
}
func (c *Config) Parse(input json.RawMessage) error {
err := json.Unmarshal(input, c)
if err != nil {
return err
}
if c.TunnelInfo == nil { // if tunnel info is not provided as a separate json object, try to parse it from the input
if c.TunnelInfo, err = sshtunnel.ParseInlineConfig(input); err != nil {
return err
}
}
if !c.SkipHostValidation {
return util.ValidateHost(c.Host)
}
return nil
}