Skip to content

Commit

Permalink
Inline specialCase into a method on Node
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Scott committed Jun 22, 2018
1 parent b859803 commit 34d83e1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
9 changes: 1 addition & 8 deletions cmd/collapse.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"regexp"

"github.com/CircleCI-Public/circleci-cli/filetree"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
Expand All @@ -20,13 +18,8 @@ func init() {
collapseCommand.Flags().StringVarP(&root, "root", "r", ".circleci", "path to your configuration (default is .circleci)")
}

func specialCase(path string) bool {
re := regexp.MustCompile(`orb\.(yml|yaml)$`)
return re.MatchString(path)
}

func collapse(cmd *cobra.Command, args []string) {
tree, err := filetree.NewTree(root, specialCase)
tree, err := filetree.NewTree(root)
if err != nil {
Logger.FatalOnError("An error occurred trying to build the tree", err)
}
Expand Down
11 changes: 7 additions & 4 deletions filetree/filetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func (n Node) rootFile() bool {
return n.Info.Mode().IsRegular() && n.root() == n.Parent
}

func (n Node) specialCase() bool {
re := regexp.MustCompile(`orb\.(yml|yaml)$`)
return re.MatchString(n.basename())
}

func (n Node) marshalParent() (interface{}, error) {
subtree := map[string]interface{}{}
for _, child := range n.Children {
Expand All @@ -70,7 +75,7 @@ func (n Node) marshalParent() (interface{}, error) {
if child.rootFile() {
merged := mergeTree(subtree, c)
subtree = merged
} else if SpecialCase(child.basename()) {
} else if child.specialCase() {
merged := mergeTree(subtree, subtree[child.Parent.name()], c)
subtree = merged
} else {
Expand Down Expand Up @@ -194,9 +199,7 @@ func collectNodes(absRootPath string) (PathNodes, error) {
}

// NewTree creates a new filetree starting at the root
func NewTree(rootPath string, specialCase func(path string) bool) (*Node, error) {
SpecialCase = specialCase

func NewTree(rootPath string) (*Node, error) {
absRootPath, err := filepath.Abs(rootPath)
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions filetree/filetree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ var _ = Describe("filetree", func() {
anotherDirFile := filepath.Join(tempRoot, "another_dir", "another_dir_file.yml")
Expect(os.Mkdir(anotherDir, 0700)).To(Succeed())
Expect(ioutil.WriteFile(anotherDirFile, []byte("1some: in: valid: yaml"), 0600)).To(Succeed())
tree, err := filetree.NewTree(tempRoot, func(path string) bool { return false })
tree, err := filetree.NewTree(tempRoot)
Expect(err).ToNot(HaveOccurred())

_, err = yaml.Marshal(tree)
Expect(err).To(MatchError("yaml: mapping values are not allowed in this context"))
})

It("Builds a tree of the nested file-structure", func() {
tree, err := filetree.NewTree(tempRoot, func(path string) bool { return false })
tree, err := filetree.NewTree(tempRoot)

Expect(err).ToNot(HaveOccurred())
Expect(tree.FullPath).To(Equal(tempRoot))
Expand All @@ -79,7 +79,7 @@ var _ = Describe("filetree", func() {
})

It("renders to YAML", func() {
tree, err := filetree.NewTree(tempRoot, func(path string) bool { return false })
tree, err := filetree.NewTree(tempRoot)
Expect(err).ToNot(HaveOccurred())

out, err := yaml.Marshal(tree)
Expand Down Expand Up @@ -108,7 +108,7 @@ sub_dir:

})
It("renders to YAML", func() {
tree, err := filetree.NewTree(tempRoot, func(path string) bool { return false })
tree, err := filetree.NewTree(tempRoot)
Expect(err).ToNot(HaveOccurred())

out, err := yaml.Marshal(tree)
Expand Down

0 comments on commit 34d83e1

Please sign in to comment.