Skip to content

Commit

Permalink
Merge pull request #30 from circleci/dotfiles
Browse files Browse the repository at this point in the history
Skip dotfiles when populating the tree with nodes, that aren't root
  • Loading branch information
Zachary Scott authored Jun 21, 2018
2 parents 33e1820 + a49dac8 commit 4f2b4a7
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions filetree/filetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,27 @@ func (n Node) rootFile() bool {
return n.Info.Mode().IsRegular() && n.root() == n.Parent
}

func (n Node) notYaml() bool {
re := regexp.MustCompile(`.+\.(yml|yaml)$`)
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
Expand All @@ -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.notYaml() {
if !isYaml(n.Info) {
return content, nil
}

Expand All @@ -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
Expand All @@ -145,7 +147,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)
Expand All @@ -172,6 +174,12 @@ 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 && node.Info.Mode().IsRegular() {
if dotfile(node.Info) || !isYaml(node.Info) {
continue
}
}
parentPath := filepath.Dir(path)
parent, exists := parents[parentPath]
if exists {
Expand Down

0 comments on commit 4f2b4a7

Please sign in to comment.