-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
44 lines (38 loc) · 1.01 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
package main
import (
"fmt"
"github.com/spf13/viper"
)
type Config struct {
path string
name string
gpio string
maxRetries int
listenPort int
defaultLogLevel string
temperatureUnit string
}
func ReadConfig() *Config {
/**
Reads YAML configuration file and create a struct containing the values
**/
viper.SetConfigName("dht-prometheus-exporter")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc")
viper.AddConfigPath("$HOME")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Sprintf("Error when reading config file: %v", err))
}
config := &Config{
path: viper.ConfigFileUsed(),
name: viper.GetString("name"),
gpio: fmt.Sprintf("GPIO%d", viper.GetInt("gpio_pin")),
maxRetries: viper.GetInt("max_retries"),
listenPort: viper.GetInt("listen_port"),
defaultLogLevel: viper.GetString("log_level"),
temperatureUnit: viper.GetString("temperature_unit"),
}
return config
}