-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.go
103 lines (89 loc) · 2.78 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
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
package timescaledb
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
"go.k6.io/k6/lib/types"
"gopkg.in/guregu/null.v3"
)
type config struct {
// Connection URL in the form specified in the libpq docs,
// see https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING):
// postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]
URL null.String `json:"url"`
PushInterval types.NullDuration `json:"pushInterval"`
dbName null.String
addr null.String
}
func newConfig() config {
return config{
URL: null.NewString("postgresql://localhost/myk6timescaleDB", false),
PushInterval: types.NewNullDuration(time.Second, false),
dbName: null.NewString("myk6timescaleDB", false),
addr: null.NewString("postgresql://localhost", false),
}
}
func (c config) apply(modifiedConf config) config {
if modifiedConf.URL.Valid {
c.URL = modifiedConf.URL
}
if modifiedConf.PushInterval.Valid {
c.PushInterval = modifiedConf.PushInterval
}
if modifiedConf.dbName.Valid {
c.dbName = modifiedConf.dbName
}
if modifiedConf.addr.Valid {
c.addr = modifiedConf.addr
}
return c
}
func getConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string, confArg string) (config, error) {
consolidatedConf := newConfig()
if jsonRawConf != nil {
var jsonConf config
if err := json.Unmarshal(jsonRawConf, &jsonConf); err != nil {
return config{}, fmt.Errorf("problem unmarshalling JSON: %w", err)
}
consolidatedConf = consolidatedConf.apply(jsonConf)
jsonURLConf, err := parseURL(consolidatedConf.URL.String)
if err != nil {
return config{}, fmt.Errorf("problem parsing URL provided in JSON %q: %w",
consolidatedConf.URL.String, err)
}
consolidatedConf = consolidatedConf.apply(jsonURLConf)
}
envPushInterval, ok := env["K6_TIMESCALEDB_PUSH_INTERVAL"]
if ok {
pushInterval, err := time.ParseDuration(envPushInterval)
if err != nil {
return config{}, fmt.Errorf("invalid K6_TIMESCALEDB_PUSH_INTERVAL: %w", err)
}
consolidatedConf = consolidatedConf.apply(config{PushInterval: types.NewNullDuration(pushInterval, true)})
}
if confArg != "" {
parsedConfArg, err := parseURL(confArg)
if err != nil {
return config{}, fmt.Errorf("invalid config argument %q: %w", confArg, err)
}
consolidatedConf = consolidatedConf.apply(parsedConfArg)
}
return consolidatedConf, nil
}
func parseURL(text string) (config, error) {
u, err := url.Parse(text)
if err != nil {
return config{}, err
}
var parsedConf config
parsedConf.URL = null.StringFrom(text)
if u.Host != "" {
parsedConf.addr = null.StringFrom(u.Scheme + "://" + u.Host)
}
if dbName := strings.TrimPrefix(u.Path, "/"); dbName != "" {
parsedConf.dbName = null.StringFrom(dbName)
}
return parsedConf, err
}