This repository was archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconfig.go
59 lines (49 loc) · 1.34 KB
/
config.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
package main
import (
"fmt"
"os"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)
// Modem represents the address of the modem and its admin credentials.
type Modem struct {
Address string `yaml:"address"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
// Telemetry represents the exporter's listen address and metrics URI path.
type Telemetry struct {
ListenAddress string `yaml:"listen_address"`
MetricsPath string `yaml:"metrics_path"`
}
// Config represents the yaml config file structure.
type Config struct {
Modem Modem `yaml:"modem"`
Telemetry Telemetry `yaml:"telemetry"`
}
// NewConfigFromFile reads the configuration file from the given path
// and returns a populated Config struct.
func NewConfigFromFile(path string) (*Config, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrap(err, "failed to read config file")
}
// Setup default config.
config := Config{
Modem: Modem{
Address: "192.168.100.1",
Username: "admin",
},
Telemetry: Telemetry{
ListenAddress: ":9527",
MetricsPath: "/metrics",
},
}
if err := yaml.Unmarshal(content, &config); err != nil {
return nil, errors.Wrap(err, "unable to parse config YAML")
}
if config.Modem.Password == "" {
return nil, fmt.Errorf("modem password isn't set in config")
}
return &config, nil
}