From 9cfe9679c9b05d7180a1b31fb7d7145cc445a20f Mon Sep 17 00:00:00 2001 From: Matthias Hannig Date: Tue, 16 May 2017 18:26:07 +0200 Subject: [PATCH] started with config loading --- backend/config.go | 71 ++++++++++++++++++++++++++++++++++++++++++ backend/config_test.go | 12 +++++++ 2 files changed, 83 insertions(+) create mode 100644 backend/config.go create mode 100644 backend/config_test.go diff --git a/backend/config.go b/backend/config.go new file mode 100644 index 0000000..561a1b7 --- /dev/null +++ b/backend/config.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" + "log" + "strings" + + "github.com/BurntSushi/toml" + "github.com/imdario/mergo" +) + +type ServerConfig struct { + Listen string `toml:"listen_http"` +} + +type UiConfig struct { + ShowLastReboot bool `toml:"rs_show_last_reboot"` +} + +type SourceConfig struct { + Name string +} + +type Config struct { + Server ServerConfig + Ui UiConfig + + Sources []SourceConfig +} + +// Try to load configfiles as specified in the files +// list. For example: +// +// ./etc/alicelg/alice.conf +// /etc/alicelg/alice.conf +// ./etc/alicelg/alice.local.conf +// +func LoadConfigs(configFiles []string) (*Config, error) { + config := &Config{} + hasConfig := false + var confError error + + for _, filename := range configFiles { + tmp := &Config{} + _, err := toml.DecodeFile(filename, tmp) + if err != nil { + continue + } else { + log.Println("Using config file:", filename) + hasConfig = true + // Merge configs + if err := mergo.Merge(config, tmp); err != nil { + return nil, err + } + } + } + + if !hasConfig { + confError = fmt.Errorf("Could not load any config file") + } + + return config, confError +} + +func ConfigOptions(filename string) []string { + return []string{ + strings.Join([]string{"/", filename}, ""), + strings.Join([]string{"./", filename}, ""), + strings.Replace(filename, ".conf", ".local.conf", 1), + } +} diff --git a/backend/config_test.go b/backend/config_test.go new file mode 100644 index 0000000..b376dce --- /dev/null +++ b/backend/config_test.go @@ -0,0 +1,12 @@ +package main + +import ( + "testing" +) + +// Test configuration loading and parsing +// using the default config + +func TestLoadConfigs(t *testing.T) { + +}