forked from tkuchiki/alp
/
config.go
163 lines (129 loc) · 3.96 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
)
type Config struct {
File string `yaml:"file"`
Sort string `yaml:"sort"`
Reverse bool `yaml:"reverse"`
QueryString bool `yaml:"query_string"`
Tsv bool `yaml:"tsv"`
ApptimeLabel string `yaml:"apptime_label"`
ReqtimeLabel string `yaml:"reqtime_label"`
StatusLabel string `yaml:"status_label"`
SizeLabel string `yaml:"size_label"`
MethodLabel string `yaml:"method_label"`
UriLabel string `yaml:"uri_label"`
TimeLabel string `yaml:"time_label"`
Limit int `yaml:"limit"`
Includes []string `yaml:"includes"`
Excludes []string `yaml:"excludes"`
IncludeStatuses []string `yaml:"include_statuses"`
ExcludeStatuses []string `yaml:"exclude_statuses"`
NoHeaders bool `yaml:no_headers`
Aggregates []string `yaml:"aggregates"`
StartTime string `yaml:"start_time"`
EndTime string `yaml:"end_time"`
StartTimeDuration string `yaml:"start_time_duration"`
EndTimeDuration string `yaml:"end_time_duration"`
IncludesStr string
ExcludesStr string
IncludeStatusesStr string
ExcludeStatusesStr string
AggregatesStr string
}
func SetConfig(config Config, arg Config) Config {
if arg.File != "" {
config.File = arg.File
}
if arg.Reverse {
config.Reverse = arg.Reverse
}
if arg.QueryString {
config.QueryString = arg.QueryString
}
if arg.Tsv {
config.Tsv = arg.Tsv
}
if config.ApptimeLabel == "" || (config.ApptimeLabel != "" && arg.ApptimeLabel != ApptimeLabel) {
config.ApptimeLabel = arg.ApptimeLabel
}
if config.ReqtimeLabel == "" || (config.ReqtimeLabel != "" && arg.ReqtimeLabel != ReqtimeLabel) {
config.ReqtimeLabel = arg.ReqtimeLabel
}
if config.StatusLabel == "" || (config.StatusLabel != "" && arg.StatusLabel != StatusLabel) {
config.StatusLabel = arg.StatusLabel
}
if config.SizeLabel == "" || (config.SizeLabel != "" && arg.SizeLabel != SizeLabel) {
config.SizeLabel = arg.SizeLabel
}
if config.MethodLabel == "" || (config.MethodLabel != "" && arg.MethodLabel != MethodLabel) {
config.MethodLabel = arg.MethodLabel
}
if config.UriLabel == "" || (config.UriLabel != "" && arg.UriLabel != UriLabel) {
config.UriLabel = arg.UriLabel
}
if config.TimeLabel == "" || (config.TimeLabel != "" && arg.TimeLabel != TimeLabel) {
config.TimeLabel = arg.TimeLabel
}
if config.Limit == 0 || (config.Limit != 0 && arg.Limit != Limit) {
config.Limit = arg.Limit
}
if arg.IncludesStr != "" {
config.Includes = Split(arg.IncludesStr, ",")
}
if arg.ExcludesStr != "" {
config.Excludes = Split(arg.ExcludesStr, ",")
}
if arg.IncludeStatusesStr != "" {
config.IncludeStatuses = Split(arg.IncludeStatusesStr, ",")
}
if arg.ExcludeStatusesStr != "" {
config.ExcludeStatuses = Split(arg.ExcludeStatusesStr, ",")
}
if arg.NoHeaders {
config.NoHeaders = arg.NoHeaders
}
if arg.AggregatesStr != "" {
config.Aggregates = Split(arg.AggregatesStr, ",")
}
if arg.StartTime != "" {
config.StartTime = arg.StartTime
}
if arg.EndTime != "" {
config.EndTime = arg.EndTime
}
if arg.StartTimeDuration != "" {
config.StartTimeDuration = arg.StartTimeDuration
}
if arg.EndTimeDuration != "" {
config.EndTimeDuration = arg.EndTimeDuration
}
return config
}
func LoadYAML(filename string) (config Config, err error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return config, err
}
err = yaml.Unmarshal(buf, &config)
return config, err
}
func DumpProfiles(filename string, ps Profiles) (err error) {
buf, err := yaml.Marshal(&ps)
if err != nil {
return err
}
err = ioutil.WriteFile(filename, buf, os.ModePerm)
return err
}
func LoadProfiles(filename string) (ps Profiles, err error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return ps, err
}
err = yaml.Unmarshal(buf, &ps)
return ps, err
}