Skip to content

Commit

Permalink
add abstruct flag
Browse files Browse the repository at this point in the history
Signed-off-by: Pulak Kanti Bhowmick <pkbhowmick007@gmail.com>
  • Loading branch information
pkbhowmick committed Dec 16, 2024
1 parent 925d0e7 commit 2c85549
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 3 additions & 1 deletion cmd/list_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var listComponentsCmd = &cobra.Command{
checkAtmosConfig()

stackFlag, _ := cmd.Flags().GetString("stack")
abstractFlag, _ := cmd.Flags().GetBool("abstract")

configAndStacksInfo := schema.ConfigAndStacksInfo{}
cliConfig, err := config.InitCliConfig(configAndStacksInfo, true)
Expand All @@ -39,7 +40,7 @@ var listComponentsCmd = &cobra.Command{
return
}

output, err := l.FilterAndListComponents(stackFlag, stacksMap, cliConfig.Components.List)
output, err := l.FilterAndListComponents(stackFlag, abstractFlag, stacksMap, cliConfig.Components.List)
if err != nil {
u.PrintMessageInColor(fmt.Sprintf("Error: %v"+"\n", err), color.New(color.FgYellow))
return
Expand All @@ -51,5 +52,6 @@ var listComponentsCmd = &cobra.Command{

func init() {
listComponentsCmd.PersistentFlags().StringP("stack", "s", "", "Filter components by stack (e.g., atmos list components -s stack1)")
listComponentsCmd.PersistentFlags().Bool("abstract", false, "Filter abstract component if true")
listCmd.AddCommand(listComponentsCmd)
}
23 changes: 15 additions & 8 deletions pkg/list/list_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/charmbracelet/lipgloss/table"
"github.com/samber/lo"

"github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/schema"
)

Expand All @@ -21,7 +22,7 @@ type tableData struct {
}

// getStackComponents extracts Terraform components from the final map of stacks
func getStackComponents(stackData any, listFields []string) ([]string, error) {
func getStackComponents(stackData any, abstractFlag bool, listFields []string) ([]string, error) {
stackMap, ok := stackData.(map[string]any)
if !ok {
return nil, fmt.Errorf("could not parse stacks")
Expand All @@ -37,7 +38,13 @@ func getStackComponents(stackData any, listFields []string) ([]string, error) {
return nil, fmt.Errorf("could not parse Terraform components")
}

uniqueKeys := lo.Keys(terraformComponents)
var uniqueKeys []string

if abstractFlag {
uniqueKeys = lo.Keys(terraformComponents)
} else {
uniqueKeys = exec.FilterAbstractComponents(terraformComponents)
}
result := make([]string, 0)

for _, dataKey := range uniqueKeys {
Expand Down Expand Up @@ -114,13 +121,13 @@ func parseColumns(listConfig schema.ListConfig) ([]string, []string, error) {
}

// collectComponents gathers components for the specified stack or all stacks
func collectComponents(stackFlag string, stacksMap map[string]any, listFields []string) ([][]string, error) {
func collectComponents(stackFlag string, abstractFlag bool, stacksMap map[string]any, listFields []string) ([][]string, error) {
components := [][]string{}

if stackFlag != "" {
// Filter components for the specified stack
if stackData, ok := stacksMap[stackFlag]; ok {
stackComponents, err := getStackComponents(stackData, listFields)
stackComponents, err := getStackComponents(stackData, abstractFlag, listFields)
if err != nil {
return nil, fmt.Errorf("error processing stack '%s': %w", stackFlag, err)
}
Expand All @@ -134,7 +141,7 @@ func collectComponents(stackFlag string, stacksMap map[string]any, listFields []
// Collect components from all stacks
var errors []string
for _, stackData := range stacksMap {
stackComponents, err := getStackComponents(stackData, listFields)
stackComponents, err := getStackComponents(stackData, abstractFlag, listFields)
if err != nil {
errors = append(errors, err.Error())
continue // Skip invalid stacks
Expand Down Expand Up @@ -199,7 +206,7 @@ func generateTable(data tableData) {
EvenRowStyle = CellStyle.Foreground(lightGray) // Style for even rows

BorderStyle = lipgloss.NewStyle().
Foreground(purple) // Border style with purple color
Foreground(gray)
)

// Create the table with headers, rows, and styles
Expand Down Expand Up @@ -233,15 +240,15 @@ func generateTable(data tableData) {
}

// FilterAndListComponents orchestrates the process
func FilterAndListComponents(stackFlag string, stacksMap map[string]any, listConfig schema.ListConfig) (string, error) {
func FilterAndListComponents(stackFlag string, abstractFlag bool, stacksMap map[string]any, listConfig schema.ListConfig) (string, error) {
// Step 1: Parse columns
header, listFields, err := parseColumns(listConfig)
if err != nil {
return "", err
}

// Step 2: Collect components
components, err := collectComponents(stackFlag, stacksMap, listFields)
components, err := collectComponents(stackFlag, abstractFlag, stacksMap, listFields)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/list/list_components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestListComponents(t *testing.T) {
{Name: "Folder", Value: "{{ .vars.tenant }}"},
},
}
output, err := FilterAndListComponents("", stacksMap, listConfig)
output, err := FilterAndListComponents("", false, stacksMap, listConfig)
assert.Nil(t, err)
dependentsYaml, err := u.ConvertToYAML(output)
assert.Nil(t, err)
Expand Down

0 comments on commit 2c85549

Please sign in to comment.