Move config code

This commit is contained in:
Robert Jacob 2020-06-27 17:50:36 +02:00
parent 9416c8b92f
commit 02bca43416
3 changed files with 27 additions and 24 deletions

View File

@ -1,4 +1,4 @@
package main package config
import ( import (
"errors" "errors"
@ -34,7 +34,7 @@ const (
) )
var ( var (
defaultConfig = config{ defaultConfig = Config{
Addr: ":9210", Addr: ":9210",
LogLevel: logLevel(logrus.InfoLevel), LogLevel: logLevel(logrus.InfoLevel),
RefreshInterval: defaultRefreshInterval, RefreshInterval: defaultRefreshInterval,
@ -69,7 +69,8 @@ func (l *logLevel) Set(value string) error {
return nil return nil
} }
type config struct { // Config contains the configuration options.
type Config struct {
Addr string Addr string
LogLevel logLevel LogLevel logLevel
RefreshInterval time.Duration RefreshInterval time.Duration
@ -77,7 +78,8 @@ type config struct {
Netatmo netatmo.Config Netatmo netatmo.Config
} }
func parseConfig(args []string, getenv func(string) string) (config, error) { // Parse takes the arguments and environment variables provided and creates the Config from that.
func Parse(args []string, getenv func(string) string) (Config, error) {
cfg := defaultConfig cfg := defaultConfig
if len(args) < 1 { if len(args) < 1 {
@ -96,37 +98,37 @@ func parseConfig(args []string, getenv func(string) string) (config, error) {
flagSet.Parse(args[1:]) flagSet.Parse(args[1:])
if err := applyEnvironment(&cfg, getenv); err != nil { if err := applyEnvironment(&cfg, getenv); err != nil {
return config{}, fmt.Errorf("error in environment: %s", err) return Config{}, fmt.Errorf("error in environment: %s", err)
} }
if len(cfg.Addr) == 0 { if len(cfg.Addr) == 0 {
return config{}, errNoListenAddress return Config{}, errNoListenAddress
} }
if len(cfg.Netatmo.ClientID) == 0 { if len(cfg.Netatmo.ClientID) == 0 {
return config{}, errNoNetatmoClientID return Config{}, errNoNetatmoClientID
} }
if len(cfg.Netatmo.ClientSecret) == 0 { if len(cfg.Netatmo.ClientSecret) == 0 {
return config{}, errNoNetatmoClientSecret return Config{}, errNoNetatmoClientSecret
} }
if len(cfg.Netatmo.Username) == 0 { if len(cfg.Netatmo.Username) == 0 {
return config{}, errNoNetatmoUsername return Config{}, errNoNetatmoUsername
} }
if len(cfg.Netatmo.Password) == 0 { if len(cfg.Netatmo.Password) == 0 {
return config{}, errNoNetatmoPassword return Config{}, errNoNetatmoPassword
} }
if cfg.StaleDuration < cfg.RefreshInterval { if cfg.StaleDuration < cfg.RefreshInterval {
return config{}, fmt.Errorf("stale duration smaller than refresh interval: %s < %s", cfg.StaleDuration, cfg.RefreshInterval) return Config{}, fmt.Errorf("stale duration smaller than refresh interval: %s < %s", cfg.StaleDuration, cfg.RefreshInterval)
} }
return cfg, nil return cfg, nil
} }
func applyEnvironment(cfg *config, getenv func(string) string) error { 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
} }

View File

@ -1,4 +1,4 @@
package main package config
import ( import (
"reflect" "reflect"
@ -14,14 +14,14 @@ func TestParseConfig(t *testing.T) {
name string name string
args []string args []string
env map[string]string env map[string]string
wantConfig config wantConfig Config
wantErr error wantErr error
}{ }{
{ {
name: "no args", name: "no args",
args: []string{}, args: []string{},
env: map[string]string{}, env: map[string]string{},
wantConfig: config{}, wantConfig: Config{},
wantErr: errNoBinaryName, wantErr: errNoBinaryName,
}, },
{ {
@ -38,7 +38,7 @@ func TestParseConfig(t *testing.T) {
"password", "password",
}, },
env: map[string]string{}, env: map[string]string{},
wantConfig: config{ wantConfig: Config{
Addr: defaultConfig.Addr, Addr: defaultConfig.Addr,
LogLevel: logLevel(logrus.InfoLevel), LogLevel: logLevel(logrus.InfoLevel),
RefreshInterval: defaultRefreshInterval, RefreshInterval: defaultRefreshInterval,
@ -67,7 +67,7 @@ func TestParseConfig(t *testing.T) {
envVarNetatmoUsername: "username", envVarNetatmoUsername: "username",
envVarNetatmoPassword: "password", envVarNetatmoPassword: "password",
}, },
wantConfig: config{ wantConfig: Config{
Addr: ":8080", Addr: ":8080",
LogLevel: logLevel(logrus.DebugLevel), LogLevel: logLevel(logrus.DebugLevel),
RefreshInterval: 5 * time.Minute, RefreshInterval: 5 * time.Minute,
@ -97,7 +97,7 @@ func TestParseConfig(t *testing.T) {
"password", "password",
}, },
env: map[string]string{}, env: map[string]string{},
wantConfig: config{ wantConfig: Config{
Addr: defaultConfig.Addr, Addr: defaultConfig.Addr,
Netatmo: netatmo.Config{ Netatmo: netatmo.Config{
ClientID: "id", ClientID: "id",
@ -120,7 +120,7 @@ func TestParseConfig(t *testing.T) {
"password", "password",
}, },
env: map[string]string{}, env: map[string]string{},
wantConfig: config{ wantConfig: Config{
Addr: defaultConfig.Addr, Addr: defaultConfig.Addr,
Netatmo: netatmo.Config{ Netatmo: netatmo.Config{
ClientID: "id", ClientID: "id",
@ -143,7 +143,7 @@ func TestParseConfig(t *testing.T) {
"password", "password",
}, },
env: map[string]string{}, env: map[string]string{},
wantConfig: config{ wantConfig: Config{
Addr: defaultConfig.Addr, Addr: defaultConfig.Addr,
Netatmo: netatmo.Config{ Netatmo: netatmo.Config{
ClientID: "id", ClientID: "id",
@ -166,7 +166,7 @@ func TestParseConfig(t *testing.T) {
"password", "password",
}, },
env: map[string]string{}, env: map[string]string{},
wantConfig: config{ wantConfig: Config{
Addr: defaultConfig.Addr, Addr: defaultConfig.Addr,
Netatmo: netatmo.Config{ Netatmo: netatmo.Config{
ClientID: "id", ClientID: "id",
@ -189,7 +189,7 @@ func TestParseConfig(t *testing.T) {
"username", "username",
}, },
env: map[string]string{}, env: map[string]string{},
wantConfig: config{ wantConfig: Config{
Addr: defaultConfig.Addr, Addr: defaultConfig.Addr,
Netatmo: netatmo.Config{ Netatmo: netatmo.Config{
ClientID: "id", ClientID: "id",
@ -212,7 +212,7 @@ func TestParseConfig(t *testing.T) {
return tt.env[key] return tt.env[key]
} }
config, err := parseConfig(tt.args, getenv) config, err := Parse(tt.args, getenv)
if err != tt.wantErr { if err != tt.wantErr {
t.Errorf("got error %q, want %q", err, tt.wantErr) t.Errorf("got error %q, want %q", err, tt.wantErr)

View File

@ -8,6 +8,7 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/xperimental/netatmo-exporter/internal/config"
) )
var ( var (
@ -23,7 +24,7 @@ var (
) )
func main() { func main() {
cfg, err := parseConfig(os.Args, os.Getenv) cfg, err := config.Parse(os.Args, os.Getenv)
if err != nil { if err != nil {
log.Fatalf("Error in configuration: %s", err) log.Fatalf("Error in configuration: %s", err)
} }