-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
89 lines (78 loc) · 2.33 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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package csv
import (
"fmt"
"strings"
"time"
"gopkg.in/guregu/null.v3"
"github.com/loadimpact/k6/lib/types"
)
// Config is the config for the csv collector
type Config struct {
// Samples.
FileName null.String `json:"file_name" envconfig:"K6_CSV_FILENAME"`
SaveInterval types.NullDuration `json:"save_interval" envconfig:"K6_CSV_SAVE_INTERVAL"`
}
// NewConfig creates a new Config instance with default values for some fields.
func NewConfig() Config {
return Config{
FileName: null.StringFrom("file.csv"),
SaveInterval: types.NullDurationFrom(1 * time.Second),
}
}
// Apply merges two configs by overwriting properties in the old config
func (c Config) Apply(cfg Config) Config {
if cfg.FileName.Valid {
c.FileName = cfg.FileName
}
if cfg.SaveInterval.Valid {
c.SaveInterval = cfg.SaveInterval
}
return c
}
// ParseArg takes an arg string and converts it to a config
func ParseArg(arg string) (Config, error) {
c := Config{}
if !strings.Contains(arg, "=") {
c.FileName = null.StringFrom(arg)
c.SaveInterval = types.NullDurationFrom(1 * time.Second)
return c, nil
}
pairs := strings.Split(arg, ",")
for _, pair := range pairs {
r := strings.SplitN(pair, "=", 2)
if len(r) != 2 {
return c, fmt.Errorf("couldn't parse %q as argument for csv output", arg)
}
switch r[0] {
case "save_interval":
err := c.SaveInterval.UnmarshalText([]byte(r[1]))
if err != nil {
return c, err
}
case "file_name":
c.FileName = null.StringFrom(r[1])
default:
return c, fmt.Errorf("unknown key %q as argument for csv output", r[0])
}
}
return c, nil
}