-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from circleci/filetree
Filetree
- Loading branch information
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |