From f5b24dfab25921eb76abcc5d02fc90174411aa27 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Sat, 13 Jan 2018 14:41:49 +0100 Subject: [PATCH] Move config structs to packages --- cmd/config.go | 33 ++++++++++++++-- {runtime/config => cmd}/config_test.go | 8 ++-- cmd/testdata/config_invalid.toml | 1 + respond/config.go | 12 ++++++ runtime/config/config.go | 46 ---------------------- runtime/config/testdata/config_failed.toml | 1 - webserver/config.go | 7 ++++ 7 files changed, 54 insertions(+), 54 deletions(-) rename {runtime/config => cmd}/config_test.go (89%) create mode 100644 cmd/testdata/config_invalid.toml create mode 100644 respond/config.go delete mode 100644 runtime/config/config.go delete mode 100644 runtime/config/testdata/config_failed.toml create mode 100644 webserver/config.go diff --git a/cmd/config.go b/cmd/config.go index b0215a8..b7b184b 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -2,14 +2,24 @@ package cmd import ( "fmt" + "io/ioutil" "os" "github.com/FreifunkBremen/yanic/database" "github.com/FreifunkBremen/yanic/respond" "github.com/FreifunkBremen/yanic/runtime" - "github.com/FreifunkBremen/yanic/runtime/config" + "github.com/FreifunkBremen/yanic/webserver" + "github.com/naoina/toml" ) +// Config represents the whole configuration +type Config struct { + Respondd respond.Config + Webserver webserver.Config + Nodes runtime.NodesConfig + Database database.Config +} + var ( configPath string collector *respond.Collector @@ -17,11 +27,28 @@ var ( nodes *runtime.Nodes ) -func loadConfig() *config.Config { - config, err := config.ReadConfigFile(configPath) +func loadConfig() *Config { + config, err := ReadConfigFile(configPath) if err != nil { fmt.Fprintln(os.Stderr, "unable to load config file:", err) os.Exit(2) } return config } + +// ReadConfigFile reads a config model from path of a yml file +func ReadConfigFile(path string) (config *Config, err error) { + config = &Config{} + + file, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + + err = toml.Unmarshal(file, config) + if err != nil { + return nil, err + } + + return +} diff --git a/runtime/config/config_test.go b/cmd/config_test.go similarity index 89% rename from runtime/config/config_test.go rename to cmd/config_test.go index 77cd389..16fb36c 100644 --- a/runtime/config/config_test.go +++ b/cmd/config_test.go @@ -1,4 +1,4 @@ -package config +package cmd import ( "testing" @@ -10,8 +10,8 @@ import ( func TestReadConfig(t *testing.T) { assert := assert.New(t) - config, err := ReadConfigFile("../../config_example.toml") - assert.NoError(err, "no error during reading") + config, err := ReadConfigFile("../config_example.toml") + assert.NoError(err) assert.NotNil(config) assert.True(config.Respondd.Enable) @@ -42,7 +42,7 @@ func TestReadConfig(t *testing.T) { graphitedb = dbs[0] assert.Equal(graphitedb["address"], "localhost:2003") - _, err = ReadConfigFile("testdata/config_failed.toml") + _, err = ReadConfigFile("testdata/config_invalid.toml") assert.Error(err, "not unmarshalable") assert.Contains(err.Error(), "Near line ") diff --git a/cmd/testdata/config_invalid.toml b/cmd/testdata/config_invalid.toml new file mode 100644 index 0000000..323fae0 --- /dev/null +++ b/cmd/testdata/config_invalid.toml @@ -0,0 +1 @@ +foobar diff --git a/respond/config.go b/respond/config.go new file mode 100644 index 0000000..b221fa1 --- /dev/null +++ b/respond/config.go @@ -0,0 +1,12 @@ +package respond + +import "github.com/FreifunkBremen/yanic/lib/duration" + +type Config struct { + Enable bool `toml:"enable"` + Synchronize duration.Duration `toml:"synchronize"` + Interfaces []string `toml:"interfaces"` + Sites []string `toml:"sites"` + Port int `toml:"port"` + CollectInterval duration.Duration `toml:"collect_interval"` +} diff --git a/runtime/config/config.go b/runtime/config/config.go deleted file mode 100644 index fde1345..0000000 --- a/runtime/config/config.go +++ /dev/null @@ -1,46 +0,0 @@ -package config - -import ( - "io/ioutil" - - "github.com/BurntSushi/toml" - "github.com/FreifunkBremen/yanic/database" - "github.com/FreifunkBremen/yanic/lib/duration" - "github.com/FreifunkBremen/yanic/runtime" -) - -//Config the config File of this daemon -type Config struct { - Respondd struct { - Enable bool `toml:"enable"` - Synchronize duration.Duration `toml:"synchronize"` - Interfaces []string `toml:"interfaces"` - Sites []string `toml:"sites"` - Port int `toml:"port"` - CollectInterval duration.Duration `toml:"collect_interval"` - } - Webserver struct { - Enable bool `toml:"enable"` - Bind string `toml:"bind"` - Webroot string `toml:"webroot"` - } - Nodes runtime.NodesConfig - Database database.Config -} - -// ReadConfigFile reads a config model from path of a yml file -func ReadConfigFile(path string) (config *Config, err error) { - config = &Config{} - - file, err := ioutil.ReadFile(path) - if err != nil { - return nil, err - } - - err = toml.Unmarshal(file, config) - if err != nil { - return nil, err - } - - return -} diff --git a/runtime/config/testdata/config_failed.toml b/runtime/config/testdata/config_failed.toml deleted file mode 100644 index 0247eeb..0000000 --- a/runtime/config/testdata/config_failed.toml +++ /dev/null @@ -1 +0,0 @@ -asdas diff --git a/webserver/config.go b/webserver/config.go new file mode 100644 index 0000000..6edca0b --- /dev/null +++ b/webserver/config.go @@ -0,0 +1,7 @@ +package webserver + +type Config struct { + Enable bool `toml:"enable"` + Bind string `toml:"bind"` + Webroot string `toml:"webroot"` +}