From 34d83e1364560e6b1169d6e92ce7af7fb1943a1f Mon Sep 17 00:00:00 2001 From: Zachary Scott Date: Fri, 22 Jun 2018 20:40:49 +0900 Subject: [PATCH 1/2] Inline specialCase into a method on Node --- cmd/collapse.go | 9 +-------- filetree/filetree.go | 11 +++++++---- filetree/filetree_test.go | 8 ++++---- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/cmd/collapse.go b/cmd/collapse.go index cb7c625b5..7f73a0cb3 100644 --- a/cmd/collapse.go +++ b/cmd/collapse.go @@ -1,8 +1,6 @@ package cmd import ( - "regexp" - "github.com/CircleCI-Public/circleci-cli/filetree" "github.com/spf13/cobra" yaml "gopkg.in/yaml.v2" @@ -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) } diff --git a/filetree/filetree.go b/filetree/filetree.go index a8e4a1cce..b1593e634 100644 --- a/filetree/filetree.go +++ b/filetree/filetree.go @@ -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 { @@ -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 { @@ -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 diff --git a/filetree/filetree_test.go b/filetree/filetree_test.go index fc3d4deda..dd5a1a3a9 100644 --- a/filetree/filetree_test.go +++ b/filetree/filetree_test.go @@ -48,7 +48,7 @@ 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) @@ -56,7 +56,7 @@ var _ = Describe("filetree", func() { }) 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)) @@ -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) @@ -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) From 31d367d308639b4688de8908cff13e537bf49a5f Mon Sep 17 00:00:00 2001 From: Zachary Scott Date: Fri, 22 Jun 2018 20:47:14 +0900 Subject: [PATCH 2/2] Collapse looks for yaml files that start with "@" for specialCase --- .../hugo-collapse/.circleci/orbs/hugo/{orb.yml => @orb.yml} | 0 cmd/testdata/myorb/test/{orb.yml => @orb.yml} | 0 .../test/orbs/foo/{orb.yml => @orb.yml} | 0 .../test/orbs/foo/{orb.yml => @orb.yml} | 0 .../test/orbs/rails/{orb.yml => @orb.yml} | 0 filetree/filetree.go | 2 +- 6 files changed, 1 insertion(+), 1 deletion(-) rename cmd/testdata/hugo-collapse/.circleci/orbs/hugo/{orb.yml => @orb.yml} (100%) rename cmd/testdata/myorb/test/{orb.yml => @orb.yml} (100%) rename cmd/testdata/nested-orbs-and-local-commands-etc/test/orbs/foo/{orb.yml => @orb.yml} (100%) rename cmd/testdata/test-with-large-nested-rails-orb/test/orbs/foo/{orb.yml => @orb.yml} (100%) rename cmd/testdata/test-with-large-nested-rails-orb/test/orbs/rails/{orb.yml => @orb.yml} (100%) diff --git a/cmd/testdata/hugo-collapse/.circleci/orbs/hugo/orb.yml b/cmd/testdata/hugo-collapse/.circleci/orbs/hugo/@orb.yml similarity index 100% rename from cmd/testdata/hugo-collapse/.circleci/orbs/hugo/orb.yml rename to cmd/testdata/hugo-collapse/.circleci/orbs/hugo/@orb.yml diff --git a/cmd/testdata/myorb/test/orb.yml b/cmd/testdata/myorb/test/@orb.yml similarity index 100% rename from cmd/testdata/myorb/test/orb.yml rename to cmd/testdata/myorb/test/@orb.yml diff --git a/cmd/testdata/nested-orbs-and-local-commands-etc/test/orbs/foo/orb.yml b/cmd/testdata/nested-orbs-and-local-commands-etc/test/orbs/foo/@orb.yml similarity index 100% rename from cmd/testdata/nested-orbs-and-local-commands-etc/test/orbs/foo/orb.yml rename to cmd/testdata/nested-orbs-and-local-commands-etc/test/orbs/foo/@orb.yml diff --git a/cmd/testdata/test-with-large-nested-rails-orb/test/orbs/foo/orb.yml b/cmd/testdata/test-with-large-nested-rails-orb/test/orbs/foo/@orb.yml similarity index 100% rename from cmd/testdata/test-with-large-nested-rails-orb/test/orbs/foo/orb.yml rename to cmd/testdata/test-with-large-nested-rails-orb/test/orbs/foo/@orb.yml diff --git a/cmd/testdata/test-with-large-nested-rails-orb/test/orbs/rails/orb.yml b/cmd/testdata/test-with-large-nested-rails-orb/test/orbs/rails/@orb.yml similarity index 100% rename from cmd/testdata/test-with-large-nested-rails-orb/test/orbs/rails/orb.yml rename to cmd/testdata/test-with-large-nested-rails-orb/test/orbs/rails/@orb.yml diff --git a/filetree/filetree.go b/filetree/filetree.go index b1593e634..e18c91e02 100644 --- a/filetree/filetree.go +++ b/filetree/filetree.go @@ -60,7 +60,7 @@ func (n Node) rootFile() bool { } func (n Node) specialCase() bool { - re := regexp.MustCompile(`orb\.(yml|yaml)$`) + re := regexp.MustCompile(`^@.*\.(yml|yaml)$`) return re.MatchString(n.basename()) }