1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-11-25 09:35:39 +00:00

*: cleaner Parent handling

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2016-03-18 16:30:54 -04:00
parent 3b6cb6e117
commit a05d8ebbbd
4 changed files with 47 additions and 10 deletions

View file

@ -3,6 +3,7 @@ package mtree
import ( import (
"fmt" "fmt"
"io" "io"
"path/filepath"
"sort" "sort"
"strings" "strings"
) )
@ -45,6 +46,13 @@ type Entry struct {
Type EntryType 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 { func (e Entry) String() string {
if e.Raw != "" { if e.Raw != "" {
return e.Raw return e.Raw

View file

@ -10,7 +10,9 @@ import (
func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) { func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) {
s := bufio.NewScanner(r) s := bufio.NewScanner(r)
i := int(0) i := int(0)
dh := DirectoryHierarchy{} creator := dhCreator{
DH: &DirectoryHierarchy{},
}
for s.Scan() { for s.Scan() {
str := s.Text() str := s.Text()
e := Entry{Pos: i} e := Entry{Pos: i}
@ -43,6 +45,11 @@ func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) {
f := strings.Fields(str) f := strings.Fields(str)
e.Name = f[0] e.Name = f[0]
e.Keywords = f[1:] 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] == "..": case len(strings.Fields(str)) > 0 && strings.Fields(str)[0] == "..":
e.Type = DotDotType e.Type = DotDotType
e.Raw = str e.Raw = str
@ -67,12 +74,22 @@ func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) {
} }
e.Name = f[0] e.Name = f[0]
e.Keywords = f[1:] 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: default:
// TODO(vbatts) log a warning? // TODO(vbatts) log a warning?
continue continue
} }
dh.Entries = append(dh.Entries, e) creator.DH.Entries = append(creator.DH.Entries, e)
i++ i++
} }
return &dh, s.Err() return creator.DH, s.Err()
} }

17
walk.go
View file

@ -36,10 +36,19 @@ func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHie
} }
} }
// handle the /set SpecialType
if info.IsDir() { if info.IsDir() {
if creator.curSet == nil { // 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 // set the initial /set keywords
if creator.curSet == nil {
e := Entry{ e := Entry{
Name: "/set", Name: "/set",
Type: SpecialType, Type: SpecialType,
@ -95,6 +104,7 @@ func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHie
Name: filepath.Base(path), Name: filepath.Base(path),
Pos: len(creator.DH.Entries), Pos: len(creator.DH.Entries),
Set: creator.curSet, Set: creator.curSet,
Parent: creator.curDir,
} }
for _, keyword := range keywords { for _, keyword := range keywords {
if str, err := KeywordFuncs[keyword](path, info); err == nil && str != "" { 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, Type: DotDotType,
Pos: len(c.DH.Entries), Pos: len(c.DH.Entries),
}) })
if c.curDir != nil {
c.curDir = c.curDir.Parent
}
return nil return nil
} }

View file

@ -2,7 +2,6 @@ package mtree
import ( import (
"io/ioutil" "io/ioutil"
"log"
"testing" "testing"
) )
@ -12,7 +11,7 @@ func TestWalk(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
log.Fatalf("%#v", dh) //log.Fatalf("%#v", dh)
fh, err := ioutil.TempFile("", "walk.") fh, err := ioutil.TempFile("", "walk.")
if err != nil { if err != nil {