1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-11-22 08:25:38 +00:00

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

View file

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