This repository has been archived by the owner on Apr 18, 2020. It is now read-only.
forked from trickstercache/trickster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
159 lines (137 loc) · 4.37 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
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
/**
* 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 main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
)
const (
// Command-line flags
cfConfig = "config"
cfVersion = "version"
cfLogLevel = "log-level"
cfInstanceID = "instance-id"
cfOrigin = "origin"
cfProxyPort = "proxy-port"
cfMetricsPort = "metrics-port"
cfProfilerPort = "profiler-port"
// Environment variables
evOrigin = "TRK_ORIGIN"
evProxyPort = "TRK_PROXY_PORT"
evMetricsPort = "TRK_METRICS_PORT"
evLogLevel = "TRK_LOG_LEVEL"
evProfilerPort = "TRK_PROFILER_PORT"
)
// loadConfiguration reads the config path from Flags,
// Loads the configs (w/ default values where missing)
// and then evaluates any provided flags as overrides
func loadConfiguration(c *Config, arguments []string) error {
var path string
var version bool
f := flag.NewFlagSet(applicationName, -1)
f.SetOutput(ioutil.Discard)
f.StringVar(&path, cfConfig, "", "Supplies Path to Config File")
f.BoolVar(&version, cfVersion, false, "Prints trickster version")
f.Parse(arguments)
// If the config file is not specified on the cmdline then try the default
// location to load the config file. If the default config does not exist
// then move on, no big deal.
if path != "" {
if err := c.LoadFile(path); err != nil {
return err
}
} else {
_, err := os.Open(c.Main.ConfigFile)
if err == nil {
if err := c.LoadFile(c.Main.ConfigFile); err != nil {
return err
}
}
}
// Display version information then exit the program
if version == true {
fmt.Println(applicationVersion)
os.Exit(3)
}
// Load from Environment Variables
loadEnvVars(c)
//Load from command line flags.
loadFlags(c, arguments)
return nil
}
func loadEnvVars(c *Config) {
// Origin
if x := os.Getenv(evOrigin); x != "" {
c.DefaultOriginURL = x
}
// Proxy Port
if x := os.Getenv(evProxyPort); x != "" {
if y, err := strconv.ParseInt(x, 10, 64); err == nil {
c.ProxyServer.ListenPort = int(y)
}
}
// Metrics Port
if x := os.Getenv(evMetricsPort); x != "" {
if y, err := strconv.ParseInt(x, 10, 64); err == nil {
c.Metrics.ListenPort = int(y)
}
}
// ProfilerPort
if x := os.Getenv(evProfilerPort); x != "" {
if y, err := strconv.ParseInt(x, 10, 64); err == nil {
c.Profiler.ListenPort = int(y)
c.Profiler.Enabled = true
}
}
// LogLevel
if x := os.Getenv(evLogLevel); x != "" {
c.Logging.LogLevel = x
}
}
// loadFlags loads configuration from command line flags.
func loadFlags(c *Config, arguments []string) {
var path string
var version bool
var origin string
var proxyListenPort int
var metricsListenPort int
var profilerListenPort int
f := flag.NewFlagSet(applicationName, flag.ExitOnError)
f.BoolVar(&version, cfVersion, true, "Prints Trickster version")
f.StringVar(&c.Logging.LogLevel, cfLogLevel, c.Logging.LogLevel, "Level of Logging to use (debug, info, warn, error)")
f.IntVar(&c.Main.InstanceID, cfInstanceID, 0, "Instance ID for when running multiple processes")
f.StringVar(&origin, cfOrigin, "", "URL to the Prometheus Origin. Enter it like you would in grafana, e.g., http://prometheus:9090")
f.IntVar(&proxyListenPort, cfProxyPort, 0, "Port that the Proxy server will listen on.")
f.IntVar(&metricsListenPort, cfMetricsPort, 0, "Port that the /metrics endpoint will listen on.")
f.IntVar(&profilerListenPort, cfProfilerPort, 0, "Port that the /debug/pprof endpoint will listen on.")
// BEGIN IGNORED FLAGS
f.StringVar(&path, cfConfig, "", "Path to Trickster Config File")
// END IGNORED FLAGS
f.Parse(arguments)
if len(origin) > 0 {
c.DefaultOriginURL = origin
}
if proxyListenPort > 0 {
c.ProxyServer.ListenPort = proxyListenPort
}
if metricsListenPort > 0 {
c.Metrics.ListenPort = metricsListenPort
}
if profilerListenPort > 0 {
c.Profiler.ListenPort = profilerListenPort
c.Profiler.Enabled = true
}
}