From e87bcde6944247e1990bc4bc1ed68378184e8014 Mon Sep 17 00:00:00 2001 From: Zachary Scott Date: Thu, 21 Jun 2018 17:50:17 +0900 Subject: [PATCH 1/2] Skip dotfiles when populating the tree with nodes, that aren't root This change also addresses some confusing names in the codebase --- filetree/filetree.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/filetree/filetree.go b/filetree/filetree.go index 874ab1827..9ee8126d9 100644 --- a/filetree/filetree.go +++ b/filetree/filetree.go @@ -59,32 +59,32 @@ func (n Node) rootFile() bool { return n.Info.Mode().IsRegular() && n.root() == n.Parent } -func (n Node) notYaml() bool { +func (n Node) isYaml() bool { re := regexp.MustCompile(`.+\.(yml|yaml)$`) - return !re.MatchString(n.FullPath) + return re.MatchString(n.FullPath) } func (n Node) marshalParent() (interface{}, error) { - tree := map[string]interface{}{} + subtree := map[string]interface{}{} for _, child := range n.Children { c, err := child.MarshalYAML() if err != nil { - return tree, err + return subtree, err } if child.rootFile() { - merged := mergeTree(tree, c) - tree = merged + merged := mergeTree(subtree, c) + subtree = merged } else if SpecialCase(child.basename()) { - merged := mergeTree(tree, tree[child.Parent.name()], c) - tree = merged + merged := mergeTree(subtree, subtree[child.Parent.name()], c) + subtree = merged } else { - merged := mergeTree(tree[child.name()], c) - tree[child.name()] = merged + merged := mergeTree(subtree[child.name()], c) + subtree[child.name()] = merged } } - return tree, nil + return subtree, nil } // Returns the root node @@ -103,7 +103,7 @@ func (n Node) marshalLeaf() (interface{}, error) { if n.Info.IsDir() { return content, nil } - if n.notYaml() { + if !n.isYaml() { return content, nil } @@ -145,7 +145,7 @@ func NewTree(root string, specialCase func(path string) bool) (*Node, error) { return err } - // Skip any dotfolders automatically + // Skip any dotfolders that aren't the root path if absroot != path && dotfolder(info) { // Turn off logging to stdout in this package //fmt.Printf("Skipping dotfolder: %+v\n", path) @@ -172,6 +172,10 @@ func NewTree(root string, specialCase func(path string) bool) (*Node, error) { for _, path := range pathKeys { node := parents[path] + // skip dotfile nodes that aren't the root path + if absroot != path && dotfile(node.Info.Name()) { + continue + } parentPath := filepath.Dir(path) parent, exists := parents[parentPath] if exists { From a49dac8366721db76817b0f0bdb9af3799b4b7e5 Mon Sep 17 00:00:00 2001 From: Zachary Scott Date: Thu, 21 Jun 2018 18:19:12 +0900 Subject: [PATCH 2/2] Don't add non-yaml file nodes when building the tree This also changes isYaml to be a regular function which takes an os.FileInfo as opposed to a method on a Node. Falling in suite with dotfile() and dotfolder() which do too. --- filetree/filetree.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/filetree/filetree.go b/filetree/filetree.go index 9ee8126d9..5aa48ee8d 100644 --- a/filetree/filetree.go +++ b/filetree/filetree.go @@ -59,11 +59,6 @@ func (n Node) rootFile() bool { return n.Info.Mode().IsRegular() && n.root() == n.Parent } -func (n Node) isYaml() bool { - re := regexp.MustCompile(`.+\.(yml|yaml)$`) - return re.MatchString(n.FullPath) -} - func (n Node) marshalParent() (interface{}, error) { subtree := map[string]interface{}{} for _, child := range n.Children { @@ -100,10 +95,12 @@ func (n Node) root() *Node { func (n Node) marshalLeaf() (interface{}, error) { var content interface{} + + // TODO: this check may be unnecessary with the isYaml check if n.Info.IsDir() { return content, nil } - if !n.isYaml() { + if !isYaml(n.Info) { return content, nil } @@ -118,13 +115,18 @@ func (n Node) marshalLeaf() (interface{}, error) { return content, err } -func dotfile(path string) bool { +func dotfile(info os.FileInfo) bool { re := regexp.MustCompile(`^\..+`) - return re.MatchString(path) + return re.MatchString(info.Name()) } func dotfolder(info os.FileInfo) bool { - return info.IsDir() && dotfile(info.Name()) + return info.IsDir() && dotfile(info) +} + +func isYaml(info os.FileInfo) bool { + re := regexp.MustCompile(`.+\.(yml|yaml)$`) + return re.MatchString(info.Name()) } // NewTree creates a new filetree starting at the root @@ -173,8 +175,10 @@ func NewTree(root string, specialCase func(path string) bool) (*Node, error) { for _, path := range pathKeys { node := parents[path] // skip dotfile nodes that aren't the root path - if absroot != path && dotfile(node.Info.Name()) { - continue + if absroot != path && node.Info.Mode().IsRegular() { + if dotfile(node.Info) || !isYaml(node.Info) { + continue + } } parentPath := filepath.Dir(path) parent, exists := parents[parentPath]