diff --git a/Gopkg.lock b/Gopkg.lock index 5f3a66c0d..e27cff859 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -247,6 +247,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "2f46f529d88e9669424c4096e715fdabebe2ee533b6cd239f046dfe6fa9b246b" + inputs-digest = "0918dd6d626b6bc967d88ec42515ca4e30fa1890caafbf7f74472bf85a3dea7b" solver-name = "gps-cdcl" solver-version = 1 diff --git a/filetree/filetree.go b/filetree/filetree.go index ac41e6fa2..29c2e33a8 100644 --- a/filetree/filetree.go +++ b/filetree/filetree.go @@ -2,8 +2,11 @@ package filetree import ( "fmt" + "io/ioutil" "os" "path/filepath" + + yaml "gopkg.in/yaml.v2" ) // Node represents a leaf in the filetree @@ -15,6 +18,15 @@ type Node struct { } func (n Node) MarshalYAML() (interface{}, error) { + switch len(n.Children) { + case 0: + return n.marshalLeaf() + default: + return n.marshalParent() + } +} + +func (n Node) marshalParent() (interface{}, error) { tree := map[string]interface{}{} for _, child := range n.Children { c, err := child.MarshalYAML() @@ -27,6 +39,19 @@ func (n Node) MarshalYAML() (interface{}, error) { return tree, nil } +func (n Node) marshalLeaf() (interface{}, error) { + var content interface{} + + buf, err := ioutil.ReadFile(n.FullPath) + if err != nil { + return content, err + } + + err = yaml.Unmarshal(buf, &content) + + return content, err +} + // 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 4f87592bf..669f75690 100644 --- a/filetree/filetree_test.go +++ b/filetree/filetree_test.go @@ -33,13 +33,13 @@ var _ = Describe("filetree", func() { var rootFile, subDir, subDirFile string BeforeEach(func() { - rootFile = filepath.Join(tempRoot, "root_file") + rootFile = filepath.Join(tempRoot, "root_file.yml") subDir = filepath.Join(tempRoot, "sub_dir") - subDirFile = filepath.Join(tempRoot, "sub_dir", "sub_dir_file") + subDirFile = filepath.Join(tempRoot, "sub_dir", "sub_dir_file.yml") - Expect(ioutil.WriteFile(rootFile, []byte{}, 0600)).To(Succeed()) + Expect(ioutil.WriteFile(rootFile, []byte("foo:\n bar"), 0600)).To(Succeed()) Expect(os.Mkdir(subDir, 0700)).To(Succeed()) - Expect(ioutil.WriteFile(subDirFile, []byte{}, 0600)).To(Succeed()) + Expect(ioutil.WriteFile(subDirFile, []byte("foo:\n bar:\n baz"), 0600)).To(Succeed()) }) It("Builds a tree of the nested file-structure", func() { @@ -53,13 +53,13 @@ var _ = Describe("filetree", func() { sort.Slice(tree.Children, func(i, j int) bool { return tree.Children[i].FullPath < tree.Children[j].FullPath }) - Expect(tree.Children[0].Info.Name()).To(Equal("root_file")) + Expect(tree.Children[0].Info.Name()).To(Equal("root_file.yml")) Expect(tree.Children[0].FullPath).To(Equal(rootFile)) Expect(tree.Children[1].Info.Name()).To(Equal("sub_dir")) Expect(tree.Children[1].FullPath).To(Equal(subDir)) Expect(tree.Children[1].Children).To(HaveLen(1)) - Expect(tree.Children[1].Children[0].Info.Name()).To(Equal("sub_dir_file")) + Expect(tree.Children[1].Children[0].Info.Name()).To(Equal("sub_dir_file.yml")) Expect(tree.Children[1].Children[0].FullPath).To(Equal(subDirFile)) }) @@ -69,9 +69,14 @@ var _ = Describe("filetree", func() { out, err := yaml.Marshal(tree) Expect(err).ToNot(HaveOccurred()) - Expect(out).To(MatchYAML(`root_file: {} + Expect(out).To(MatchYAML(`root_file.yml: + foo: + bar sub_dir: - sub_dir_file: {} + sub_dir_file.yml: + foo: + bar: + baz `)) }) })