2019-05-08 23:45:04 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/heetch/confita"
|
|
|
|
"github.com/heetch/confita/backend"
|
|
|
|
"github.com/heetch/confita/backend/env"
|
|
|
|
"github.com/heetch/confita/backend/flags"
|
|
|
|
)
|
|
|
|
|
2019-05-09 21:11:31 +02:00
|
|
|
// Config is the exporter CLI configuration.
|
2019-05-08 23:45:04 +02:00
|
|
|
type Config struct {
|
|
|
|
PIHoleHostname string `config:"pihole_hostname"`
|
|
|
|
PIHolePassword string `config:"pihole_password"`
|
|
|
|
|
|
|
|
Port string `config:"port"`
|
|
|
|
Interval time.Duration `config:"interval"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
PIHoleHostname: "127.0.0.1",
|
|
|
|
PIHolePassword: "",
|
|
|
|
|
2019-07-29 18:03:33 +02:00
|
|
|
Port: "9617",
|
2019-05-09 11:28:35 +02:00
|
|
|
Interval: 10 * time.Second,
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 21:11:31 +02:00
|
|
|
// Load method loads the configuration by using both flag or environment variables.
|
2019-05-08 23:45:04 +02:00
|
|
|
func Load() *Config {
|
|
|
|
loaders := []backend.Backend{
|
|
|
|
env.NewBackend(),
|
|
|
|
flags.NewBackend(),
|
|
|
|
}
|
|
|
|
|
|
|
|
loader := confita.NewLoader(loaders...)
|
|
|
|
|
|
|
|
cfg := getDefaultConfig()
|
|
|
|
err := loader.Load(context.Background(), cfg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.show()
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) show() {
|
|
|
|
val := reflect.ValueOf(&c).Elem()
|
|
|
|
log.Println("------------------------------------")
|
|
|
|
log.Println("- PI-Hole exporter configuration -")
|
|
|
|
log.Println("------------------------------------")
|
|
|
|
for i := 0; i < val.NumField(); i++ {
|
|
|
|
valueField := val.Field(i)
|
|
|
|
typeField := val.Type().Field(i)
|
|
|
|
|
2020-04-04 20:59:31 +02:00
|
|
|
// Do not print password
|
|
|
|
if typeField.Name != "PIHolePassword" {
|
|
|
|
log.Println(fmt.Sprintf("%s : %v", typeField.Name, valueField.Interface()))
|
|
|
|
}
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
log.Println("------------------------------------")
|
|
|
|
}
|