check: populate the Result set

allowing the caller to display the results as desired

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2016-04-05 11:44:55 -04:00
parent 0898fd6d90
commit b7724b906b
2 changed files with 19 additions and 12 deletions

View file

@ -8,10 +8,20 @@ import (
)
type Result struct {
Failures []Failure
// XXX perhaps this is a list of the failed files and keywords?
}
var ErrNotAllClear = fmt.Errorf("some keyword check failed validation")
type Failure struct {
Path string
Keyword string
Expected string
Got string
}
func (f Failure) String() string {
return fmt.Sprintf("%q: keyword %q: expected %s; got %s", f.Path, f.Keyword, f.Expected, f.Got)
}
func Check(root string, dh *DirectoryHierarchy) (*Result, error) {
creator := dhCreator{DH: dh}
@ -25,7 +35,7 @@ func Check(root string, dh *DirectoryHierarchy) (*Result, error) {
}
sort.Sort(byPos(creator.DH.Entries))
var failed bool
var result Result
for _, e := range creator.DH.Entries {
switch e.Type {
case SpecialType:
@ -57,16 +67,11 @@ func Check(root string, dh *DirectoryHierarchy) (*Result, error) {
return nil, err
}
if string(kv) != curKeyVal {
failed = true
fmt.Printf("%q: keyword %q: expected %s; got %s\n", e.Path(), kv.Keyword(), kv.Value(), KeyVal(curKeyVal).Value())
failure := Failure{Path: e.Path(), Keyword: kv.Keyword(), Expected: kv.Value(), Got: KeyVal(curKeyVal).Value()}
result.Failures = append(result.Failures, failure)
}
}
}
}
if failed {
return nil, ErrNotAllClear
}
return nil, nil
return &result, nil
}

View file

@ -98,8 +98,10 @@ func main() {
isErr = true
return
}
if res != nil {
fmt.Printf("%#v\n", res)
if res != nil && len(res.Failures) > 0 {
for _, failure := range res.Failures {
fmt.Println(failure)
}
}
}
}