-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
78 lines (65 loc) · 1.75 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
package main
import (
"errors"
"flag"
"strings"
"time"
)
type Config struct {
Start time.Time
End time.Time
PrometheusAddress string
CredentialFile string
BigQueryProject string
BigQueryDataset string
BigQueryTable string
Metrics []string
}
func ParseOptions() (*Config, error) {
start := flag.String("start", "", "start time")
end := flag.String("end", "", "end time")
prometheus := flag.String("prometheus", "", "URL of Prometheus")
credential := flag.String("credential", "", "Credential file")
bigquery := flag.String("bigquery", "", "BigQuery (project:dataset.table)")
flag.Parse()
if *start == "" {
return nil, errors.New("start option is required")
}
if *end == "" {
return nil, errors.New("end option is required")
}
if *prometheus == "" {
return nil, errors.New("prometheus option is required")
}
if *credential == "" {
return nil, errors.New("credential option is required")
}
if *bigquery == "" {
return nil, errors.New("bigquery option is required")
}
parsedStart, err := time.Parse(time.RFC3339, *start)
if err != nil {
return nil, err
}
parsedEnd, err := time.Parse(time.RFC3339, *end)
if err != nil {
return nil, err
}
bigqueryComponents := strings.FieldsFunc(*bigquery, func(r rune) bool {
return r == ':' || r == '.'
})
if len(bigqueryComponents) != 3 {
return nil, errors.New("invalid bigquery argument format")
}
metrics := flag.Args()
return &Config{
Start: parsedStart,
End: parsedEnd,
PrometheusAddress: *prometheus,
CredentialFile: *credential,
BigQueryProject: bigqueryComponents[0],
BigQueryDataset: bigqueryComponents[1],
BigQueryTable: bigqueryComponents[2],
Metrics: metrics,
}, nil
}