-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.go
109 lines (93 loc) · 3.33 KB
/
setup.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
package setup
import (
"bufio"
"crypto/tls"
"net/http"
"os"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/alecthomas/kong"
tfe "github.com/hashicorp/go-tfe"
)
type CLI struct {
// TODO: Only works for single org.
// Add mutli ORG capabilities for comma separated list or missing for "All"
Organization string `required:"" short:"o" env:"TF_ORGANIZATION" help:"Name of the Organization to scrape from."`
APIToken string `short:"t" env:"TF_API_TOKEN" help:"User token for autheticating with the API."`
APITokenFile *os.File `placeholder:"/path/to/file" help:"File containing user token for autheticating with the API."`
APIAddress string `placeholder:"https://app.terraform.io/" help:"Terraform API address to scrape metrics from."`
APIInsecureSkipVerify bool `help:"Accept any certificate presented by the API."`
ListenAddress string `default:"0.0.0.0:9100" help:"Address to listen on for web interface and telemetry."`
LogLevel string `default:"info" enum:"debug,info,warn,error" help:"Only log messages with the given severity or above. One of: [${enum}]"`
LogFormat string `default:"logfmt" enum:"logfmt,json" help:"Output format of log messages. One of: [${enum}]"`
}
type Config struct {
CLI
Client tfe.Client
Logger log.Logger
}
// NewConfig returns a new Config object that was initialized according to the CLI params.
func NewConfig() *Config {
config := &Config{}
kong.Parse(&config.CLI)
config.setupLogger()
config.setupClient()
return config
}
func (c *Config) setupLogger() {
// Changes timestamp from 9 variable to 3 fixed
// decimals (.130 instead of .130987456).
timestampFormat := log.TimestampFormat(
func() time.Time { return time.Now().UTC() },
"2006-01-02T15:04:05.000Z07:00",
)
if c.LogFormat == "json" {
c.Logger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
} else {
c.Logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
}
switch c.LogLevel {
case "debug":
c.Logger = level.NewFilter(c.Logger, level.AllowDebug())
case "warn":
c.Logger = level.NewFilter(c.Logger, level.AllowWarn())
case "error":
c.Logger = level.NewFilter(c.Logger, level.AllowError())
default:
c.Logger = level.NewFilter(c.Logger, level.AllowInfo())
}
c.Logger = log.With(c.Logger, "ts", timestampFormat, "caller", log.DefaultCaller)
}
func (c *Config) setupClient() {
config := &tfe.Config{}
if c.APITokenFile != nil {
defer c.APITokenFile.Close()
scanner := bufio.NewScanner(c.APITokenFile)
scanner.Scan()
config.Token = scanner.Text()
} else if c.APIToken != "" {
config.Token = c.APIToken
} else {
level.Error(c.Logger).Log("msg", "Error creating tfe client", "err", "Missing API Token.")
os.Exit(1)
}
if c.APIAddress != "" {
config.Address = c.APIAddress
level.Info(c.Logger).Log("msg", "Overwritten Terraform API address", "address", c.APIAddress)
}
if c.APIInsecureSkipVerify {
config.HTTPClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.APIInsecureSkipVerify},
},
}
level.Warn(c.Logger).Log("msg", "HTTP InsecureSkipVerify is enabled.")
}
client, err := tfe.NewClient(config)
if err != nil {
level.Error(c.Logger).Log("msg", "Error creating tfe client", "err", err)
os.Exit(1)
}
c.Client = *client
}