|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "errors" |
5 | 4 | "os"
|
6 |
| - "time" |
7 | 5 |
|
8 |
| - "github.com/go-kit/kit/log/level" |
9 | 6 | "github.com/joho/godotenv"
|
10 |
| - "github.com/promhippie/scw_exporter/pkg/action" |
11 |
| - "github.com/promhippie/scw_exporter/pkg/config" |
12 |
| - "github.com/promhippie/scw_exporter/pkg/version" |
13 |
| - "gopkg.in/urfave/cli.v2" |
14 |
| -) |
15 |
| - |
16 |
| -var ( |
17 |
| - // ErrMissingScalewayToken defines the error if scw.token is empty. |
18 |
| - ErrMissingScalewayToken = errors.New("Missing required scw.token") |
19 |
| - |
20 |
| - // ErrMissingScalewayOrg defines the error if scw.org is empty. |
21 |
| - ErrMissingScalewayOrg = errors.New("Missing required scw.org") |
22 |
| - |
23 |
| - // ErrMissingScalewayRegion defines the error if scw.region is empty. |
24 |
| - ErrMissingScalewayRegion = errors.New("Missing required scw.region") |
| 7 | + "github.com/promhippie/scw_exporter/pkg/command" |
25 | 8 | )
|
26 | 9 |
|
27 | 10 | func main() {
|
28 |
| - cfg := config.Load() |
29 |
| - |
30 | 11 | if env := os.Getenv("SCW_EXPORTER_ENV_FILE"); env != "" {
|
31 | 12 | godotenv.Load(env)
|
32 | 13 | }
|
33 | 14 |
|
34 |
| - app := &cli.App{ |
35 |
| - Name: "scw_exporter", |
36 |
| - Version: version.Version, |
37 |
| - Usage: "Scaleway Exporter", |
38 |
| - Authors: []*cli.Author{ |
39 |
| - { |
40 |
| - Name: "Thomas Boerger", |
41 |
| - |
42 |
| - }, |
43 |
| - }, |
44 |
| - Flags: []cli.Flag{ |
45 |
| - &cli.StringFlag{ |
46 |
| - Name: "log.level", |
47 |
| - Value: "info", |
48 |
| - Usage: "Only log messages with given severity", |
49 |
| - EnvVars: []string{"SCW_EXPORTER_LOG_LEVEL"}, |
50 |
| - Destination: &cfg.Logs.Level, |
51 |
| - }, |
52 |
| - &cli.BoolFlag{ |
53 |
| - Name: "log.pretty", |
54 |
| - Value: false, |
55 |
| - Usage: "Enable pretty messages for logging", |
56 |
| - EnvVars: []string{"SCW_EXPORTER_LOG_PRETTY"}, |
57 |
| - Destination: &cfg.Logs.Pretty, |
58 |
| - }, |
59 |
| - &cli.StringFlag{ |
60 |
| - Name: "web.address", |
61 |
| - Value: "0.0.0.0:9503", |
62 |
| - Usage: "Address to bind the metrics server", |
63 |
| - EnvVars: []string{"SCW_EXPORTER_WEB_ADDRESS"}, |
64 |
| - Destination: &cfg.Server.Addr, |
65 |
| - }, |
66 |
| - &cli.StringFlag{ |
67 |
| - Name: "web.path", |
68 |
| - Value: "/metrics", |
69 |
| - Usage: "Path to bind the metrics server", |
70 |
| - EnvVars: []string{"SCW_EXPORTER_WEB_PATH"}, |
71 |
| - Destination: &cfg.Server.Path, |
72 |
| - }, |
73 |
| - &cli.DurationFlag{ |
74 |
| - Name: "request.timeout", |
75 |
| - Value: 5 * time.Second, |
76 |
| - Usage: "Request timeout as duration", |
77 |
| - EnvVars: []string{"SCW_EXPORTER_REQUEST_TIMEOUT"}, |
78 |
| - Destination: &cfg.Target.Timeout, |
79 |
| - }, |
80 |
| - &cli.StringFlag{ |
81 |
| - Name: "scw.token", |
82 |
| - Value: "", |
83 |
| - Usage: "Access token for the Scaleway API", |
84 |
| - EnvVars: []string{"SCW_EXPORTER_TOKEN"}, |
85 |
| - Destination: &cfg.Target.Token, |
86 |
| - }, |
87 |
| - &cli.StringFlag{ |
88 |
| - Name: "scw.org", |
89 |
| - Value: "", |
90 |
| - Usage: "Organization for the Scaleway API", |
91 |
| - EnvVars: []string{"SCW_EXPORTER_ORG"}, |
92 |
| - Destination: &cfg.Target.Org, |
93 |
| - }, |
94 |
| - &cli.StringFlag{ |
95 |
| - Name: "scw.region", |
96 |
| - Value: "", |
97 |
| - Usage: "Region for the Scaleway API", |
98 |
| - EnvVars: []string{"SCW_EXPORTER_REGION"}, |
99 |
| - Destination: &cfg.Target.Region, |
100 |
| - }, |
101 |
| - &cli.BoolFlag{ |
102 |
| - Name: "collector.dashboard", |
103 |
| - Value: true, |
104 |
| - Usage: "Enable collector for dashboard", |
105 |
| - EnvVars: []string{"SCW_EXPORTER_COLLECTOR_DASHBOARD"}, |
106 |
| - Destination: &cfg.Collector.Dashboard, |
107 |
| - }, |
108 |
| - &cli.BoolFlag{ |
109 |
| - Name: "collector.security-groups", |
110 |
| - Value: true, |
111 |
| - Usage: "Enable collector for security groups", |
112 |
| - EnvVars: []string{"SCW_EXPORTER_COLLECTOR_SECURITY_GROUPS"}, |
113 |
| - Destination: &cfg.Collector.SecurityGroups, |
114 |
| - }, |
115 |
| - &cli.BoolFlag{ |
116 |
| - Name: "collector.servers", |
117 |
| - Value: true, |
118 |
| - Usage: "Enable collector for servers", |
119 |
| - EnvVars: []string{"SCW_EXPORTER_COLLECTOR_SERVERS"}, |
120 |
| - Destination: &cfg.Collector.Servers, |
121 |
| - }, |
122 |
| - &cli.BoolFlag{ |
123 |
| - Name: "collector.snapshots", |
124 |
| - Value: true, |
125 |
| - Usage: "Enable collector for snapshots", |
126 |
| - EnvVars: []string{"SCW_EXPORTER_COLLECTOR_SNAPSHOTS"}, |
127 |
| - Destination: &cfg.Collector.Snapshots, |
128 |
| - }, |
129 |
| - &cli.BoolFlag{ |
130 |
| - Name: "collector.volumes", |
131 |
| - Value: true, |
132 |
| - Usage: "Enable collector for volumes", |
133 |
| - EnvVars: []string{"SCW_EXPORTER_COLLECTOR_VOLUMES"}, |
134 |
| - Destination: &cfg.Collector.Volumes, |
135 |
| - }, |
136 |
| - }, |
137 |
| - Action: func(c *cli.Context) error { |
138 |
| - logger := setupLogger(cfg) |
139 |
| - |
140 |
| - if cfg.Target.Token == "" { |
141 |
| - level.Error(logger).Log( |
142 |
| - "msg", ErrMissingScalewayToken, |
143 |
| - ) |
144 |
| - |
145 |
| - return ErrMissingScalewayToken |
146 |
| - } |
147 |
| - |
148 |
| - if cfg.Target.Org == "" { |
149 |
| - level.Error(logger).Log( |
150 |
| - "msg", ErrMissingScalewayOrg, |
151 |
| - ) |
152 |
| - |
153 |
| - return ErrMissingScalewayOrg |
154 |
| - } |
155 |
| - |
156 |
| - if cfg.Target.Region == "" { |
157 |
| - level.Error(logger).Log( |
158 |
| - "msg", ErrMissingScalewayRegion, |
159 |
| - ) |
160 |
| - |
161 |
| - return ErrMissingScalewayRegion |
162 |
| - } |
163 |
| - |
164 |
| - return action.Server(cfg, logger) |
165 |
| - }, |
166 |
| - } |
167 |
| - |
168 |
| - cli.HelpFlag = &cli.BoolFlag{ |
169 |
| - Name: "help", |
170 |
| - Aliases: []string{"h"}, |
171 |
| - Usage: "Show the help, so what you see now", |
172 |
| - } |
173 |
| - |
174 |
| - cli.VersionFlag = &cli.BoolFlag{ |
175 |
| - Name: "version", |
176 |
| - Aliases: []string{"v"}, |
177 |
| - Usage: "Print the current version of that tool", |
178 |
| - } |
179 |
| - |
180 |
| - if err := app.Run(os.Args); err != nil { |
| 15 | + if err := command.Run(); err != nil { |
181 | 16 | os.Exit(1)
|
182 | 17 | }
|
183 | 18 | }
|
0 commit comments