Skip to content

Commit

Permalink
Add a filetree package we'll need for collapsing folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Scott committed Jun 13, 2018
1 parent e518c58 commit 7c1996d
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions filetree/tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package filetree

import (
"fmt"
"os"
"path/filepath"
)

type Node struct {
FullPath string
Info *os.FileInfo
Children []*Node
Parent *Node
}

func NewTree(root string) (*Node, error) {
parents := make(map[string]*Node)
// need to pass this in as an array
exclude := []string{"path/to/skip"}

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())
return filepath.SkipDir
}

parents[path] = &Node{
FullPath: path,
Info: info,
Children: make([]*Node, 0),
}
return nil
})
if err != nil {
return nil, err
}
for path, node := range parents {
parentPath := filepath.Dir(path)
parent, exists := parents[parentPath]
if exists {
node.Parent = parent
parent.Children = append(parent.Children, node)
}

}
return node, err
}

0 comments on commit 7c1996d

Please sign in to comment.