~nvkv/amitm

decc8d65f3474f58f30c63bd0ee9924da635ef78 — Semyon Novikov 3 months ago e0de993
Untangle config reading from reading file
2 files changed, 19 insertions(+), 6 deletions(-)

M internal/config/v1/config.go
M internal/config/v1/config_test.go
M internal/config/v1/config.go => internal/config/v1/config.go +7 -3
@@ 21,13 21,17 @@ type Config struct {
	Rules     []Rule
}

func ReadConfig(path string) (*Config, error) {
func ReadConfigFile(path string) (*Config, error) {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}
	return NewConfig(data)
}

func NewConfig(data []byte) (*Config, error) {
	cfg := &Config{}
	err = toml.Unmarshal(data, cfg)
	err := toml.Unmarshal(data, cfg)
	if err != nil {
		return nil, err
	}


@@ 36,4 40,4 @@ func ReadConfig(path string) (*Config, error) {
		cfg.actionmap[rule.Action] = append(cfg.actionmap[rule.Action], &rule)
	}
	return cfg, nil
}
}
\ No newline at end of file

M internal/config/v1/config_test.go => internal/config/v1/config_test.go +12 -3
@@ 6,9 6,18 @@ import (
)

func TestReadConfig(t *testing.T) {
	cfg, err := ReadConfig("../../../examples/amitm.toml")
	fixture := []byte(`
[[rules]]
name = "Testing tests"
glob = "test*.test"
action = "put"

[[rules.pipeline]]
exec = "test -t $file"`)

	cfg, err := NewConfig(fixture)
	fmt.Printf("cfg: %+v\n", cfg.actionmap)
	if err != nil {
		t.Errorf("Can't read example.toml file: %s", err)
	if err != nil || cfg == nil {
		t.Errorf("Can't read %s file: %s", fixture, err)
	}
}