mirror of
https://github.com/vbatts/go-mtree.git
synced 2024-12-21 21:36:31 +00:00
*: cleaner Parent handling
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
3b6cb6e117
commit
a05d8ebbbd
4 changed files with 47 additions and 10 deletions
|
@ -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
|
||||
|
|
23
parse.go
23
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()
|
||||
}
|
||||
|
|
23
walk.go
23
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue