Skip to content

Commit 386fe0d

Browse files
committed
Add stack generation fix
1 parent a87c395 commit 386fe0d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

config/stack.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func generateUnits(ctx context.Context, opts *options.TerragruntOptions, pool *u
205205
source: unitCopy.Source,
206206
values: unitCopy.Values,
207207
noStack: unitCopy.NoStack != nil && *unitCopy.NoStack,
208+
kind: unitKind,
208209
}
209210

210211
opts.Logger.Infof("Processing unit %s", unitCopy.Name)
@@ -235,6 +236,7 @@ func generateStacks(ctx context.Context, opts *options.TerragruntOptions, pool *
235236
source: stackCopy.Source,
236237
noStack: stackCopy.NoStack != nil && *stackCopy.NoStack,
237238
values: stackCopy.Values,
239+
kind: stackKind,
238240
}
239241

240242
opts.Logger.Infof("Processing stack %s", stackCopy.Name)
@@ -250,6 +252,13 @@ func generateStacks(ctx context.Context, opts *options.TerragruntOptions, pool *
250252
return nil
251253
}
252254

255+
type componentKind int
256+
257+
const (
258+
unitKind componentKind = iota
259+
stackKind
260+
)
261+
253262
// componentToProcess represents an item of work for processing a stack or unit.
254263
// It contains information about the source and target directories, the name and path of the item, the source URL or path,
255264
// and any associated values that need to be processed.
@@ -261,6 +270,7 @@ type componentToProcess struct {
261270
path string
262271
source string
263272
noStack bool
273+
kind componentKind
264274
}
265275

266276
// processComponent copies files from the source directory to the target destination and generates a corresponding values file.
@@ -309,6 +319,21 @@ func processComponent(ctx context.Context, opts *options.TerragruntOptions, cmp
309319
return errors.Errorf("Failed to copy %s to %s %w", source, dest, err)
310320
}
311321

322+
if !cmp.noStack {
323+
// validate what was copied to the destination, don't do validation for special noStack components
324+
expectedFile := DefaultTerragruntConfigPath
325+
kindStr := "unit"
326+
327+
if cmp.kind == stackKind {
328+
expectedFile = defaultStackFile
329+
kindStr = "stack"
330+
}
331+
332+
if err := validateTargetDir(kindStr, cmp.name, dest, expectedFile); err != nil {
333+
return errors.Errorf("validation failed for %v %v", cmp.name, err)
334+
}
335+
}
336+
312337
// generate values file
313338
if err := writeValues(opts, cmp.values, dest); err != nil {
314339
return errors.Errorf("failed to write values %v %v", cmp.name, err)
@@ -633,3 +658,19 @@ func listStackFiles(opts *options.TerragruntOptions, dir string) ([]string, erro
633658

634659
return stackFiles, nil
635660
}
661+
662+
// validateTargetDir target destination directory.
663+
func validateTargetDir(kind, name, destDir, expectedFile string) error {
664+
expectedPath := filepath.Join(destDir, expectedFile)
665+
666+
info, err := os.Stat(expectedPath)
667+
if err != nil {
668+
return fmt.Errorf("%s '%s': expected file '%s' not found in target directory '%s': %w", kind, name, expectedFile, destDir, err)
669+
}
670+
671+
if info.IsDir() {
672+
return fmt.Errorf("%s '%s': expected file '%s' is a directory, not a file", kind, name, expectedFile)
673+
}
674+
675+
return nil
676+
}

0 commit comments

Comments
 (0)