Skip to content

Commit

Permalink
fix: handle external terragrunt dep without panic (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 authored Jul 15, 2024
1 parent 408c767 commit 518b60f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
6 changes: 3 additions & 3 deletions internal/module/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func (s *Service) loadTerragruntDependenciesFromDigraph(r io.Reader) error {
continue
}
// Convert dependency paths to module IDs
dependencyIDs := make([]resource.ID, len(depPaths))
for i, path := range depPaths {
dependencyIDs := make([]resource.ID, 0, len(depPaths))
for _, path := range depPaths {
// If absolute path then convert to path relative to pug's working
// directory.
if filepath.IsAbs(path) {
Expand All @@ -176,7 +176,7 @@ func (s *Service) loadTerragruntDependenciesFromDigraph(r io.Reader) error {
// Skip loading this dependency
continue
}
dependencyIDs[i] = mod.ID
dependencyIDs = append(dependencyIDs, mod.ID)
}
s.table.Update(mod.ID, func(existing *Module) error {
existing.Common = existing.WithDependencies(dependencyIDs...)
Expand Down
4 changes: 4 additions & 0 deletions internal/module/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/leg100/pug/internal"
"github.com/leg100/pug/internal/logging"
"github.com/leg100/pug/internal/resource"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -22,6 +23,7 @@ func TestLoadTerragruntDependenciesFromDigraph(t *testing.T) {
svc := &Service{
table: &fakeModuleTable{modules: []*Module{vpc, redis, mysql, backend, frontend}},
workdir: workdir,
logger: logging.Discard,
}

// Check it can handle both relative and absolute paths - `terragrunt
Expand Down Expand Up @@ -50,11 +52,13 @@ digraph {
"%[1]s/root/backend-app" -> "%[1]s/root/mysql";
"%[1]s/root/backend-app" -> "%[1]s/root/redis";
"%[1]s/root/backend-app" -> "%[1]s/root/vpc";
"%[1]s/root/backend-app" -> "/outside_workdir/kafka";
"%[1]s/root/mysql" ;
"%[1]s/root/mysql" -> "%[1]s/root/vpc";
"%[1]s/root/redis" ;
"%[1]s/root/redis" -> "%[1]s/root/vpc";
"%[1]s/root/vpc" ;
"/outside_workdir/kafka" ;
}
`, workdir)
)
Expand Down
13 changes: 9 additions & 4 deletions internal/tui/module/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ func (m *ListMaker) Make(_ resource.ID, width, height int) (tea.Model, error) {
currentWorkspace.Key: m.Helpers.CurrentWorkspaceName(mod.CurrentWorkspaceID),
table.ResourceCountColumn.Key: m.Helpers.ModuleCurrentResourceCount(mod),
}
dependencyNames := make([]string, len(mod.Dependencies()))
for i, id := range mod.Dependencies() {
mod, _ := m.ModuleService.Get(id)
dependencyNames[i] = mod.Path
dependencyNames := make([]string, 0, len(mod.Dependencies()))
for _, id := range mod.Dependencies() {
mod, err := m.ModuleService.Get(id)
if err != nil {
// Should never happen
dependencyNames = append(dependencyNames, fmt.Sprintf("error: %s", err.Error()))
continue
}
dependencyNames = append(dependencyNames, mod.Path)
}
row[dependencies.Key] = strings.Join(dependencyNames, ",")
return row
Expand Down

0 comments on commit 518b60f

Please sign in to comment.