mirror of
https://github.com/steinhobelgruen/netatmo-exporter.git
synced 2024-11-24 02:13:55 +00:00
Move config code
This commit is contained in:
parent
9416c8b92f
commit
02bca43416
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
@ -34,7 +34,7 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
defaultConfig = config{
|
||||
defaultConfig = Config{
|
||||
Addr: ":9210",
|
||||
LogLevel: logLevel(logrus.InfoLevel),
|
||||
RefreshInterval: defaultRefreshInterval,
|
||||
|
@ -69,7 +69,8 @@ func (l *logLevel) Set(value string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
type config struct {
|
||||
// Config contains the configuration options.
|
||||
type Config struct {
|
||||
Addr string
|
||||
LogLevel logLevel
|
||||
RefreshInterval time.Duration
|
||||
|
@ -77,7 +78,8 @@ type config struct {
|
|||
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
|
||||
|
||||
if len(args) < 1 {
|
||||
|
@ -96,37 +98,37 @@ func parseConfig(args []string, getenv func(string) string) (config, error) {
|
|||
flagSet.Parse(args[1:])
|
||||
|
||||
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 {
|
||||
return config{}, errNoListenAddress
|
||||
return Config{}, errNoListenAddress
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.ClientID) == 0 {
|
||||
return config{}, errNoNetatmoClientID
|
||||
return Config{}, errNoNetatmoClientID
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.ClientSecret) == 0 {
|
||||
return config{}, errNoNetatmoClientSecret
|
||||
return Config{}, errNoNetatmoClientSecret
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.Username) == 0 {
|
||||
return config{}, errNoNetatmoUsername
|
||||
return Config{}, errNoNetatmoUsername
|
||||
}
|
||||
|
||||
if len(cfg.Netatmo.Password) == 0 {
|
||||
return config{}, errNoNetatmoPassword
|
||||
return Config{}, errNoNetatmoPassword
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func applyEnvironment(cfg *config, getenv func(string) string) error {
|
||||
func applyEnvironment(cfg *Config, getenv func(string) string) error {
|
||||
if envAddr := getenv(envVarListenAddress); envAddr != "" {
|
||||
cfg.Addr = envAddr
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
@ -14,14 +14,14 @@ func TestParseConfig(t *testing.T) {
|
|||
name string
|
||||
args []string
|
||||
env map[string]string
|
||||
wantConfig config
|
||||
wantConfig Config
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "no args",
|
||||
args: []string{},
|
||||
env: map[string]string{},
|
||||
wantConfig: config{},
|
||||
wantConfig: Config{},
|
||||
wantErr: errNoBinaryName,
|
||||
},
|
||||
{
|
||||
|
@ -38,7 +38,7 @@ func TestParseConfig(t *testing.T) {
|
|||
"password",
|
||||
},
|
||||
env: map[string]string{},
|
||||
wantConfig: config{
|
||||
wantConfig: Config{
|
||||
Addr: defaultConfig.Addr,
|
||||
LogLevel: logLevel(logrus.InfoLevel),
|
||||
RefreshInterval: defaultRefreshInterval,
|
||||
|
@ -67,7 +67,7 @@ func TestParseConfig(t *testing.T) {
|
|||
envVarNetatmoUsername: "username",
|
||||
envVarNetatmoPassword: "password",
|
||||
},
|
||||
wantConfig: config{
|
||||
wantConfig: Config{
|
||||
Addr: ":8080",
|
||||
LogLevel: logLevel(logrus.DebugLevel),
|
||||
RefreshInterval: 5 * time.Minute,
|
||||
|
@ -97,7 +97,7 @@ func TestParseConfig(t *testing.T) {
|
|||
"password",
|
||||
},
|
||||
env: map[string]string{},
|
||||
wantConfig: config{
|
||||
wantConfig: Config{
|
||||
Addr: defaultConfig.Addr,
|
||||
Netatmo: netatmo.Config{
|
||||
ClientID: "id",
|
||||
|
@ -120,7 +120,7 @@ func TestParseConfig(t *testing.T) {
|
|||
"password",
|
||||
},
|
||||
env: map[string]string{},
|
||||
wantConfig: config{
|
||||
wantConfig: Config{
|
||||
Addr: defaultConfig.Addr,
|
||||
Netatmo: netatmo.Config{
|
||||
ClientID: "id",
|
||||
|
@ -143,7 +143,7 @@ func TestParseConfig(t *testing.T) {
|
|||
"password",
|
||||
},
|
||||
env: map[string]string{},
|
||||
wantConfig: config{
|
||||
wantConfig: Config{
|
||||
Addr: defaultConfig.Addr,
|
||||
Netatmo: netatmo.Config{
|
||||
ClientID: "id",
|
||||
|
@ -166,7 +166,7 @@ func TestParseConfig(t *testing.T) {
|
|||
"password",
|
||||
},
|
||||
env: map[string]string{},
|
||||
wantConfig: config{
|
||||
wantConfig: Config{
|
||||
Addr: defaultConfig.Addr,
|
||||
Netatmo: netatmo.Config{
|
||||
ClientID: "id",
|
||||
|
@ -189,7 +189,7 @@ func TestParseConfig(t *testing.T) {
|
|||
"username",
|
||||
},
|
||||
env: map[string]string{},
|
||||
wantConfig: config{
|
||||
wantConfig: Config{
|
||||
Addr: defaultConfig.Addr,
|
||||
Netatmo: netatmo.Config{
|
||||
ClientID: "id",
|
||||
|
@ -212,7 +212,7 @@ func TestParseConfig(t *testing.T) {
|
|||
return tt.env[key]
|
||||
}
|
||||
|
||||
config, err := parseConfig(tt.args, getenv)
|
||||
config, err := Parse(tt.args, getenv)
|
||||
|
||||
if err != tt.wantErr {
|
||||
t.Errorf("got error %q, want %q", err, tt.wantErr)
|
3
main.go
3
main.go
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/xperimental/netatmo-exporter/internal/config"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -23,7 +24,7 @@ var (
|
|||
)
|
||||
|
||||
func main() {
|
||||
cfg, err := parseConfig(os.Args, os.Getenv)
|
||||
cfg, err := config.Parse(os.Args, os.Getenv)
|
||||
if err != nil {
|
||||
log.Fatalf("Error in configuration: %s", err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue