From b7724b906b1283dc8eb403ac4bb4bd009123f897 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 5 Apr 2016 11:44:55 -0400 Subject: [PATCH 1/2] check: populate the Result set allowing the caller to display the results as desired Signed-off-by: Vincent Batts --- check.go | 25 +++++++++++++++---------- cmd/gomtree/main.go | 6 ++++-- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/check.go b/check.go index 54ca338..bf26ab1 100644 --- a/check.go +++ b/check.go @@ -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 } diff --git a/cmd/gomtree/main.go b/cmd/gomtree/main.go index 347e383..f0c9e1b 100644 --- a/cmd/gomtree/main.go +++ b/cmd/gomtree/main.go @@ -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) + } } } } From 05f9b75a19addf58f53e840bdc70772b58f8d807 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 5 Apr 2016 16:20:04 -0400 Subject: [PATCH 2/2] check: fix the checking of a hierarchy Signed-off-by: Vincent Batts --- check.go | 7 +++++-- cmd/gomtree/main.go | 1 + hierarchy.go | 1 + parse.go | 4 ++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/check.go b/check.go index bf26ab1..7901402 100644 --- a/check.go +++ b/check.go @@ -7,11 +7,12 @@ import ( "sort" ) +// Result of a Check type Result struct { - Failures []Failure - // XXX perhaps this is a list of the failed files and keywords? + Failures []Failure // list of any failures in the Check } +// Failure of a particular keyword for a path type Failure struct { Path string Keyword string @@ -19,10 +20,12 @@ type Failure struct { Got string } +// String returns a "pretty" formatting for a Failure func (f Failure) String() string { return fmt.Sprintf("%q: keyword %q: expected %s; got %s", f.Path, f.Keyword, f.Expected, f.Got) } +// Check a root directory path for a DirectoryHierarchy func Check(root string, dh *DirectoryHierarchy) (*Result, error) { creator := dhCreator{DH: dh} curDir, err := os.Getwd() diff --git a/cmd/gomtree/main.go b/cmd/gomtree/main.go index f0c9e1b..4f7cb48 100644 --- a/cmd/gomtree/main.go +++ b/cmd/gomtree/main.go @@ -99,6 +99,7 @@ func main() { return } if res != nil && len(res.Failures) > 0 { + defer os.Exit(1) for _, failure := range res.Failures { fmt.Println(failure) } diff --git a/hierarchy.go b/hierarchy.go index daeab74..2d4bec5 100644 --- a/hierarchy.go +++ b/hierarchy.go @@ -46,6 +46,7 @@ type Entry struct { Type EntryType } +// Path provides the full path of the file, despite RelativeType or FullType func (e Entry) Path() string { if e.Parent == nil || e.Type == FullType { return e.Name diff --git a/parse.go b/parse.go index 2904965..61af381 100644 --- a/parse.go +++ b/parse.go @@ -53,6 +53,9 @@ func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) { case len(strings.Fields(str)) > 0 && strings.Fields(str)[0] == "..": e.Type = DotDotType e.Raw = str + if creator.curDir != nil { + creator.curDir = creator.curDir.Parent + } // nothing else to do here case len(strings.Fields(str)) > 0: // collapse any escaped newlines @@ -74,6 +77,7 @@ func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) { } e.Name = f[0] e.Keywords = f[1:] + e.Parent = creator.curDir for i := range e.Keywords { kv := KeyVal(e.Keywords[i]) if kv.Keyword() == "type" {