Skip to content

Commit

Permalink
filetree: Insert file contents into YAML
Browse files Browse the repository at this point in the history
Render the contents of leaf nodes. Next up we need to omit empty files
and files that aren't YAML.
  • Loading branch information
dcarley committed Jun 15, 2018
1 parent 2ff4a59 commit da660f4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions filetree/filetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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 {
Expand Down
21 changes: 13 additions & 8 deletions filetree/filetree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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))
})

Expand All @@ -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
`))
})
})
Expand Down

0 comments on commit da660f4

Please sign in to comment.