From 7c1996d8950db8c13a0ba4fd8cf3d5d51760d488 Mon Sep 17 00:00:00 2001 From: Zachary Scott Date: Wed, 13 Jun 2018 19:03:03 +0900 Subject: [PATCH] Add a filetree package we'll need for collapsing folders --- filetree/tree.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 filetree/tree.go diff --git a/filetree/tree.go b/filetree/tree.go new file mode 100644 index 000000000..9850005b3 --- /dev/null +++ b/filetree/tree.go @@ -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 +}