-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a filetree package we'll need for collapsing folders
- Loading branch information
Zachary Scott
committed
Jun 13, 2018
1 parent
e518c58
commit 7c1996d
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |