mirror of
https://github.com/steinhobelgruen/netatmo-exporter.git
synced 2024-11-24 10:13:56 +00:00
Configurable log level
This commit is contained in:
parent
87960917f6
commit
814c0344f1
51
config.go
51
config.go
|
@ -2,19 +2,23 @@ package main
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
netatmo "github.com/exzz/netatmo-api-go"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
const (
|
||||
envVarListenAddress = "NETATMO_EXPORTER_ADDR"
|
||||
envVarLogLevel = "NETATMO_LOG_LEVEL"
|
||||
envVarNetatmoClientID = "NETATMO_CLIENT_ID"
|
||||
envVarNetatmoClientSecret = "NETATMO_CLIENT_SECRET"
|
||||
envVarNetatmoUsername = "NETATMO_CLIENT_USERNAME"
|
||||
envVarNetatmoPassword = "NETATMO_CLIENT_PASSWORD"
|
||||
|
||||
flagListenAddress = "addr"
|
||||
flagLogLevel = "log-level"
|
||||
flagNetatmoClientID = "client-id"
|
||||
flagNetatmoClientSecret = "client-secret"
|
||||
flagNetatmoUsername = "username"
|
||||
|
@ -24,6 +28,7 @@ const (
|
|||
var (
|
||||
defaultConfig = config{
|
||||
Addr: ":9210",
|
||||
LogLevel: logLevel(logrus.InfoLevel),
|
||||
}
|
||||
|
||||
errNoBinaryName = errors.New("need the binary name as first argument")
|
||||
|
@ -34,8 +39,29 @@ var (
|
|||
errNoNetatmoPassword = errors.New("password can not be blank")
|
||||
)
|
||||
|
||||
type logLevel logrus.Level
|
||||
|
||||
func (l *logLevel) Type() string {
|
||||
return "level"
|
||||
}
|
||||
|
||||
func (l *logLevel) String() string {
|
||||
return fmt.Sprintf("%s", logrus.Level(*l))
|
||||
}
|
||||
|
||||
func (l *logLevel) Set(value string) error {
|
||||
level, err := logrus.ParseLevel(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*l = logLevel(level)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type config struct {
|
||||
Addr string
|
||||
LogLevel logLevel
|
||||
Netatmo netatmo.Config
|
||||
}
|
||||
|
||||
|
@ -48,42 +74,51 @@ func parseConfig(args []string, getenv func(string) string) (config, error) {
|
|||
|
||||
flagSet := pflag.NewFlagSet(args[0], pflag.ExitOnError)
|
||||
flagSet.StringVarP(&cfg.Addr, "addr", "a", cfg.Addr, "Address to listen on.")
|
||||
flagSet.Var(&cfg.LogLevel, flagLogLevel, "Sets the minimum level output through logging.")
|
||||
flagSet.StringVarP(&cfg.Netatmo.ClientID, "client-id", "i", cfg.Netatmo.ClientID, "Client ID for NetAtmo app.")
|
||||
flagSet.StringVarP(&cfg.Netatmo.ClientSecret, "client-secret", "s", cfg.Netatmo.ClientSecret, "Client secret for NetAtmo app.")
|
||||
flagSet.StringVarP(&cfg.Netatmo.Username, "username", "u", cfg.Netatmo.Username, "Username of NetAtmo account.")
|
||||
flagSet.StringVarP(&cfg.Netatmo.Password, "password", "p", cfg.Netatmo.Password, "Password of NetAtmo account.")
|
||||
flagSet.Parse(args[1:])
|
||||
|
||||
applyEnvironment(&cfg, getenv)
|
||||
if err := applyEnvironment(&cfg, getenv); err != nil {
|
||||
return config{}, fmt.Errorf("error in environment: %s", err)
|
||||
}
|
||||
|
||||
if len(cfg.Addr) == 0 {
|
||||
return cfg, errNoListenAddress
|
||||
return config{}, errNoListenAddress
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.ClientID) == 0 {
|
||||
return cfg, errNoNetatmoClientID
|
||||
return config{}, errNoNetatmoClientID
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.ClientSecret) == 0 {
|
||||
return cfg, errNoNetatmoClientSecret
|
||||
return config{}, errNoNetatmoClientSecret
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.Username) == 0 {
|
||||
return cfg, errNoNetatmoUsername
|
||||
return config{}, errNoNetatmoUsername
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.Password) == 0 {
|
||||
return cfg, errNoNetatmoPassword
|
||||
return config{}, errNoNetatmoPassword
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func applyEnvironment(cfg *config, getenv func(string) string) {
|
||||
func applyEnvironment(cfg *config, getenv func(string) string) error {
|
||||
if envAddr := getenv(envVarListenAddress); envAddr != "" {
|
||||
cfg.Addr = envAddr
|
||||
}
|
||||
|
||||
if envLogLevel := getenv(envVarLogLevel); envLogLevel != "" {
|
||||
if err := cfg.LogLevel.Set(envLogLevel); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if envClientID := getenv(envVarNetatmoClientID); envClientID != "" {
|
||||
cfg.Netatmo.ClientID = envClientID
|
||||
}
|
||||
|
@ -99,4 +134,6 @@ func applyEnvironment(cfg *config, getenv func(string) string) {
|
|||
if envPassword := getenv(envVarNetatmoPassword); envPassword != "" {
|
||||
cfg.Netatmo.Password = envPassword
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"testing"
|
||||
|
||||
netatmo "github.com/exzz/netatmo-api-go"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TestParseConfig(t *testing.T) {
|
||||
|
@ -38,6 +39,7 @@ func TestParseConfig(t *testing.T) {
|
|||
env: map[string]string{},
|
||||
wantConfig: config{
|
||||
Addr: defaultConfig.Addr,
|
||||
LogLevel: logLevel(logrus.InfoLevel),
|
||||
Netatmo: netatmo.Config{
|
||||
ClientID: "id",
|
||||
ClientSecret: "secret",
|
||||
|
@ -54,6 +56,7 @@ func TestParseConfig(t *testing.T) {
|
|||
},
|
||||
env: map[string]string{
|
||||
envVarListenAddress: ":8080",
|
||||
envVarLogLevel: "debug",
|
||||
envVarNetatmoClientID: "id",
|
||||
envVarNetatmoClientSecret: "secret",
|
||||
envVarNetatmoUsername: "username",
|
||||
|
@ -61,6 +64,7 @@ func TestParseConfig(t *testing.T) {
|
|||
},
|
||||
wantConfig: config{
|
||||
Addr: ":8080",
|
||||
LogLevel: logLevel(logrus.DebugLevel),
|
||||
Netatmo: netatmo.Config{
|
||||
ClientID: "id",
|
||||
ClientSecret: "secret",
|
||||
|
|
Loading…
Reference in a new issue