Change toml parser

This commit is contained in:
Julian Kornberger 2022-03-28 04:41:32 +02:00
parent 4261ad2288
commit 578a45c19f
4 changed files with 14 additions and 20 deletions

View File

@ -2,10 +2,9 @@ package cmd
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"github.com/naoina/toml" "github.com/BurntSushi/toml"
"github.com/FreifunkBremen/yanic/database" "github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/respond" "github.com/FreifunkBremen/yanic/respond"
@ -40,12 +39,13 @@ func loadConfig() *Config {
func ReadConfigFile(path string) (config *Config, err error) { func ReadConfigFile(path string) (config *Config, err error) {
config = &Config{} config = &Config{}
file, err := ioutil.ReadFile(path) file, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close()
err = toml.Unmarshal(file, config) _, err = toml.NewDecoder(file).Decode(config)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -5,14 +5,16 @@ import (
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestReadConfig(t *testing.T) { func TestReadConfig(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t)
config, err := ReadConfigFile("../config_example.toml") config, err := ReadConfigFile("../config_example.toml")
assert.NoError(err) require.NoError(err)
assert.NotNil(config) require.NotNil(config)
assert.True(config.Respondd.Enable) assert.True(config.Respondd.Enable)
assert.Equal("br-ffhb", config.Respondd.Interfaces[0].InterfaceName) assert.Equal("br-ffhb", config.Respondd.Interfaces[0].InterfaceName)
@ -26,7 +28,7 @@ func TestReadConfig(t *testing.T) {
// Test output plugins // Test output plugins
assert.Len(config.Nodes.Output, 5) assert.Len(config.Nodes.Output, 5)
outputs := config.Nodes.Output["meshviewer"].([]interface{}) outputs := config.Nodes.Output["meshviewer"].([]map[string]interface{})
assert.Len(outputs, 1) assert.Len(outputs, 1)
meshviewer := outputs[0] meshviewer := outputs[0]
@ -41,10 +43,8 @@ func TestReadConfig(t *testing.T) {
}, meshviewer) }, meshviewer)
_, err = ReadConfigFile("testdata/config_invalid.toml") _, err = ReadConfigFile("testdata/config_invalid.toml")
assert.Error(err, "not unmarshalable") assert.EqualError(err, "toml: line 2: expected '.' or '=', but got '\\n' instead")
assert.Contains(err.Error(), "invalid TOML syntax")
_, err = ReadConfigFile("testdata/adsa.toml") _, err = ReadConfigFile("testdata/adsa.toml")
assert.Error(err, "not found able") assert.EqualError(err, "open testdata/adsa.toml: no such file or directory")
assert.Contains(err.Error(), "no such file or directory")
} }

4
go.mod
View File

@ -3,12 +3,12 @@ module github.com/FreifunkBremen/yanic
go 1.17 go 1.17
require ( require (
github.com/BurntSushi/toml v1.0.0
github.com/NYTimes/gziphandler v1.1.1 github.com/NYTimes/gziphandler v1.1.1
github.com/bdlm/log v0.1.20 github.com/bdlm/log v0.1.20
github.com/bdlm/std v1.0.1 github.com/bdlm/std v1.0.1
github.com/fgrosse/graphigo v0.0.0-20151222101953-5770fe631d9a github.com/fgrosse/graphigo v0.0.0-20151222101953-5770fe631d9a
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
github.com/naoina/toml v0.1.1
github.com/paulmach/go.geojson v1.4.0 github.com/paulmach/go.geojson v1.4.0
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.4.0 github.com/spf13/cobra v1.4.0
@ -20,8 +20,6 @@ require (
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/naoina/go-stringutil v0.1.0 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.19.0 // indirect github.com/onsi/gomega v1.19.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect

8
go.sum
View File

@ -1,3 +1,5 @@
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I= github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c= github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/bdlm/log v0.1.20 h1:fSxBuBSHz+DkxPSFlaVcPiep20mCYUJZ5azUynkjhfA= github.com/bdlm/log v0.1.20 h1:fSxBuBSHz+DkxPSFlaVcPiep20mCYUJZ5azUynkjhfA=
@ -37,12 +39,6 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c h1:qSHzRbhzK8RdXOsAdfDgO49TtqC1oZ+acxPrkfTxcCs= github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c h1:qSHzRbhzK8RdXOsAdfDgO49TtqC1oZ+acxPrkfTxcCs=
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks=
github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0=
github.com/naoina/toml v0.1.1 h1:PT/lllxVVN0gzzSqSlHEmP8MJB4MY2U7STGxiouV4X8=
github.com/naoina/toml v0.1.1/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=