diff --git a/config.go b/config.go index 4824cc7..66ea2da 100644 --- a/config.go +++ b/config.go @@ -1,5 +1,12 @@ package main +import ( + "encoding/json" + "io/ioutil" +) + +var f interface{} + type Rule struct { Path string Run string @@ -7,5 +14,24 @@ type Rule struct { func ParseConfig(path string) (rules []*Rule, err error) { rules = make([]*Rule, 0) - return + + c, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + + err = json.Unmarshal(c, &f) + if err != nil { + return nil, err + } + + config := f.(map[string]interface{}) + for path, v := range config { + attributes := v.(map[string]interface{}) + run := attributes["run"].(string) + + rules = append(rules, &Rule{path, run}) + } + + return rules, nil } diff --git a/config_test.go b/config_test.go index 759ae2e..5ee2604 100644 --- a/config_test.go +++ b/config_test.go @@ -4,9 +4,39 @@ import ( "testing" ) +var parseTests = []struct { + index int + expectedPath string + expectedRun string +}{ + { + 0, + "/tmp/foo", + "/usr/local/bar/foobar", + }, + { + 1, + "/tmp/bar", + "/usr/local/bar/barfoo", + }, +} + func TestParseConfig(t *testing.T) { - _, err := ParseConfig(fixtures + "/example_config.json") + rules, err := ParseConfig(fixtures + "/example_config.json") if err != nil { t.Fatal(err) } + if len(rules) != 2 { + t.Fatal("Did not include all rules") + } + + for _, pt := range parseTests { + actualPath := rules[pt.index].Path + actualRun := rules[pt.index].Run + + if actualPath != pt.expectedPath && actualRun != pt.expectedRun { + t.Errorf("ACTUALPATH %v - EXPECTEDPATH %v, ACTUALRUN %v - EXPECTEDRUN %v", actualPath, pt.expectedPath, actualRun, pt.expectedRun) + continue + } + } } diff --git a/fixtures/example_config.json b/fixtures/example_config.json index d2d25e6..336e3d2 100644 --- a/fixtures/example_config.json +++ b/fixtures/example_config.json @@ -3,6 +3,6 @@ "run": "/usr/local/bar/foobar" }, "/tmp/bar": { - "run": "/usr/local/bar/foobar" + "run": "/usr/local/bar/barfoo" } }