diff options
-rw-r--r-- | .gitignore | 7 | ||||
-rw-r--r-- | Gopkg.lock | 104 | ||||
-rw-r--r-- | Gopkg.toml | 43 | ||||
-rw-r--r-- | Makefile | 46 | ||||
-rw-r--r-- | cmd/die/main.go | 25 | ||||
-rw-r--r-- | filetree/changeinfo.go (renamed from filechangeinfo.go) | 14 | ||||
-rw-r--r-- | filetree/changeinfo_test.go (renamed from filechangeinfo_test.go) | 32 | ||||
-rw-r--r-- | filetree/node.go (renamed from filenode.go) | 76 | ||||
-rw-r--r-- | filetree/node_test.go (renamed from filenode_test.go) | 52 | ||||
-rw-r--r-- | filetree/tree.go (renamed from filetree.go) | 64 | ||||
-rw-r--r-- | filetree/tree_test.go (renamed from filetree_test.go) | 110 | ||||
-rw-r--r-- | filetreeview.go | 114 | ||||
-rw-r--r-- | image/image.go | 104 | ||||
-rw-r--r-- | image/tar_read.go (renamed from tar_read.go) | 53 | ||||
-rw-r--r-- | layerview.go | 79 | ||||
-rw-r--r-- | main.go | 229 | ||||
-rw-r--r-- | ui/filetreeview.go | 121 | ||||
-rw-r--r-- | ui/layerview.go | 85 | ||||
-rw-r--r-- | ui/ui.go | 122 | ||||
-rw-r--r-- | ui/view.go | 10 | ||||
-rw-r--r-- | view.go | 8 |
21 files changed, 852 insertions, 646 deletions
@@ -1,4 +1,4 @@ -.idea +/.idea # Binaries for programs and plugins *.exe @@ -13,4 +13,7 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out -image +/build +/_vendor* +/vendor +/.image diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..5166b9e --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,104 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/Microsoft/go-winio" + packages = ["."] + revision = "7da180ee92d8bd8bb8c37fc560e673e6557c392f" + version = "v0.4.7" + +[[projects]] + name = "github.com/docker/distribution" + packages = [ + "digest", + "reference" + ] + revision = "48294d928ced5dd9b378f7fd7c6f5da3ff3f2c89" + version = "v2.6.2" + +[[projects]] + name = "github.com/docker/docker" + packages = [ + "api/types", + "api/types/blkiodev", + "api/types/container", + "api/types/events", + "api/types/filters", + "api/types/mount", + "api/types/network", + "api/types/reference", + "api/types/registry", + "api/types/strslice", + "api/types/swarm", + "api/types/time", + "api/types/versions", + "api/types/volume", + "client", + "pkg/tlsconfig" + ] + revision = "092cba3727bb9b4a2f0e922cd6c0f93ea270e363" + version = "v1.13.1" + +[[projects]] + name = "github.com/docker/go-connections" + packages = [ + "nat", + "sockets", + "tlsconfig" + ] + revision = "3ede32e2033de7505e6500d6c868c2b9ed9f169d" + version = "v0.3.0" + +[[projects]] + name = "github.com/docker/go-units" + packages = ["."] + revision = "47565b4f722fb6ceae66b95f853feed578a4a51c" + version = "v0.3.3" + +[[projects]] + branch = "master" + name = "github.com/jroimartin/gocui" + packages = ["."] + revision = "c055c87ae801372cd74a0839b972db4f7697ae5f" + +[[projects]] + name = "github.com/mattn/go-runewidth" + packages = ["."] + revision = "9e777a8366cce605130a531d2cd6363d07ad7317" + version = "v0.0.2" + +[[projects]] + branch = "master" + name = "github.com/nsf/termbox-go" + packages = ["."] + revision = "21a4d435a86280a2927985fd6296de56cbce453e" + +[[projects]] + name = "github.com/pkg/errors" + packages = ["."] + revision = "645ef00459ed84a119197bfb8d8205042c6df63d" + version = "v0.8.0" + +[[projects]] + branch = "master" + name = "golang.org/x/net" + packages = [ + "context", + "context/ctxhttp", + "internal/socks", + "proxy" + ] + revision = "1e491301e022f8f977054da4c2d852decd59571f" + +[[projects]] + branch = "master" + name = "golang.org/x/sys" + packages = ["windows"] + revision = "c11f84a56e43e20a78cee75a7c034031ecf57d1f" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "73c0fae1988538f4def02f2bd28830793264b4260b25d078b561413085a81845" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..2fa2eba --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,43 @@ +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true + + +[[constraint]] + name = "github.com/docker/docker" + version = "1.13.1" + +[[constraint]] + name = "github.com/jroimartin/gocui" + # version = "0.3.0" + branch = "master" + +[[constraint]] + branch = "master" + name = "golang.org/x/net" + +[prune] + go-tests = true + unused-packages = true @@ -1,12 +1,34 @@ -SHELL := /bin/bash -.DEFAULT_GOAL := run -.PHONY: run - -run: - go run main.go \ - filechangeinfo.go \ - filenode.go \ - filetree.go \ - tar_read.go \ - filetreeview.go \ - layerview.go +BIN = die + +all: clean build + +run: build + ./build/$(BIN) + +build: deps + go build -o build/$(BIN) ./cmd/... + +install: deps + go install ./... + +deps: + command -v dep >/dev/null || go get -u github.com/golang/dep/cmd/dep + dep ensure + +test: build + @! git grep tcell -- ':!tui/' ':!Gopkg.lock' ':!Gopkg.toml' ':!Makefile' + go test -v ./... + +lint: lintdeps build + golint -set_exit_status $$(go list ./... | grep -v /vendor/) + +lintdeps: + go get -d -v -t ./... + command -v golint >/dev/null || go get -u github.com/golang/lint/golint + +clean: + rm -rf build + rm -rf vendor + go clean + +.PHONY: build install deps test lint lintdeps clean
\ No newline at end of file diff --git a/cmd/die/main.go b/cmd/die/main.go new file mode 100644 index 0000000..ab39ead --- /dev/null +++ b/cmd/die/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "os" + "github.com/wagoodman/docker-image-explorer/image" + "github.com/wagoodman/docker-image-explorer/ui" +) + +const name = "die" +const version = "v0.0.0" +const author = "wagoodman" + +func main() { + os.Exit(run(os.Args)) +} + + +func run(args []string) int { + image.WriteImage() + manifest, refTrees := image.InitializeData() + + ui.Run(manifest, refTrees) + return 0 +} + diff --git a/filechangeinfo.go b/filetree/changeinfo.go index faa85ba..f1b19ba 100644 --- a/filechangeinfo.go +++ b/filetree/changeinfo.go @@ -1,4 +1,4 @@ -package main +package filetree import ( "bytes" @@ -6,10 +6,10 @@ import ( ) type FileChangeInfo struct { - path string - typeflag byte - md5sum [16]byte - diffType DiffType + Path string + Typeflag byte + MD5sum [16]byte + DiffType DiffType } type DiffType int @@ -51,8 +51,8 @@ func (a *FileChangeInfo) getDiffType(b *FileChangeInfo) DiffType { if a == nil || b == nil { return Changed } - if a.typeflag == b.typeflag { - if bytes.Compare(a.md5sum[:], b.md5sum[:]) == 0 { + if a.Typeflag == b.Typeflag { + if bytes.Compare(a.MD5sum[:], b.MD5sum[:]) == 0 { return Unchanged } } diff --git a/filechangeinfo_test.go b/filetree/changeinfo_test.go index a1152ff..1892ac7 100644 --- a/filechangeinfo_test.go +++ b/filetree/changeinfo_test.go @@ -1,4 +1,4 @@ -package main +package filetree import ( "fmt" @@ -6,9 +6,9 @@ import ( ) func TestAssignDiffType(t *testing.T) { - tree := NewTree() + tree := NewFileTree() tree.AddPath("/usr", BlankFileChangeInfo("/usr", Changed)) - if tree.root.children["usr"].data.diffType != Changed { + if tree.Root.Children["usr"].Data.DiffType != Changed { t.Fail() } } @@ -29,25 +29,25 @@ func TestMergeDiffTypes(t *testing.T) { } func TestDiffTypeFromChildren(t *testing.T) { - tree := NewTree() + tree := NewFileTree() tree.AddPath("/usr", BlankFileChangeInfo("/usr", Unchanged)) info1 := BlankFileChangeInfo("/usr/bin", Added) tree.AddPath("/usr/bin", info1) info2 := BlankFileChangeInfo("/usr/bin2", Removed) tree.AddPath("/usr/bin2", info2) - tree.root.children["usr"].deriveDiffType(Unchanged) - if tree.root.children["usr"].data.diffType != Changed { - t.Errorf("Expected Changed but got %v", tree.root.children["usr"].data.diffType) + tree.Root.Children["usr"].deriveDiffType(Unchanged) + if tree.Root.Children["usr"].Data.DiffType != Changed { + t.Errorf("Expected Changed but got %v", tree.Root.Children["usr"].Data.DiffType) } } func AssertDiffType(node *FileNode, expectedDiffType DiffType, t *testing.T) error { - if node.data == nil { - t.Errorf("Expected *FileChangeInfo but got nil at path %s", node.Path()) - return fmt.Errorf("expected *FileChangeInfo but got nil at path %s", node.Path()) + if node.Data == nil { + t.Errorf("Expected *FileChangeInfo but got nil at Path %s", node.Path()) + return fmt.Errorf("expected *FileChangeInfo but got nil at Path %s", node.Path()) } - if node.data.diffType != expectedDiffType { - t.Errorf("Expecting node at %s to have DiffType %v, but had %v", node.Path(), expectedDiffType, node.data.diffType) + if node.Data.DiffType != expectedDiffType { + t.Errorf("Expecting node at %s to have DiffType %v, but had %v", node.Path(), expectedDiffType, node.Data.DiffType) return fmt.Errorf("Assertion failed") } return nil @@ -55,10 +55,10 @@ func AssertDiffType(node *FileNode, expectedDiffType DiffType, t *testing.T) err func BlankFileChangeInfo(path string, diffType DiffType) (f *FileChangeInfo) { result := FileChangeInfo{ - path: path, - typeflag: 1, - md5sum: [16]byte{1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0}, - diffType: diffType, + Path: path, + Typeflag: 1, + MD5sum: [16]byte{1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0}, + DiffType: diffType, } return &result } diff --git a/filenode.go b/filetree/node.go index ec06eef..394cf93 100644 --- a/filenode.go +++ b/filetree/node.go @@ -1,4 +1,4 @@ -package main +package filetree import ( "sort" @@ -6,25 +6,25 @@ import ( ) type FileNode struct { - tree *FileTree - parent *FileNode - name string - collapsed bool - data *FileChangeInfo - children map[string]*FileNode + Tree *FileTree + Parent *FileNode + Name string + Collapsed bool + Data *FileChangeInfo + Children map[string]*FileNode } func NewNode(parent *FileNode, name string, data *FileChangeInfo) (node *FileNode) { node = new(FileNode) - node.name = name + node.Name = name if data == nil { data = &FileChangeInfo{} } - node.data = data - node.children = make(map[string]*FileNode) - node.parent = parent + node.Data = data + node.Children = make(map[string]*FileNode) + node.Parent = parent if parent != nil { - node.tree = parent.tree + node.Tree = parent.Tree } return node } @@ -33,46 +33,46 @@ func (node *FileNode) Copy() *FileNode { // newNode := new(FileNode) // *newNode = *node // return newNode - newNode := NewNode(node.parent, node.name, node.data) - for name, child := range node.children { - newNode.children[name] = child.Copy() + newNode := NewNode(node.Parent, node.Name, node.Data) + for name, child := range node.Children { + newNode.Children[name] = child.Copy() } return newNode } func (node *FileNode) AddChild(name string, data *FileChangeInfo) (child *FileNode) { child = NewNode(node, name, data) - if node.children[name] != nil { + if node.Children[name] != nil { // tree node already exists, replace the payload, keep the children - node.children[name].data = data + node.Children[name].Data = data } else { - node.children[name] = child - node.tree.size++ + node.Children[name] = child + node.Tree.Size++ } return child } func (node *FileNode) Remove() error { - for _, child := range node.children { + for _, child := range node.Children { child.Remove() } - delete(node.parent.children, node.name) - node.tree.size-- + delete(node.Parent.Children, node.Name) + node.Tree.Size-- return nil } func (node *FileNode) String() string { - return node.name + return node.Name } func (node *FileNode) Visit(visiter Visiter) error { var keys []string - for key := range node.children { + for key := range node.Children { keys = append(keys, key) } sort.Strings(keys) for _, name := range keys { - child := node.children[name] + child := node.Children[name] err := child.Visit(visiter) if err != nil { return err @@ -88,12 +88,12 @@ func (node *FileNode) VisitDepthParentFirst(visiter Visiter, evaluator VisitEval } var keys []string - for key := range node.children { + for key := range node.Children { keys = append(keys, key) } sort.Strings(keys) for _, name := range keys { - child := node.children[name] + child := node.Children[name] if evaluator == nil || !evaluator(node) { continue } @@ -106,31 +106,31 @@ func (node *FileNode) VisitDepthParentFirst(visiter Visiter, evaluator VisitEval } func (node *FileNode) IsWhiteout() bool { - return strings.HasPrefix(node.name, whiteoutPrefix) + return strings.HasPrefix(node.Name, whiteoutPrefix) } func (node *FileNode) Path() string { path := []string{} curNode := node for { - if curNode.parent == nil { + if curNode.Parent == nil { break } - name := curNode.name + name := curNode.Name if curNode == node { // white out prefixes are fictitious on leaf nodes name = strings.TrimPrefix(name, whiteoutPrefix) } path = append([]string{name}, path...) - curNode = curNode.parent + curNode = curNode.Parent } return "/" + strings.Join(path, "/") } func (node *FileNode) IsLeaf() bool { - return len(node.children) == 0 + return len(node.Children) == 0 } func (node *FileNode) deriveDiffType(diffType DiffType) error { @@ -143,9 +143,9 @@ func (node *FileNode) deriveDiffType(diffType DiffType) error { } myDiffType := diffType - for _, v := range node.children { - vData := v.data - myDiffType = myDiffType.merge(vData.diffType) + for _, v := range node.Children { + vData := v.Data + myDiffType = myDiffType.merge(vData.DiffType) } node.AssignDiffType(myDiffType) @@ -156,7 +156,7 @@ func (node *FileNode) AssignDiffType(diffType DiffType) error { if node.Path() == "/" { return nil } - node.data.diffType = diffType + node.Data.DiffType = diffType return nil } @@ -177,10 +177,10 @@ func (a *FileNode) compare(b *FileNode) DiffType { if b.IsWhiteout() { return Removed } - if a.name != b.name { + if a.Name != b.Name { panic("comparing mismatched nodes") } // TODO: fails on nil - return a.data.getDiffType(b.data) + return a.Data.getDiffType(b.Data) } diff --git a/filenode_test.go b/filetree/node_test.go index 910912c..17fa86b 100644 --- a/filenode_test.go +++ b/filetree/node_test.go @@ -1,49 +1,49 @@ -package main +package filetree import "testing" func TestAddChild(t *testing.T) { var expected, actual int - tree := NewTree() + tree := NewFileTree() payload := FileChangeInfo{ - path: "stufffffs", + Path: "stufffffs", } - one := tree.Root().AddChild("first node!", &payload) + one := tree.Root.AddChild("first node!", &payload) - two := tree.Root().AddChild("nil node!", nil) + two := tree.Root.AddChild("nil node!", nil) - tree.Root().AddChild("third node!", nil) + tree.Root.AddChild("third node!", nil) two.AddChild("forth, one level down...", nil) two.AddChild("fifth, one level down...", nil) two.AddChild("fifth, one level down...", nil) - expected, actual = 5, tree.size + expected, actual = 5, tree.Size if expected != actual { t.Errorf("Expected a tree size of %d got %d.", expected, actual) } - expected, actual = 2, len(two.children) + expected, actual = 2, len(two.Children) if expected != actual { t.Errorf("Expected 'twos' number of children to be %d got %d.", expected, actual) } - expected, actual = 3, len(tree.Root().children) + expected, actual = 3, len(tree.Root.Children) if expected != actual { t.Errorf("Expected 'twos' number of children to be %d got %d.", expected, actual) } expectedFC := &FileChangeInfo{ - path: "stufffffs", + Path: "stufffffs", } - actualFC := one.data + actualFC := one.Data if *expectedFC != *actualFC { t.Errorf("Expected 'ones' payload to be %+v got %+v.", expectedFC, actualFC) } - if *two.data != *new(FileChangeInfo) { - t.Errorf("Expected 'twos' payload to be nil got %d.", two.data) + if *two.Data != *new(FileChangeInfo) { + t.Errorf("Expected 'twos' payload to be nil got %d.", two.Data) } } @@ -51,32 +51,32 @@ func TestAddChild(t *testing.T) { func TestRemoveChild(t *testing.T) { var expected, actual int - tree := NewTree() - tree.Root().AddChild("first", nil) - two := tree.Root().AddChild("nil", nil) - tree.Root().AddChild("third", nil) + tree := NewFileTree() + tree.Root.AddChild("first", nil) + two := tree.Root.AddChild("nil", nil) + tree.Root.AddChild("third", nil) forth := two.AddChild("forth", nil) two.AddChild("fifth", nil) forth.Remove() - expected, actual = 4, tree.size + expected, actual = 4, tree.Size if expected != actual { t.Errorf("Expected a tree size of %d got %d.", expected, actual) } - if tree.Root().children["forth"] != nil { + if tree.Root.Children["forth"] != nil { t.Errorf("Expected 'forth' node to be deleted.") } two.Remove() - expected, actual = 2, tree.size + expected, actual = 2, tree.Size if expected != actual { t.Errorf("Expected a tree size of %d got %d.", expected, actual) } - if tree.Root().children["nil"] != nil { + if tree.Root.Children["nil"] != nil { t.Errorf("Expected 'nil' node to be deleted.") } @@ -84,25 +84,25 @@ func TestRemoveChild(t *testing.T) { func TestPath(t *testing.T) { expected := "/etc/nginx/nginx.conf" - tree := NewTree() + tree := NewFileTree() node, _ := tree.AddPath(expected, nil) actual := node.Path() if expected != actual { - t.Errorf("Expected path '%s' got '%s'", expected, actual) + t.Errorf("Expected Path '%s' got '%s'", expected, actual) } } func TestIsWhiteout(t *testing.T) { - tree1 := NewTree() + tree1 := NewFileTree() p1, _ := tree1.AddPath("/etc/nginx/public1", nil) p2, _ := tree1.AddPath("/etc/nginx/.wh.public2", nil) if p1.IsWhiteout() != false { - t.Errorf("Expected path '%s' to **not** be a whiteout file", p1.name) + t.Errorf("Expected Path '%s' to **not** be a whiteout file", p1.Name) } if p2.IsWhiteout() != true { - t.Errorf("Expected path '%s' to be a whiteout file", p2.name) + t.Errorf("Expected Path '%s' to be a whiteout file", p2.Name) } } diff --git a/filetree.go b/filetree/tree.go index 3c87231..d1efd1a 100644 --- a/filetree.go +++ b/filetree/tree.go @@ -1,4 +1,4 @@ -package main +package filetree import ( "errors" @@ -19,24 +19,20 @@ const ( ) type FileTree struct { - root *FileNode - size int - name string + Root *FileNode + Size int + Name string } -func NewTree() (tree *FileTree) { +func NewFileTree() (tree *FileTree) { tree = new(FileTree) - tree.size = 0 - tree.root = new(FileNode) - tree.root.tree = tree - tree.root.children = make(map[string]*FileNode) + tree.Size = 0 + tree.Root = new(FileNode) + tree.Root.Tree = tree + tree.Root.Children = make(map[string]*FileNode) return tree } -func (tree *FileTree) Root() *FileNode { - return tree.root -} - func (tree *FileTree) String() string { var renderLine func(string, []bool, bool, bool) string var walkTree func(*FileNode, []bool, int) string @@ -67,16 +63,16 @@ func (tree *FileTree) String() string { walkTree = func(node *FileNode, spaces []bool, depth int) string { var result string var keys []string - for key := range node.children { + for key := range node.Children { keys = append(keys, key) } sort.Strings(keys) for idx, name := range keys { - child := node.children[name] - last := idx == (len(node.children) - 1) - showCollapsed := child.collapsed && len(child.children) > 0 + child := node.Children[name] + last := idx == (len(node.Children) - 1) + showCollapsed := child.Collapsed && len(child.Children) > 0 result += renderLine(child.String(), spaces, last, showCollapsed) - if len(child.children) > 0 && !child.collapsed { + if len(child.Children) > 0 && !child.Collapsed { spacesChild := append(spaces, last) result += walkTree(child, spacesChild, depth+1) } @@ -84,15 +80,15 @@ func (tree *FileTree) String() string { return result } - return "." + newLine + walkTree(tree.Root(), []bool{}, 0) + return "." + newLine + walkTree(tree.Root, []bool{}, 0) } func (tree *FileTree) Copy() *FileTree { - newTree := NewTree() + newTree := NewFileTree() *newTree = *tree - newTree.root = tree.Root().Copy() + newTree.Root = tree.Root.Copy() newTree.Visit(func(node *FileNode) error { - node.tree = newTree + node.Tree = newTree return nil }) @@ -103,11 +99,11 @@ type Visiter func(*FileNode) error type VisitEvaluator func(*FileNode) bool func (tree *FileTree) Visit(visiter Visiter) error { - return tree.root.Visit(visiter) + return tree.Root.Visit(visiter) } func (tree *FileTree) VisitDepthParentFirst(visiter Visiter, evaluator VisitEvaluator) error { - return tree.root.VisitDepthParentFirst(visiter, evaluator) + return tree.Root.VisitDepthParentFirst(visiter, evaluator) } func (tree *FileTree) Stack(upper *FileTree) error { @@ -118,7 +114,7 @@ func (tree *FileTree) Stack(upper *FileTree) error { return fmt.Errorf("Cannot remove node %s: %v", node.Path(), err.Error()) } } else { - newNode, err := tree.AddPath(node.Path(), node.data) + newNode, err := tree.AddPath(node.Path(), node.Data) if err != nil { return fmt.Errorf("Cannot add node %s: %v", newNode.Path(), err.Error()) } @@ -130,38 +126,38 @@ func (tree *FileTree) Stack(upper *FileTree) error { func (tree *FileTree) GetNode(path string) (*FileNode, error) { nodeNames := strings.Split(path, "/") - node := tree.Root() + node := tree.Root for _, name := range nodeNames { if name == "" { continue } - if node.children[name] == nil { + if node.Children[name] == nil { return nil, errors.New("Path does not exist") } - node = node.children[name] + node = node.Children[name] } return node, nil } func (tree *FileTree) AddPath(path string, data *FileChangeInfo) (*FileNode, error) { nodeNames := strings.Split(path, "/") - node := tree.Root() + node := tree.Root for idx, name := range nodeNames { if name == "" { continue } // find or create node - if node.children[name] != nil { - node = node.children[name] + if node.Children[name] != nil { + node = node.Children[name] } else { // don't attach the payload. The payload is destined for the - // path's end node, not any intermediary node. + // Path's end node, not any intermediary node. node = node.AddChild(name, nil) } // attach payload to the last specified node if idx == len(nodeNames)-1 { - node.data = data + node.Data = data } } @@ -186,7 +182,7 @@ func (tree *FileTree) compare(upper *FileTree) error { } else { existingNode, _ := tree.GetNode(node.Path()) if existingNode == nil { - newNode, err := tree.AddPath(node.Path(), node.data) + newNode, err := tree.AddPath(node.Path(), node.Data) fmt.Printf("added new node at %s\n", newNode.Path()) if err != nil { return fmt.Errorf("Cannot add new node %s: %v", node.Path(), err.Error()) diff --git a/filetree_test.go b/filetree/tree_test.go index cf82ff0..67d6c2e 100644 --- a/filetree_test.go +++ b/filetree/tree_test.go @@ -1,4 +1,4 @@ -package main +package filetree import ( "fmt" @@ -6,10 +6,10 @@ import ( ) func TestPrintTree(t *testing.T) { - tree := NewTree() - tree.Root().AddChild("first node!", nil) - two := tree.Root().AddChild("second node!", nil) - tree.Root().AddChild("third node!", nil) + tree := NewFileTree() + tree.Root.AddChild("first node!", nil) + two := tree.Root.AddChild("second node!", nil) + tree.Root.AddChild("third node!", nil) two.AddChild("forth, one level down...", nil) expected := `. @@ -27,7 +27,7 @@ func TestPrintTree(t *testing.T) { } func TestAddPath(t *testing.T) { - tree := NewTree() + tree := NewFileTree() tree.AddPath("/etc/nginx/nginx.conf", nil) tree.AddPath("/etc/nginx/public", nil) tree.AddPath("/var/run/systemd", nil) @@ -56,7 +56,7 @@ func TestAddPath(t *testing.T) { } func TestRemovePath(t *testing.T) { - tree := NewTree() + tree := NewFileTree() tree.AddPath("/etc/nginx/nginx.conf", nil) tree.AddPath("/etc/nginx/public", nil) tree.AddPath("/var/run/systemd", nil) @@ -87,10 +87,10 @@ func TestRemovePath(t *testing.T) { func TestStack(t *testing.T) { payloadKey := "/var/run/systemd" payloadValue := FileChangeInfo{ - path: "yup", + Path: "yup", } - tree1 := NewTree() + tree1 := NewFileTree() tree1.AddPath("/etc/nginx/public", nil) tree1.AddPath(payloadKey, nil) @@ -98,7 +98,7 @@ func TestStack(t *testing.T) { tree1.AddPath("/tmp", nil) tree1.AddPath("/tmp/nonsense", nil) - tree2 := NewTree() + tree2 := NewFileTree() // add new files tree2.AddPath("/etc/nginx/nginx.conf", nil) // modify current files @@ -128,8 +128,8 @@ func TestStack(t *testing.T) { t.Errorf("Expected '%s' to still exist, but it doesn't", payloadKey) } - if *node.data != payloadValue { - t.Errorf("Expected '%s' value to be %+v but got %+v", payloadKey, payloadValue, node.data) + if *node.Data != payloadValue { + t.Errorf("Expected '%s' value to be %+v but got %+v", payloadKey, payloadValue, node.Data) } actual := tree1.String() @@ -141,7 +141,7 @@ func TestStack(t *testing.T) { } func TestCopy(t *testing.T) { - tree := NewTree() + tree := NewFileTree() tree.AddPath("/etc/nginx/nginx.conf", nil) tree.AddPath("/etc/nginx/public", nil) tree.AddPath("/var/run/systemd", nil) @@ -162,8 +162,8 @@ func TestCopy(t *testing.T) { └── systemd ` - newTree := tree.Copy() - actual := newTree.String() + NewFileTree := tree.Copy() + actual := NewFileTree.String() if expected != actual { t.Errorf("Expected tree string:\n--->%s<---\nGot:\n--->%s<---", expected, actual) @@ -172,16 +172,16 @@ func TestCopy(t *testing.T) { } func TestCompareWithNoChanges(t *testing.T) { - lowerTree := NewTree() - upperTree := NewTree() + lowerTree := NewFileTree() + upperTree := NewFileTree() paths := [...]string{"/etc", "/etc/sudoers", "/etc/hosts", "/usr/bin", "/usr/bin/bash", "/usr"} for _, value := range paths { fakeData := FileChangeInfo{ - path: value, - typeflag: 1, - md5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - diffType: Unchanged, + Path: value, + Typeflag: 1, + MD5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + DiffType: Unchanged, } lowerTree.AddPath(value, &fakeData) upperTree.AddPath(value, &fakeData) @@ -191,12 +191,12 @@ func TestCompareWithNoChanges(t *testing.T) { if n.Path() == "/" { return nil } - if n.data == nil { + if n.Data == nil { t.Errorf("Expected *FileChangeInfo but got nil") return fmt.Errorf("expected *FileChangeInfo but got nil") } - if (n.data.diffType) != Unchanged { - t.Errorf("Expecting node at %s to have DiffType unchanged, but had %v", n.Path(), n.data.diffType) + if (n.Data.DiffType) != Unchanged { + t.Errorf("Expecting node at %s to have DiffType unchanged, but had %v", n.Path(), n.Data.DiffType) } return nil } @@ -207,27 +207,27 @@ func TestCompareWithNoChanges(t *testing.T) { } func TestCompareWithAdds(t *testing.T) { - lowerTree := NewTree() - upperTree := NewTree() + lowerTree := NewFileTree() + upperTree := NewFileTree() lowerPaths := [...]string{"/etc", "/etc/sudoers", "/usr", "/etc/hosts", "/usr/bin"} upperPaths := [...]string{"/etc", "/etc/sudoers", "/usr", "/etc/hosts", "/usr/bin", "/usr/bin/bash"} for _, value := range lowerPaths { fakeData := FileChangeInfo{ - path: value, - typeflag: 1, - md5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - diffType: Unchanged, + Path: value, + Typeflag: 1, + MD5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + DiffType: Unchanged, } lowerTree.AddPath(value, &fakeData) } for _, value := range upperPaths { fakeData := FileChangeInfo{ - path: value, - typeflag: 1, - md5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - diffType: Unchanged, + Path: value, + Typeflag: 1, + MD5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0 |