Skip to content

Commit

Permalink
Implement excluded to skip certain paths
Browse files Browse the repository at this point in the history
Linter is also happy now
  • Loading branch information
Zachary Scott committed Jun 14, 2018
1 parent 7c1996d commit 3cd4f39
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions filetree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,50 @@ import (
"path/filepath"
)

// Node represents a leaf in the filetree
type Node struct {
FullPath string
Info *os.FileInfo
Children []*Node
Parent *Node
}

// Helper function that returns true if a path exists in excludes array
func excluded(exclude []string, path string) bool {
for _, n := range exclude {
if path == n {
return true
}
}
return false
}

// NewTree creates a new filetree starting at the root
func NewTree(root string) (*Node, error) {
parents := make(map[string]*Node)
// need to pass this in as an array
exclude := []string{"path/to/skip"}
var result *Node

err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// check if file is in exclude slice and skip it
name := info.Name()
_, exists := exclude[name]
if exists {
fmt.Printf("skipping a dir without errors: %+v \n", info.Name())
// need to pass this in as an array
exclude := []string{"path/to/skip"}
if excluded(exclude, path) {
fmt.Printf("skipping: %+v \n", info.Name())
return filepath.SkipDir
}

parents[path] = &Node{
FullPath: path,
Info: info,
Info: &info,
Children: make([]*Node, 0),
}
return nil
})

if err != nil {
return nil, err
}
Expand All @@ -47,8 +59,10 @@ func NewTree(root string) (*Node, error) {
if exists {
node.Parent = parent
parent.Children = append(parent.Children, node)
} else {
result = node
}

}
return node, err
return result, err
}

0 comments on commit 3cd4f39

Please sign in to comment.