mirror of
https://github.com/steinhobelgruen/netatmo-exporter.git
synced 2024-11-21 17:03:56 +00:00
Configurable log level
This commit is contained in:
parent
87960917f6
commit
814c0344f1
57
config.go
57
config.go
|
@ -2,19 +2,23 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
netatmo "github.com/exzz/netatmo-api-go"
|
netatmo "github.com/exzz/netatmo-api-go"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
envVarListenAddress = "NETATMO_EXPORTER_ADDR"
|
envVarListenAddress = "NETATMO_EXPORTER_ADDR"
|
||||||
|
envVarLogLevel = "NETATMO_LOG_LEVEL"
|
||||||
envVarNetatmoClientID = "NETATMO_CLIENT_ID"
|
envVarNetatmoClientID = "NETATMO_CLIENT_ID"
|
||||||
envVarNetatmoClientSecret = "NETATMO_CLIENT_SECRET"
|
envVarNetatmoClientSecret = "NETATMO_CLIENT_SECRET"
|
||||||
envVarNetatmoUsername = "NETATMO_CLIENT_USERNAME"
|
envVarNetatmoUsername = "NETATMO_CLIENT_USERNAME"
|
||||||
envVarNetatmoPassword = "NETATMO_CLIENT_PASSWORD"
|
envVarNetatmoPassword = "NETATMO_CLIENT_PASSWORD"
|
||||||
|
|
||||||
flagListenAddress = "addr"
|
flagListenAddress = "addr"
|
||||||
|
flagLogLevel = "log-level"
|
||||||
flagNetatmoClientID = "client-id"
|
flagNetatmoClientID = "client-id"
|
||||||
flagNetatmoClientSecret = "client-secret"
|
flagNetatmoClientSecret = "client-secret"
|
||||||
flagNetatmoUsername = "username"
|
flagNetatmoUsername = "username"
|
||||||
|
@ -23,7 +27,8 @@ const (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultConfig = config{
|
defaultConfig = config{
|
||||||
Addr: ":9210",
|
Addr: ":9210",
|
||||||
|
LogLevel: logLevel(logrus.InfoLevel),
|
||||||
}
|
}
|
||||||
|
|
||||||
errNoBinaryName = errors.New("need the binary name as first argument")
|
errNoBinaryName = errors.New("need the binary name as first argument")
|
||||||
|
@ -34,9 +39,30 @@ var (
|
||||||
errNoNetatmoPassword = errors.New("password can not be blank")
|
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 {
|
type config struct {
|
||||||
Addr string
|
Addr string
|
||||||
Netatmo netatmo.Config
|
LogLevel logLevel
|
||||||
|
Netatmo netatmo.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(args []string, getenv func(string) string) (config, error) {
|
func parseConfig(args []string, getenv func(string) string) (config, error) {
|
||||||
|
@ -48,42 +74,51 @@ func parseConfig(args []string, getenv func(string) string) (config, error) {
|
||||||
|
|
||||||
flagSet := pflag.NewFlagSet(args[0], pflag.ExitOnError)
|
flagSet := pflag.NewFlagSet(args[0], pflag.ExitOnError)
|
||||||
flagSet.StringVarP(&cfg.Addr, "addr", "a", cfg.Addr, "Address to listen on.")
|
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.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.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.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.StringVarP(&cfg.Netatmo.Password, "password", "p", cfg.Netatmo.Password, "Password of NetAtmo account.")
|
||||||
flagSet.Parse(args[1:])
|
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 {
|
if len(cfg.Addr) == 0 {
|
||||||
return cfg, errNoListenAddress
|
return config{}, errNoListenAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cfg.Netatmo.ClientID) == 0 {
|
if len(cfg.Netatmo.ClientID) == 0 {
|
||||||
return cfg, errNoNetatmoClientID
|
return config{}, errNoNetatmoClientID
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cfg.Netatmo.ClientSecret) == 0 {
|
if len(cfg.Netatmo.ClientSecret) == 0 {
|
||||||
return cfg, errNoNetatmoClientSecret
|
return config{}, errNoNetatmoClientSecret
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cfg.Netatmo.Username) == 0 {
|
if len(cfg.Netatmo.Username) == 0 {
|
||||||
return cfg, errNoNetatmoUsername
|
return config{}, errNoNetatmoUsername
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cfg.Netatmo.Password) == 0 {
|
if len(cfg.Netatmo.Password) == 0 {
|
||||||
return cfg, errNoNetatmoPassword
|
return config{}, errNoNetatmoPassword
|
||||||
}
|
}
|
||||||
|
|
||||||
return cfg, nil
|
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 != "" {
|
if envAddr := getenv(envVarListenAddress); envAddr != "" {
|
||||||
cfg.Addr = envAddr
|
cfg.Addr = envAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if envLogLevel := getenv(envVarLogLevel); envLogLevel != "" {
|
||||||
|
if err := cfg.LogLevel.Set(envLogLevel); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if envClientID := getenv(envVarNetatmoClientID); envClientID != "" {
|
if envClientID := getenv(envVarNetatmoClientID); envClientID != "" {
|
||||||
cfg.Netatmo.ClientID = envClientID
|
cfg.Netatmo.ClientID = envClientID
|
||||||
}
|
}
|
||||||
|
@ -99,4 +134,6 @@ func applyEnvironment(cfg *config, getenv func(string) string) {
|
||||||
if envPassword := getenv(envVarNetatmoPassword); envPassword != "" {
|
if envPassword := getenv(envVarNetatmoPassword); envPassword != "" {
|
||||||
cfg.Netatmo.Password = envPassword
|
cfg.Netatmo.Password = envPassword
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
netatmo "github.com/exzz/netatmo-api-go"
|
netatmo "github.com/exzz/netatmo-api-go"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseConfig(t *testing.T) {
|
func TestParseConfig(t *testing.T) {
|
||||||
|
@ -37,7 +38,8 @@ func TestParseConfig(t *testing.T) {
|
||||||
},
|
},
|
||||||
env: map[string]string{},
|
env: map[string]string{},
|
||||||
wantConfig: config{
|
wantConfig: config{
|
||||||
Addr: defaultConfig.Addr,
|
Addr: defaultConfig.Addr,
|
||||||
|
LogLevel: logLevel(logrus.InfoLevel),
|
||||||
Netatmo: netatmo.Config{
|
Netatmo: netatmo.Config{
|
||||||
ClientID: "id",
|
ClientID: "id",
|
||||||
ClientSecret: "secret",
|
ClientSecret: "secret",
|
||||||
|
@ -54,13 +56,15 @@ func TestParseConfig(t *testing.T) {
|
||||||
},
|
},
|
||||||
env: map[string]string{
|
env: map[string]string{
|
||||||
envVarListenAddress: ":8080",
|
envVarListenAddress: ":8080",
|
||||||
|
envVarLogLevel: "debug",
|
||||||
envVarNetatmoClientID: "id",
|
envVarNetatmoClientID: "id",
|
||||||
envVarNetatmoClientSecret: "secret",
|
envVarNetatmoClientSecret: "secret",
|
||||||
envVarNetatmoUsername: "username",
|
envVarNetatmoUsername: "username",
|
||||||
envVarNetatmoPassword: "password",
|
envVarNetatmoPassword: "password",
|
||||||
},
|
},
|
||||||
wantConfig: config{
|
wantConfig: config{
|
||||||
Addr: ":8080",
|
Addr: ":8080",
|
||||||
|
LogLevel: logLevel(logrus.DebugLevel),
|
||||||
Netatmo: netatmo.Config{
|
Netatmo: netatmo.Config{
|
||||||
ClientID: "id",
|
ClientID: "id",
|
||||||
ClientSecret: "secret",
|
ClientSecret: "secret",
|
||||||
|
|
1
main.go
1
main.go
|
@ -27,6 +27,7 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error in configuration: %s", err)
|
log.Fatalf("Error in configuration: %s", err)
|
||||||
}
|
}
|
||||||
|
log.SetLevel(logrus.Level(cfg.LogLevel))
|
||||||
|
|
||||||
log.Infof("Login as %s", cfg.Netatmo.Username)
|
log.Infof("Login as %s", cfg.Netatmo.Username)
|
||||||
client, err := netatmo.NewClient(cfg.Netatmo)
|
client, err := netatmo.NewClient(cfg.Netatmo)
|
||||||
|
|
Loading…
Reference in a new issue