diff --git a/filetree/filetree.go b/filetree/filetree.go index 18ca149dd..ac41e6fa2 100644 --- a/filetree/filetree.go +++ b/filetree/filetree.go @@ -14,6 +14,19 @@ type Node struct { Parent *Node `json:"-"` } +func (n Node) MarshalYAML() (interface{}, error) { + tree := map[string]interface{}{} + for _, child := range n.Children { + c, err := child.MarshalYAML() + if err != nil { + return tree, err + } + tree[child.Info.Name()] = c + } + + return tree, nil +} + // Helper function that returns true if a path exists in excludes array func excluded(exclude []string, path string) bool { for _, n := range exclude { diff --git a/filetree/filetree_test.go b/filetree/filetree_test.go index 435b07d4e..4f87592bf 100644 --- a/filetree/filetree_test.go +++ b/filetree/filetree_test.go @@ -9,6 +9,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + yaml "gopkg.in/yaml.v2" "github.com/circleci/circleci-cli/filetree" ) @@ -61,6 +62,18 @@ var _ = Describe("filetree", func() { Expect(tree.Children[1].Children[0].Info.Name()).To(Equal("sub_dir_file")) Expect(tree.Children[1].Children[0].FullPath).To(Equal(subDirFile)) }) + + It("renders to YAML", func() { + tree, err := filetree.NewTree(tempRoot) + Expect(err).ToNot(HaveOccurred()) + + out, err := yaml.Marshal(tree) + Expect(err).ToNot(HaveOccurred()) + Expect(out).To(MatchYAML(`root_file: {} +sub_dir: + sub_dir_file: {} +`)) + }) }) })