forked from trickstercache/trickster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
118 lines (95 loc) · 2.88 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
)
// 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(trickster, -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(progversion)
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)
}
}
// 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
f := flag.NewFlagSet(trickster, 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.")
// 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
}
}