From a05d8ebbbdeb94457c6de0727d8902f0a0391b31 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Fri, 18 Mar 2016 16:30:54 -0400 Subject: [PATCH] *: cleaner Parent handling Signed-off-by: Vincent Batts --- hierarchy.go | 8 ++++++++ parse.go | 23 ++++++++++++++++++++--- walk.go | 23 ++++++++++++++++++----- walk_test.go | 3 +-- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/hierarchy.go b/hierarchy.go index b69601a..659cbad 100644 --- a/hierarchy.go +++ b/hierarchy.go @@ -3,6 +3,7 @@ package mtree import ( "fmt" "io" + "path/filepath" "sort" "strings" ) @@ -45,6 +46,13 @@ type Entry struct { Type EntryType } +func (e Entry) Path() string { + if e.Parent == nil { + return e.Name + } + return filepath.Join(e.Parent.Path(), e.Name) +} + func (e Entry) String() string { if e.Raw != "" { return e.Raw diff --git a/parse.go b/parse.go index accf204..2904965 100644 --- a/parse.go +++ b/parse.go @@ -10,7 +10,9 @@ import ( func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) { s := bufio.NewScanner(r) i := int(0) - dh := DirectoryHierarchy{} + creator := dhCreator{ + DH: &DirectoryHierarchy{}, + } for s.Scan() { str := s.Text() e := Entry{Pos: i} @@ -43,6 +45,11 @@ func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) { f := strings.Fields(str) e.Name = f[0] e.Keywords = f[1:] + if e.Name == "/set" { + creator.curSet = &e + } else if e.Name == "/unset" { + creator.curSet = nil + } case len(strings.Fields(str)) > 0 && strings.Fields(str)[0] == "..": e.Type = DotDotType e.Raw = str @@ -67,12 +74,22 @@ func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) { } e.Name = f[0] e.Keywords = f[1:] + for i := range e.Keywords { + kv := KeyVal(e.Keywords[i]) + if kv.Keyword() == "type" { + if kv.Value() == "dir" { + creator.curDir = &e + } else { + creator.curEnt = &e + } + } + } default: // TODO(vbatts) log a warning? continue } - dh.Entries = append(dh.Entries, e) + creator.DH.Entries = append(creator.DH.Entries, e) i++ } - return &dh, s.Err() + return creator.DH, s.Err() } diff --git a/walk.go b/walk.go index 48c5e31..7842ceb 100644 --- a/walk.go +++ b/walk.go @@ -36,10 +36,19 @@ func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHie } } - // handle the /set SpecialType if info.IsDir() { + // TODO Insert a comment of the full path of the directory's name + /* + if creator.curDir != nil { + creator.DH.Entries = append(creator.DH.Entries, Entry{ + Raw: "# " + creator.curDir.Path(), + Type: CommentType, + }) + } + */ + + // set the initial /set keywords if creator.curSet == nil { - // set the initial /set keywords e := Entry{ Name: "/set", Type: SpecialType, @@ -92,9 +101,10 @@ func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHie } e := Entry{ - Name: filepath.Base(path), - Pos: len(creator.DH.Entries), - Set: creator.curSet, + Name: filepath.Base(path), + Pos: len(creator.DH.Entries), + Set: creator.curSet, + Parent: creator.curDir, } for _, keyword := range keywords { if str, err := KeywordFuncs[keyword](path, info); err == nil && str != "" { @@ -187,6 +197,9 @@ func walk(c *dhCreator, path string, info os.FileInfo, walkFn filepath.WalkFunc) Type: DotDotType, Pos: len(c.DH.Entries), }) + if c.curDir != nil { + c.curDir = c.curDir.Parent + } return nil } diff --git a/walk_test.go b/walk_test.go index 17547fa..c37fb87 100644 --- a/walk_test.go +++ b/walk_test.go @@ -2,7 +2,6 @@ package mtree import ( "io/ioutil" - "log" "testing" ) @@ -12,7 +11,7 @@ func TestWalk(t *testing.T) { t.Fatal(err) } - log.Fatalf("%#v", dh) + //log.Fatalf("%#v", dh) fh, err := ioutil.TempFile("", "walk.") if err != nil {