Skip to content

Commit

Permalink
Merge pull request #20 from circleci/filetree
Browse files Browse the repository at this point in the history
Filetree
  • Loading branch information
Zachary Scott authored Jun 14, 2018
2 parents 660163f + 3c52039 commit 8f5c6a1
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
68 changes: 68 additions & 0 deletions filetree/filetree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package filetree

import (
"fmt"
"os"
"path/filepath"
)

// Node represents a leaf in the filetree
type Node struct {
FullPath string
Info os.FileInfo
Children []*Node
Parent *Node
}

// Helper function that returns true if a path exists in excludes array
func excluded(exclude []string, path string) bool {
for _, n := range exclude {
if path == n {
return true
}
}
return false
}

// NewTree creates a new filetree starting at the root
func NewTree(root string) (*Node, error) {
parents := make(map[string]*Node)
var result *Node

err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// check if file is in exclude slice and skip it
// need to pass this in as an array
exclude := []string{"path/to/skip"}
if excluded(exclude, path) {
fmt.Printf("skipping: %+v \n", info.Name())
return filepath.SkipDir
}

parents[path] = &Node{
FullPath: path,
Info: info,
Children: make([]*Node, 0),
}
return nil
})

if err != nil {
return nil, err
}
for path, node := range parents {
parentPath := filepath.Dir(path)
parent, exists := parents[parentPath]
if exists {
node.Parent = parent
parent.Children = append(parent.Children, node)
} else {
result = node
}

}
return result, err
}
58 changes: 58 additions & 0 deletions filetree/filetree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package filetree_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/circleci/circleci-cli/filetree"
)

var _ = Describe("filetree", func() {
var (
tempRoot string
)

BeforeEach(func() {
var err error
tempRoot, err = ioutil.TempDir("", "circleci-cli-test-")
Expect(err).ToNot(HaveOccurred())
})

AfterEach(func() {
Expect(os.RemoveAll(tempRoot)).To(Succeed())
})

Describe("NewTree", func() {
BeforeEach(func() {
var err error
_, err = os.OpenFile(
filepath.Join(tempRoot, "foo"),
os.O_RDWR|os.O_CREATE,
0600,
)
Expect(err).ToNot(HaveOccurred())
})

It("Builds a tree of the nested file-structure", func() {
tree, err := filetree.NewTree(tempRoot)

Expect(err).ToNot(HaveOccurred())
Expect(tree.FullPath).To(Equal(tempRoot))
Expect(tree.Info.Name()).To(Equal(filepath.Base(tempRoot)))

fooPath := filepath.Join(tempRoot, "foo")
Expect(tree.Children[0].Info.Name()).To(Equal("foo"))
Expect(tree.Children[0].FullPath).To(Equal(fooPath))
})
})
})

func TestCmd(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Filetree Suite")
}

0 comments on commit 8f5c6a1

Please sign in to comment.