|
| 1 | +package tips |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/gruntwork-io/terragrunt/internal/filter" |
| 9 | + inthclparse "github.com/gruntwork-io/terragrunt/internal/hclparse" |
| 10 | + "github.com/gruntwork-io/terragrunt/internal/vfs" |
| 11 | + "github.com/gruntwork-io/terragrunt/pkg/log" |
| 12 | +) |
| 13 | + |
| 14 | +// GiveStackNestedGenerateTip emits the StackNestedStacksNotGenerated tip after |
| 15 | +// generation when a literal (non-glob) path with `| type=stack` targets a stack |
| 16 | +// whose nested stacks were not themselves recursively generated. |
| 17 | +// The user likely expected the whole subtree, so the tip shows how to include it. |
| 18 | +func GiveStackNestedGenerateTip( |
| 19 | + l log.Logger, |
| 20 | + fs vfs.FS, |
| 21 | + funcsFor inthclparse.StackFuncFactory, |
| 22 | + workingDir string, |
| 23 | + filters filter.Filters, |
| 24 | + allTips Tips, |
| 25 | +) { |
| 26 | + if len(filters) == 0 || allTips == nil || funcsFor == nil { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + tip := allTips.Find(StackNestedStacksNotGenerated) |
| 31 | + if tip == nil { |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + var paths []string |
| 36 | + |
| 37 | + for _, f := range filters.RestrictToStacks() { |
| 38 | + path := literalStackFilterPath(f) |
| 39 | + if path == "" { |
| 40 | + continue |
| 41 | + } |
| 42 | + |
| 43 | + dir := path |
| 44 | + if !filepath.IsAbs(dir) { |
| 45 | + dir = filepath.Join(workingDir, dir) |
| 46 | + } |
| 47 | + |
| 48 | + if !stackHasUngeneratedNestedStacks(l, fs, funcsFor, dir) { |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + paths = append(paths, path) |
| 53 | + } |
| 54 | + |
| 55 | + if len(paths) == 0 { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + tip.EvaluateWith(l, buildStackNestedGenerateMessage(paths)) |
| 60 | +} |
| 61 | + |
| 62 | +// SuggestRecursiveStackFilter returns the recursive stack filter that selects |
| 63 | +// the nested stacks beneath path. |
| 64 | +func SuggestRecursiveStackFilter(path string) string { |
| 65 | + return path + "/** | type=stack" |
| 66 | +} |
| 67 | + |
| 68 | +// literalStackFilterPath returns the first literal (non-glob) path targeted by |
| 69 | +// the filter, or "" if it has none. |
| 70 | +func literalStackFilterPath(f *filter.Filter) string { |
| 71 | + var found string |
| 72 | + |
| 73 | + filter.WalkExpressions(f.Expression(), func(e filter.Expression) bool { |
| 74 | + pe, ok := e.(*filter.PathExpression) |
| 75 | + if !ok { |
| 76 | + return true |
| 77 | + } |
| 78 | + |
| 79 | + if containsGlobMeta(pe.Value) { |
| 80 | + return true |
| 81 | + } |
| 82 | + |
| 83 | + found = pe.Value |
| 84 | + |
| 85 | + return false |
| 86 | + }) |
| 87 | + |
| 88 | + return found |
| 89 | +} |
| 90 | + |
| 91 | +// stackHasUngeneratedNestedStacks reports whether the stack generated at dir has |
| 92 | +// nested stacks that were not themselves recursively generated. For each nested |
| 93 | +// stack the parent generated, it checks whether that nested stack's own components |
| 94 | +// exist on disk (honoring no_dot_terragrunt_stack). |
| 95 | +func stackHasUngeneratedNestedStacks(l log.Logger, fs vfs.FS, funcsFor inthclparse.StackFuncFactory, dir string) bool { |
| 96 | + _, nestedStackDirs, err := inthclparse.DirectComponentPaths(fs, dir, funcsFor) |
| 97 | + if err != nil { |
| 98 | + l.Debugf("stack-nested-generate tip: skipping %q: %v", dir, err) |
| 99 | + return false |
| 100 | + } |
| 101 | + |
| 102 | + for _, nestedDir := range nestedStackDirs { |
| 103 | + if !nestedStackGenerated(l, fs, funcsFor, nestedDir) { |
| 104 | + return true |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return false |
| 109 | +} |
| 110 | + |
| 111 | +// nestedStackGenerated reports whether every direct component of the nested stack |
| 112 | +// generated at nestedDir exists on disk, i.e. the nested stack was itself generated. |
| 113 | +func nestedStackGenerated(l log.Logger, fs vfs.FS, funcsFor inthclparse.StackFuncFactory, nestedDir string) bool { |
| 114 | + unitPaths, stackPaths, err := inthclparse.DirectComponentPaths(fs, nestedDir, funcsFor) |
| 115 | + if err != nil { |
| 116 | + l.Debugf("stack-nested-generate tip: skipping %q: %v", nestedDir, err) |
| 117 | + return true |
| 118 | + } |
| 119 | + |
| 120 | + return !anyPathMissing(l, fs, unitPaths) && !anyPathMissing(l, fs, stackPaths) |
| 121 | +} |
| 122 | + |
| 123 | +// anyPathMissing reports whether any of paths does not exist on fs. |
| 124 | +func anyPathMissing(l log.Logger, fs vfs.FS, paths []string) bool { |
| 125 | + for _, p := range paths { |
| 126 | + exists, err := vfs.FileExists(fs, p) |
| 127 | + if err != nil { |
| 128 | + l.Debugf("stack-nested-generate tip: cannot stat %q: %v", p, err) |
| 129 | + continue |
| 130 | + } |
| 131 | + |
| 132 | + if !exists { |
| 133 | + return true |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return false |
| 138 | +} |
| 139 | + |
| 140 | +func buildStackNestedGenerateMessage(paths []string) string { |
| 141 | + var b strings.Builder |
| 142 | + |
| 143 | + b.WriteString(StackNestedStacksNotGeneratedMessage) |
| 144 | + b.WriteString(" For example:") |
| 145 | + |
| 146 | + for _, p := range paths { |
| 147 | + fmt.Fprintf(&b, "\n --filter %q --filter %q", p+" | type=stack", SuggestRecursiveStackFilter(p)) |
| 148 | + } |
| 149 | + |
| 150 | + return b.String() |
| 151 | +} |
0 commit comments