-
-
Notifications
You must be signed in to change notification settings - Fork 96
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
Filter out empty results from describe stacks #764
Open
Cerebrovinny
wants to merge
10
commits into
main
Choose a base branch
from
feat/filter-empty-results
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+220
−19
Open
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5367378
filter empty results
Cerebrovinny c003b25
fix flag description
Cerebrovinny 958d921
clean up
Cerebrovinny ad8a683
refactoring and clean up
Cerebrovinny 80bfa6e
Merge branch 'main' into feat/filter-empty-results
Cerebrovinny f76f914
added tests and test fixes for empty stacks
Cerebrovinny 7629b93
fixes test filter
Cerebrovinny 1a0d381
Merge branch 'main' into feat/filter-empty-results
osterman 21c59b8
refactoring suggestions
Cerebrovinny 37f64bb
Merge branch 'main' into feat/filter-empty-results
Cerebrovinny 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 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 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 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 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 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 |
---|---|---|
|
@@ -55,6 +55,11 @@ func ExecuteDescribeStacksCmd(cmd *cobra.Command, args []string) error { | |
return err | ||
} | ||
|
||
includeEmptyStacks, err := cmd.Flags().GetBool("include-empty-stacks") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
componentsCsv, err := flags.GetString("components") | ||
if err != nil { | ||
return err | ||
|
@@ -97,6 +102,7 @@ func ExecuteDescribeStacksCmd(cmd *cobra.Command, args []string) error { | |
sections, | ||
false, | ||
processTemplates, | ||
includeEmptyStacks, | ||
) | ||
if err != nil { | ||
return err | ||
|
@@ -119,6 +125,7 @@ func ExecuteDescribeStacks( | |
sections []string, | ||
ignoreMissingFiles bool, | ||
processTemplates bool, | ||
includeEmptyStacks bool, | ||
) (map[string]any, error) { | ||
|
||
stacksMap, _, err := FindStacksMap(cliConfig, ignoreMissingFiles) | ||
|
@@ -127,6 +134,7 @@ func ExecuteDescribeStacks( | |
} | ||
|
||
finalStacksMap := make(map[string]any) | ||
processedStacks := make(map[string]bool) | ||
var varsSection map[string]any | ||
var metadataSection map[string]any | ||
var settingsSection map[string]any | ||
|
@@ -136,12 +144,48 @@ func ExecuteDescribeStacks( | |
var backendSection map[string]any | ||
var backendTypeSection string | ||
var stackName string | ||
context := schema.Context{} | ||
|
||
for stackFileName, stackSection := range stacksMap { | ||
var context schema.Context | ||
|
||
// Delete the stack-wide imports | ||
delete(stackSection.(map[string]any), "imports") | ||
|
||
// Check if components section exists and has explicit components | ||
hasExplicitComponents := false | ||
if componentsSection, ok := stackSection.(map[string]any)["components"]; ok { | ||
if componentsSection != nil { | ||
if terraformSection, ok := componentsSection.(map[string]any)["terraform"].(map[string]any); ok { | ||
hasExplicitComponents = len(terraformSection) > 0 | ||
} | ||
if helmfileSection, ok := componentsSection.(map[string]any)["helmfile"].(map[string]any); ok { | ||
hasExplicitComponents = hasExplicitComponents || len(helmfileSection) > 0 | ||
} | ||
} | ||
} | ||
|
||
// Also check for imports | ||
hasImports := false | ||
if importsSection, ok := stackSection.(map[string]any)["import"].([]any); ok { | ||
hasImports = len(importsSection) > 0 | ||
} | ||
|
||
// Skip stacks without components or imports when includeEmptyStacks is false | ||
if !includeEmptyStacks && !hasExplicitComponents && !hasImports { | ||
continue | ||
} | ||
|
||
stackName = stackFileName | ||
if processedStacks[stackName] { | ||
continue | ||
} | ||
processedStacks[stackName] = true | ||
|
||
if !u.MapKeyExists(finalStacksMap, stackName) { | ||
finalStacksMap[stackName] = make(map[string]any) | ||
finalStacksMap[stackName].(map[string]any)["components"] = make(map[string]any) | ||
} | ||
|
||
if componentsSection, ok := stackSection.(map[string]any)["components"].(map[string]any); ok { | ||
|
||
if len(componentTypes) == 0 || u.SliceContainsString(componentTypes, "terraform") { | ||
|
@@ -242,9 +286,13 @@ func ExecuteDescribeStacks( | |
|
||
if stackName == "" { | ||
stackName = stackFileName | ||
} else if strings.HasPrefix(stackFileName, "deploy/") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Cerebrovinny why do we need a special treatment for the |
||
// If we have a deploy/ prefixed version, use that as the canonical name | ||
stackName = stackFileName | ||
} | ||
|
||
if !u.MapKeyExists(finalStacksMap, stackName) { | ||
// Only create the stack entry if it doesn't exist or if we're using the canonical name | ||
if !u.MapKeyExists(finalStacksMap, stackName) || strings.HasPrefix(stackName, "deploy/") { | ||
Cerebrovinny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
finalStacksMap[stackName] = make(map[string]any) | ||
} | ||
|
||
|
@@ -430,9 +478,13 @@ func ExecuteDescribeStacks( | |
|
||
if stackName == "" { | ||
stackName = stackFileName | ||
} else if strings.HasPrefix(stackFileName, "deploy/") { | ||
// If we have a deploy/ prefixed version, use that as the canonical name | ||
stackName = stackFileName | ||
} | ||
|
||
if !u.MapKeyExists(finalStacksMap, stackName) { | ||
// Only create the stack entry if it doesn't exist or if we're using the canonical name | ||
if !u.MapKeyExists(finalStacksMap, stackName) || strings.HasPrefix(stackName, "deploy/") { | ||
finalStacksMap[stackName] = make(map[string]any) | ||
} | ||
|
||
|
@@ -511,13 +563,58 @@ func ExecuteDescribeStacks( | |
} | ||
} | ||
} | ||
} | ||
|
||
// Filter out empty stacks after processing all stack files | ||
if !includeEmptyStacks { | ||
for stackName := range finalStacksMap { | ||
if stackName == "" { | ||
delete(finalStacksMap, stackName) | ||
continue | ||
} | ||
|
||
// Filter out empty stacks (stacks without any components) | ||
if st, ok := finalStacksMap[stackName].(map[string]any); ok { | ||
if len(st) == 0 { | ||
stackEntry := finalStacksMap[stackName].(map[string]any) | ||
componentsSection, hasComponents := stackEntry["components"].(map[string]any) | ||
|
||
Cerebrovinny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if !hasComponents { | ||
delete(finalStacksMap, stackName) | ||
continue | ||
} | ||
|
||
// Check if any component type (terraform/helmfile) has components | ||
hasNonEmptyComponents := false | ||
for _, components := range componentsSection { | ||
if compTypeMap, ok := components.(map[string]any); ok { | ||
for _, comp := range compTypeMap { | ||
if compContent, ok := comp.(map[string]any); ok { | ||
// Check for any meaningful content | ||
relevantSections := []string{"vars", "metadata", "settings", "env"} | ||
for _, section := range relevantSections { | ||
if _, hasSection := compContent[section]; hasSection { | ||
hasNonEmptyComponents = true | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if hasNonEmptyComponents { | ||
break | ||
} | ||
} | ||
|
||
if !hasNonEmptyComponents { | ||
delete(finalStacksMap, stackName) | ||
continue | ||
} | ||
|
||
// Check for duplicate stacks (deploy/ prefix) | ||
if strings.HasPrefix(stackName, "deploy/") { | ||
baseStackName := strings.TrimPrefix(stackName, "deploy/") | ||
delete(finalStacksMap, baseStackName) | ||
} | ||
} | ||
} else { | ||
} | ||
|
||
return finalStacksMap, nil | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.