Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CIRCLE-12024: Collapse looks for yaml files that start with "@" for specialCase #4

Merged
merged 2 commits into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
File renamed without changes.
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(`^@.*\.(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