-
Notifications
You must be signed in to change notification settings - Fork 177
/
flags.go
112 lines (102 loc) · 3.33 KB
/
flags.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
/*
* Copyright 2018 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"flag"
d "github.com/tricksterproxy/trickster/pkg/config/defaults"
)
const (
// Command-line flags
cfConfig = "config"
cfVersion = "version"
cfValidate = "validate-config"
cfLogLevel = "log-level"
cfInstanceID = "instance-id"
cfOrigin = "origin-url"
cfOriginType = "origin-type"
cfProxyPort = "proxy-port"
cfMetricsPort = "metrics-port"
)
// Flags holds the values for whitelisted flags
type Flags struct {
PrintVersion bool
ValidateConfig bool
ConfigPath string
customPath bool
Origin string
OriginType string
ProxyListenPort int
MetricsListenPort int
LogLevel string
InstanceID int
}
func parseFlags(applicationName string, arguments []string) (*Flags, error) {
flags := &Flags{}
flagSet := flag.NewFlagSet("trickster", flag.ContinueOnError)
flagSet.BoolVar(&flags.PrintVersion, cfVersion, false,
"Prints the Trickster version")
flagSet.BoolVar(&flags.ValidateConfig, cfValidate, false,
"Validates a Trickster config and exits without running the server")
flagSet.StringVar(&flags.ConfigPath, cfConfig, "",
"Path to Trickster Config File")
flagSet.StringVar(&flags.LogLevel, cfLogLevel, "",
"Level of Logging to use (debug, info, warn, error)")
flagSet.IntVar(&flags.InstanceID, cfInstanceID, 0,
"Instance ID is for running multiple Trickster processes"+
" from the same config while logging to their own files")
flagSet.StringVar(&flags.Origin, cfOrigin, "",
"URL to the Origin. Enter it like you would in grafana, e.g., http://prometheus:9090")
flagSet.StringVar(&flags.OriginType, cfOriginType, "",
"Type of origin (prometheus, influxdb)")
flagSet.IntVar(&flags.ProxyListenPort, cfProxyPort, 0,
"Port that the primary Proxy server will listen on")
flagSet.IntVar(&flags.MetricsListenPort, cfMetricsPort, 0,
"Port that the /metrics endpoint will listen on")
err := flagSet.Parse(arguments)
if err != nil {
return nil, err
}
if flags.ConfigPath != "" {
flags.customPath = true
} else {
flags.ConfigPath = d.DefaultConfigPath
}
return flags, nil
}
// loadFlags loads configuration from command line flags.
func (c *Config) loadFlags(flags *Flags) {
if len(flags.Origin) > 0 {
c.providedOriginURL = flags.Origin
}
if len(flags.OriginType) > 0 {
c.providedOriginType = flags.OriginType
}
if flags.ProxyListenPort > 0 {
c.Frontend.ListenPort = flags.ProxyListenPort
}
if flags.MetricsListenPort > 0 {
c.Metrics.ListenPort = flags.MetricsListenPort
}
// if flags.ReloadListenPort > 0 {
// c.Main.Reload.ListenPort = flags.ReloadListenPort
// }
if flags.LogLevel != "" {
c.Logging.LogLevel = flags.LogLevel
}
if flags.InstanceID > 0 {
c.Main.InstanceID = flags.InstanceID
}
}