-
-
Notifications
You must be signed in to change notification settings - Fork 153
Add generate section inheritance and auto-generation support #1878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
0dd4c3e
Add generate section inheritance and auto-generation support
osterman 4792e63
Use pretty-printed YAML output for file generation
osterman f8570b0
Address CodeRabbit review feedback
osterman 63082db
refactor: Migrate terraform clean/generate to pkg with adapter pattern
osterman e7d9dfe
refactor: Complete migration of terraform clean to pkg/terraform/clean
osterman 4987ed9
test: Expand test coverage for terraform clean and generate packages
osterman 395ed6a
fix: Use filepath.Separator for cross-platform test compatibility
osterman b9e51af
test: Add tests for terraform generate files command
osterman f0b36b0
feat: Add go:embed usage examples for terraform clean and generate files
osterman 3ba205e
Address CodeRabbit review feedback
osterman 8385a28
docs: Add stack configuration documentation for generate section
osterman b42968b
docs: Move ActionCard before Arguments section per convention
osterman 83088eb
docs: Add docs and changelog links to generate files roadmap entry
osterman 2fa45de
fix: Address CodeRabbit review feedback
osterman 9e28ce7
fix: address CodeRabbit review comments
osterman 2cc2182
Merge branch 'main' into osterman/generate-section-prd
aknysh 0c5c288
fix: improve grammar in error messages and add Cache field to Options
osterman afcf15b
fix: correct broken link path for generate files command
osterman 2e02feb
address comments, add tests
aknysh 211e4f9
Merge remote-tracking branch 'origin/osterman/generate-section-prd' i…
aknysh fbec904
Merge remote-tracking branch 'origin/main' into osterman/generate-sec…
aknysh 732b526
address comments, improve/add docs
aknysh 894fa5c
add test fixtures and integration tests
aknysh 7f886ad
fix 'generate' inheritance, update tests, PRD and blog post
aknysh d330920
add 'generate' to Atmos JSON schema
aknysh 37137b5
address comments, add tests
aknysh b5f31f5
address comments, add tests
aknysh d240bfe
address comments, fix tests
aknysh 3e7942f
fix `--stacks` flag, address comments, add tests
aknysh ad99e3b
address comments, fix tests
aknysh 2f1ac06
address comments, add tests
aknysh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| - Clean all components across all stacks | ||
|
|
||
| ```shell | ||
| $ atmos terraform clean | ||
| ``` | ||
|
|
||
| - Clean a specific component | ||
|
|
||
| ```shell | ||
| $ atmos terraform clean vpc | ||
| ``` | ||
|
|
||
| - Clean a component in a specific stack | ||
|
|
||
| ```shell | ||
| $ atmos terraform clean vpc -s prod-ue2 | ||
| ``` | ||
|
|
||
| - Force clean without confirmation | ||
|
|
||
| ```shell | ||
| $ atmos terraform clean vpc -s prod-ue2 --force | ||
| ``` | ||
|
|
||
| - Clean everything including state files | ||
|
|
||
| ```shell | ||
| $ atmos terraform clean vpc -s prod-ue2 --everything | ||
| ``` | ||
|
|
||
| - Dry run to see what would be deleted | ||
|
|
||
| ```shell | ||
| $ atmos terraform clean --dry-run | ||
| ``` | ||
|
|
||
| - Skip deleting the lock file | ||
|
|
||
| ```shell | ||
| $ atmos terraform clean vpc -s prod-ue2 --skip-lock-file | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| - Generate files for a single component | ||
|
|
||
| ```shell | ||
| atmos terraform generate files vpc -s prod-ue2 | ||
| ``` | ||
|
|
||
| - Generate files for all components | ||
|
|
||
| ```shell | ||
| atmos terraform generate files --all | ||
| ``` | ||
|
|
||
| - Dry run to see what would be generated | ||
|
|
||
| ```shell | ||
| atmos terraform generate files vpc -s prod-ue2 --dry-run | ||
| ``` | ||
|
|
||
| - Delete generated files | ||
|
|
||
| ```shell | ||
| atmos terraform generate files --clean --all | ||
| ``` | ||
|
|
||
| - Filter by specific stacks | ||
|
|
||
| ```shell | ||
| atmos terraform generate files --all --stacks "prod-*" | ||
| ``` | ||
|
|
||
| - Filter by specific components | ||
|
|
||
| ```shell | ||
| atmos terraform generate files --all --components vpc,rds | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package generate | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
|
|
||
| errUtils "github.com/cloudposse/atmos/errors" | ||
| e "github.com/cloudposse/atmos/internal/exec" | ||
| cfg "github.com/cloudposse/atmos/pkg/config" | ||
| "github.com/cloudposse/atmos/pkg/flags" | ||
| "github.com/cloudposse/atmos/pkg/schema" | ||
| tfgenerate "github.com/cloudposse/atmos/pkg/terraform/generate" | ||
| ) | ||
|
|
||
| // filesParser handles flag parsing for files command. | ||
| var filesParser *flags.StandardParser | ||
|
|
||
| // filesCmd generates files for terraform components from the generate section. | ||
| var filesCmd = &cobra.Command{ | ||
| Use: "files [component]", | ||
| Short: "Generate files for Terraform components from the generate section", | ||
| Long: `Generate additional configuration files for Terraform components based on | ||
| the generate section in stack configuration. | ||
|
|
||
| When called with a component argument, generates files for that component. | ||
| When called with --all, generates files for all components across stacks. | ||
|
|
||
| The generate section in stack configuration supports: | ||
| - Map values: serialized based on file extension (.json, .yaml, .hcl, .tf) | ||
| - String values: written as literal templates with Go template support`, | ||
| Args: cobra.MaximumNArgs(1), | ||
| FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false}, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| // Use Viper to respect precedence (flag > env > config > default). | ||
| v := viper.GetViper() | ||
|
|
||
| // Bind files-specific flags to Viper. | ||
| if err := filesParser.BindFlagsToViper(cmd, v); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Get flag values from Viper. | ||
| stack := v.GetString("stack") | ||
| all := v.GetBool("all") | ||
| stacksCsv := v.GetString("stacks") | ||
| componentsCsv := v.GetString("components") | ||
| dryRun := v.GetBool("dry-run") | ||
| clean := v.GetBool("clean") | ||
|
|
||
| // Validate: component requires stack, --all excludes component. | ||
| if len(args) > 0 && all { | ||
| return fmt.Errorf("%w: cannot specify both component and --all", errUtils.ErrInvalidFlag) | ||
| } | ||
| if len(args) > 0 && stack == "" { | ||
| return fmt.Errorf("%w: --stack is required when specifying a component", errUtils.ErrInvalidFlag) | ||
| } | ||
| if len(args) == 0 && !all { | ||
| return fmt.Errorf("%w: either specify a component or use --all", errUtils.ErrInvalidFlag) | ||
| } | ||
|
|
||
| // Parse CSV values with whitespace trimming. | ||
| var stacks []string | ||
| if stacksCsv != "" { | ||
| for _, s := range strings.Split(stacksCsv, ",") { | ||
| stacks = append(stacks, strings.TrimSpace(s)) | ||
| } | ||
| } | ||
|
|
||
| var components []string | ||
| if componentsCsv != "" { | ||
| for _, c := range strings.Split(componentsCsv, ",") { | ||
| components = append(components, strings.TrimSpace(c)) | ||
| } | ||
| } | ||
|
|
||
| // Get global flags from Viper (includes base-path, config, config-path, profile). | ||
| globalFlags := flags.ParseGlobalFlags(cmd, v) | ||
|
|
||
| // Build ConfigAndStacksInfo from global flags to honor config selection flags. | ||
| configAndStacksInfo := schema.ConfigAndStacksInfo{ | ||
| AtmosBasePath: globalFlags.BasePath, | ||
| AtmosConfigFilesFromArg: globalFlags.Config, | ||
| AtmosConfigDirsFromArg: globalFlags.ConfigPath, | ||
| ProfilesFromArg: globalFlags.Profile, | ||
| } | ||
|
|
||
| // Initialize Atmos configuration. | ||
| atmosConfig, err := cfg.InitCliConfig(configAndStacksInfo, true) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Create adapter and service. | ||
| adapter := tfgenerate.NewExecAdapter(e.ProcessStacksForGenerate, e.FindStacksMapForGenerate) | ||
| service := tfgenerate.NewService(adapter) | ||
|
|
||
| // Execute based on mode. | ||
| if all { | ||
| return service.ExecuteForAll(&atmosConfig, stacks, components, dryRun, clean) | ||
| } | ||
|
|
||
| component := args[0] | ||
| return service.ExecuteForComponent(&atmosConfig, component, stack, dryRun, clean) | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| // Create parser with files-specific flags using functional options. | ||
| filesParser = flags.NewStandardParser( | ||
| flags.WithStringFlag("stack", "s", "", "Stack name (required for single component)"), | ||
| flags.WithBoolFlag("all", "", false, "Process all components in all stacks"), | ||
| flags.WithStringFlag("stacks", "", "", "Filter stacks (glob pattern, requires --all)"), | ||
| flags.WithStringFlag("components", "", "", "Filter components (comma-separated, requires --all)"), | ||
| flags.WithBoolFlag("dry-run", "", false, "Show what would be generated without writing"), | ||
| flags.WithBoolFlag("clean", "", false, "Delete generated files instead of creating"), | ||
| flags.WithEnvVars("stack", "ATMOS_STACK"), | ||
| flags.WithEnvVars("stacks", "ATMOS_STACKS"), | ||
| flags.WithEnvVars("components", "ATMOS_COMPONENTS"), | ||
| ) | ||
|
|
||
| // Register flags with the command. | ||
| filesParser.RegisterFlags(filesCmd) | ||
|
|
||
| // Bind flags to Viper for environment variable support. | ||
| if err := filesParser.BindToViper(viper.GetViper()); err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Register with parent GenerateCmd. | ||
| GenerateCmd.AddCommand(filesCmd) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.