diff --git a/config/config.go b/config/config.go index 8b75471..6cd2952 100644 --- a/config/config.go +++ b/config/config.go @@ -1,10 +1,11 @@ package config import ( + log "github.com/sirupsen/logrus" "io" "io/ioutil" - yaml "gopkg.in/yaml.v2" + "gopkg.in/yaml.v2" ) // Config represents the configuration for the exporter @@ -40,9 +41,9 @@ type Device struct { Srv SrvRecord `yaml:"srv,omitempty"` User string `yaml:"user"` Password string `yaml:"password"` - Port string `yaml:"port"` - EnableTLS bool `yaml:"tls"` - InsecureTLS bool `yaml:"insecure"` + Port string `yaml:"port,omitempty"` + EnableTLS bool `yaml:"tls,omitempty"` + InsecureTLS bool `yaml:"insecure,omitempty"` } type SrvRecord struct { @@ -54,7 +55,7 @@ type DnsServer struct { Port int `yaml:"port"` } -// Load reads YAML from reader and unmashals in Config +// Load reads YAML from reader and unmarshalls in Config func Load(r io.Reader) (*Config, error) { b, err := ioutil.ReadAll(r) if err != nil { @@ -69,3 +70,15 @@ func Load(r io.Reader) (*Config, error) { return c, nil } + +func (d *Device) UnmarshalYAML(unmarshal func(interface{}) error) error { + type inputDevice Device + defaults := &inputDevice{Port: "8728", EnableTLS: false, InsecureTLS: false} + err := unmarshal(defaults) + if err != nil { + log.WithError(err).Error("Device unmarshal error") + return err + } + *d = (Device)(*defaults) + return nil +} diff --git a/config/config.test.yml b/config/config.test.yml index 6e06dda..9c445c6 100644 --- a/config/config.test.yml +++ b/config/config.test.yml @@ -6,8 +6,8 @@ devices: - name: test2 address: 192.168.2.1 user: test - password: 123 - port: 324 + password: '123' + port: '324' tls: true insecure: true diff --git a/config/config_test.go b/config/config_test.go index a0687c9..b7328e1 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -19,7 +19,7 @@ func TestShouldParse(t *testing.T) { assertDevice("test1", "192.168.1.1", "foo", "bar", c.Devices[0], t) assertDevice("test2", "192.168.2.1", "test", "123", c.Devices[1], t) - assertDeviceConnection("", false, false, c.Devices[0], t) + assertDeviceConnection("8728", false, false, c.Devices[0], t) assertDeviceConnection("324", true, true, c.Devices[1], t) assertFeature("BGP", c.Features.BGP, t) assertFeature("Conntrack", c.Features.Conntrack, t)