From f435c4644abc167cdfe6bfe3d320ff840f468c09 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Fri, 28 Sep 2018 15:17:17 +0100 Subject: Allow the config directory to be specified --- go/internal/config/config.go | 19 +++++++++++++------ go/internal/config/config_test.go | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) (limited to 'go/internal/config') diff --git a/go/internal/config/config.go b/go/internal/config/config.go index 4069851..435cb29 100644 --- a/go/internal/config/config.go +++ b/go/internal/config/config.go @@ -26,24 +26,31 @@ type Config struct { } func New() (*Config, error) { - cfg := Config{} - dir, err := os.Getwd() if err != nil { return nil, err } - cfg.RootDir = dir - configBytes, err := ioutil.ReadFile(path.Join(cfg.RootDir, configFile)) + return NewFromDir(dir) +} + +func NewFromDir(dir string) (*Config, error) { + return newFromFile(path.Join(dir, configFile)) +} + +func newFromFile(filename string) (*Config, error) { + cfg := &Config{RootDir: path.Dir(filename)} + + configBytes, err := ioutil.ReadFile(filename) if err != nil { return nil, err } - if err := parseConfig(configBytes, &cfg); err != nil { + if err := parseConfig(configBytes, cfg); err != nil { return nil, err } - return &cfg, nil + return cfg, nil } // parseConfig expects YAML data in configBytes and a Config instance with RootDir set. diff --git a/go/internal/config/config_test.go b/go/internal/config/config_test.go index 368ab29..87a582f 100644 --- a/go/internal/config/config_test.go +++ b/go/internal/config/config_test.go @@ -6,7 +6,7 @@ import ( "testing" ) -func TestConfigLogFile(t *testing.T) { +func TestParseConfig(t *testing.T) { testRoot := "/foo/bar" testCases := []struct { yaml string -- cgit v1.2.1