Skip to content

Commit

Permalink
Merge pull request #46 from ivanilves/final-node
Browse files Browse the repository at this point in the history
fix: solve path choice issue
  • Loading branch information
ivanilves authored Apr 11, 2023
2 parents e9eb2e7 + 362d5c5 commit c1d16e5
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 31 deletions.
11 changes: 11 additions & 0 deletions fixtures/directory/terragrunt/dev/region-1/rds/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
source = "my-terraform-module"
}

include "root" {
path = find_in_parent_folders()
}

inputs = {
foo = "bar"
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-git/go-git/v5 v5.4.2
github.com/manifoldco/promptui v0.9.0
github.com/stretchr/testify v1.8.0
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
golang.org/x/term v0.2.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ github.com/go-git/go-git-fixtures/v4 v4.2.1 h1:n9gGL1Ct/yIw+nfsfr8s4+sbhT+Ncu2Su
github.com/go-git/go-git-fixtures/v4 v4.2.1/go.mod h1:K8zd3kDUAykwTdDCr+I0per6Y6vMiRR/nnVTBtavnB0=
github.com/go-git/go-git/v5 v5.4.2 h1:BXyZu9t0VkbiHtqrsvdq39UDhGJTl1h55VW6CSC4aY4=
github.com/go-git/go-git/v5 v5.4.2/go.mod h1:gQ1kArt6d+n+BGd+/B/I74HwRTLhth2+zti4ihgckDc=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
Expand Down Expand Up @@ -81,6 +81,8 @@ golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
Expand Down
13 changes: 3 additions & 10 deletions pkg/config/mode/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"reflect"
"runtime"
"strings"

"golang.org/x/exp/slices"
)

const (
Expand All @@ -22,15 +24,6 @@ func getFnName(fn func(os.DirEntry) bool) string {
return parts[len(parts)-1] + "()"
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

func runSuite(t *testing.T, fn func(os.DirEntry) bool, paths ...string) {
assert := assert.New(t)

Expand All @@ -54,7 +47,7 @@ func runSuite(t *testing.T, fn func(os.DirEntry) bool, paths ...string) {
if p == path {
assert.Truef(fn(d), "func \"%+s\" is expected to include this path: %s", fnName, p)
} else {
if !contains(paths, path) {
if !slices.Contains(paths, path) {
assert.Falsef(fn(d), "func \"%+s\" is expected to exclude this path: %s", fnName, p)
}
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/directory/tree/node/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package node

// Node is an element Tree gets composed from (a path with hierarchical and connection metadata)
type Node struct {
name string
path string
parent string
children []string
levelID int
terminal bool
}

// NewNode creates a new Node instance
func NewNode(name, path, parent string, children []string, levelID int, terminal bool) Node {
return Node{
name: name,
path: path,
parent: parent,
children: children,
levelID: levelID,
terminal: terminal,
}
}

// IsAChildOf tells us if current node is a child of another node
func (n Node) IsAChildOf(pn Node) bool {
return n.parent == pn.path
}

// HasChildren tells us if that node has child nodes (i.e. if node is a parent itself)
func (n Node) HasChildren() bool {
return len(n.children) > 0
}

// IsTerminal tells us if that node has can be a Tree exit point
func (n Node) IsTerminal() bool {
return n.terminal
}
44 changes: 24 additions & 20 deletions pkg/directory/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ package tree
import (
"sort"
"strings"

"golang.org/x/exp/slices"

"github.com/ivanilves/travelgrunt/pkg/directory/tree/node"
)

// Tree is a hierarchical representation of the directory contents
type Tree struct {
nodes map[string]node
nodes map[string]node.Node

levels []map[string]string
}

type node struct {
name string
path string
parent string
children []string
levelID int
}

func getLevels(paths []string) (levels []map[string]string) {
for _, path := range paths {
elements := strings.Split(path, "/")
Expand Down Expand Up @@ -73,20 +69,21 @@ func getNodeChildren(path string, levels []map[string]string, idx int) (children
return children
}

func getNodes(levels []map[string]string) (nodes map[string]node) {
nodes = make(map[string]node, 0)
func getNodes(levels []map[string]string, paths []string) (nodes map[string]node.Node) {
nodes = make(map[string]node.Node, 0)

var prevLevel map[string]string

for idx, level := range levels {
for path, name := range level {
nodes[path] = node{
name: name,
path: path,
parent: getNodeParent(path, prevLevel),
children: getNodeChildren(path, levels, idx),
levelID: idx,
}
nodes[path] = node.NewNode(
name,
path,
getNodeParent(path, prevLevel),
getNodeChildren(path, levels, idx),
idx,
slices.Contains(paths, path),
)
}

prevLevel = level
Expand Down Expand Up @@ -114,7 +111,7 @@ func sortedKeys(items map[string]string) (keys []string) {
func NewTree(paths []string) Tree {
levels := getLevels(paths)

nodes := getNodes(levels)
nodes := getNodes(levels, paths)

return Tree{nodes: nodes, levels: levels}
}
Expand Down Expand Up @@ -155,9 +152,16 @@ func (t Tree) ChildItems(idx int, parentPath string) (items map[string]string) {
items = make(map[string]string, len(t.levels[idx+1]))

for path, name := range t.levels[idx+1] {
if t.nodes[path].parent == parentPath {
currentNode := t.nodes[path]
parentNode := t.nodes[parentPath]

if currentNode.IsAChildOf(parentNode) {
items[name] = path
}

if parentNode.IsTerminal() && parentNode.HasChildren() {
items["."] = parentPath
}
}

return items
Expand Down
16 changes: 16 additions & 0 deletions pkg/directory/tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ func TestChildItems(t *testing.T) {
assert.Equal(expected, items)
}

func TestChildItemsTerminalAndWithChildren(t *testing.T) {
assert := assert.New(t)

items := mock.ChildItems(3, "terragrunt/dev/region-1/rds")
expected := map[string]string{
".": "terragrunt/dev/region-1/rds",
"bar": "terragrunt/dev/region-1/rds/bar",
"baz": "terragrunt/dev/region-1/rds/baz",
"foo": "terragrunt/dev/region-1/rds/foo",
}

assert.NotNil(items)
assert.Equal(4, len(items))
assert.Equal(expected, items)
}

func TestChildNames(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit c1d16e5

Please sign in to comment.