diff --git a/floaty.example.yaml b/floaty.example.yaml new file mode 100644 index 0000000..f641bb3 --- /dev/null +++ b/floaty.example.yaml @@ -0,0 +1,5 @@ +provider: cloudscale +cloudscale: + token: fake-token +managed-addresses: + - 192.0.2.10 diff --git a/go.mod b/go.mod index e8e61bc..3175ca5 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 gopkg.in/yaml.v2 v2.4.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -24,5 +25,4 @@ require ( github.com/vishvananda/netlink v1.1.0 // indirect github.com/vishvananda/netns v0.0.4 // indirect golang.org/x/sys v0.17.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/notify_config.go b/notify_config.go index c6edcab..a6b2eeb 100644 --- a/notify_config.go +++ b/notify_config.go @@ -3,11 +3,11 @@ package main import ( "errors" "fmt" - "io/ioutil" "net/url" + "os" "time" - yaml "gopkg.in/yaml.v2" + yaml "gopkg.in/yaml.v3" ) const ( @@ -51,12 +51,17 @@ func newNotifyConfig() notifyConfig { // Update configuration from a YAML file func (c *notifyConfig) ReadFromYAML(path string) error { - content, err := ioutil.ReadFile(path) + configreader, err := os.Open(path) if err != nil { return err } - return yaml.UnmarshalStrict(content, c) + decoder := yaml.NewDecoder(configreader) + // NOTE(sg): With gopkg.in/yaml.v3, we use the decoder API instead of + // `Unmarshal` so we can ensure that we get errors for unknown fields. + decoder.KnownFields(true) + + return decoder.Decode(c) } func (c notifyConfig) NewProvider() (elasticIPProvider, error) { diff --git a/notify_config_test.go b/notify_config_test.go new file mode 100644 index 0000000..fbf8015 --- /dev/null +++ b/notify_config_test.go @@ -0,0 +1,17 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestLoadConfig(t *testing.T) { + cfg, err := loadConfig("floaty.example.yaml", false) + assert.NoError(t, err) + assert.Equalf(t, "cloudscale", cfg.Provider, "error parsing provider from config file") + assert.Equalf(t, "fake-token", cfg.Cloudscale.Token, "error parsing cloudscale token from config file") + managedAddr := netAddress{} + managedAddr.UnmarshalText([]byte("192.0.2.10")) + assert.Equalf(t, []netAddress{managedAddr}, cfg.ManagedAddresses, "error parsing managed addresses from config file") +}