Skip to content
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

Improve atmos list stacks #979

Open
wants to merge 65 commits into
base: main
Choose a base branch
from
Open

Improve atmos list stacks #979

wants to merge 65 commits into from

Conversation

Cerebrovinny
Copy link
Collaborator

@Cerebrovinny Cerebrovinny commented Jan 28, 2025

what

why

Evidence:
CleanShot 2025-01-28 at 20 15 39

references

Summary by CodeRabbit

  • New Features

    • Introduced customizable configuration options for listing stacks and workflows with new display columns.
    • Added new CLI flags for output formatting (--format and --delimiter) supporting table, JSON, CSV, and TSV formats.
    • Enhanced configuration to include structured stack information with new list section in the configuration files.
    • Added support for tab-separated values (TSV) in output formatting.
  • Bug Fixes

    • Improved handling of stack metadata and component sections for enhanced robustness.
  • Documentation

    • Updated command help and online guides to illustrate the new display options and usage examples, including template usage and output format details.
  • Tests

    • Expanded test coverage to verify output format correctness and configuration behavior, including new test cases for TSV format validation.

@mergify mergify bot added the triage Needs triage label Jan 28, 2025
@Cerebrovinny Cerebrovinny marked this pull request as ready for review January 28, 2025 23:10
@Cerebrovinny Cerebrovinny requested a review from a team as a code owner January 28, 2025 23:10
Copy link
Contributor

coderabbitai bot commented Jan 28, 2025

📝 Walkthrough

Walkthrough

The changes introduce a new list configuration section for both stacks and workflows in the YAML files, adding options for custom columns, including "Stack," "Tenant," "Environment," and "File." It updates the CLI command for listing stacks by adding --format and --delimiter flags and revises the underlying functions to accept these parameters. The changes include updates to configuration schemas, enhanced output formatting (including TSV for workflows), and additional tests and snapshots to validate the new behavior.

Changes

File(s) Change Summary
atmos.yaml, examples/quick-start-advanced/atmos.yaml, tests/snapshots/TestCLICommands_atmos_describe_config*.golden Added new list sections under stacks (and workflows) with a columns array and format properties; updated file paths in snapshots.
cmd/list_stacks.go, website/docs/cli/commands/list/list-stacks.mdx Introduced --format and --delimiter flags; updated command examples and documentation to support multiple output formats.
pkg/list/list_stacks.go, pkg/list/list_stacks_test.go, internal/exec/describe_stacks.go, tests/snapshots/TestCLICommands_atmos_stacks_with_relative_paths.stdout.golden Enhanced stack listing functionality by updating function signatures to include listConfig, format, and delimiter parameters; added atmos_stack_file metadata and improved filtering and formatting logic.
pkg/schema/schema.go Updated configuration schema to include a new List field (of type ListConfig) in both Stacks and Workflows.
pkg/list/list_workflows.go, pkg/list/list_workflows_test.go Added support for TSV output, updated format validation, and adjusted workflow listing output accordingly.

Possibly related PRs

  • cloudposse/atmos#797: The changes in the main PR, which add a new list section in the atmos.yaml configuration file for stack information, are directly related to the modifications in the retrieved PR that introduces the atmos list command, as both involve enhancements to stack listing functionality and utilize similar configuration structures.
  • cloudposse/atmos#1006: The changes in the main PR, which add a new list configuration section in the atmos.yaml, are related to the modifications in the retrieved PR that enhance the ExecuteDescribeStacks function to include new flags for processing templates and functions, as both involve updates to how stack information is configured and processed.
  • cloudposse/atmos#764: The changes in the main PR, which add a new list configuration section in atmos.yaml, are related to the modifications in the retrieved PR that enhance the ExecuteDescribeStacks function to include an includeEmptyStacks parameter, as both involve the handling and representation of stack data in the context of the Atmos configuration.

Suggested labels

minor


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1eeb293 and 53a10b7.

📒 Files selected for processing (3)
  • pkg/schema/schema.go (1 hunks)
  • tests/snapshots/TestCLICommands_atmos_describe_config.stdout.golden (1 hunks)
  • tests/snapshots/TestCLICommands_atmos_describe_config_-f_yaml.stdout.golden (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • tests/snapshots/TestCLICommands_atmos_describe_config.stdout.golden
  • tests/snapshots/TestCLICommands_atmos_describe_config_-f_yaml.stdout.golden
  • pkg/schema/schema.go
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
pkg/list/list_stacks.go (1)

202-236: Enhance table rendering and consider performance.
The TTY support is a nice touch. Also, in lines 226-228, you can apply the suggested copy(headerRow, headers) improvement to simplify the loop.

-for i, h := range headers {
-    headerRow[i] = h
-}
+copy(headerRow, headers)
🧰 Tools
🪛 golangci-lint (1.62.2)

226-226: S1001: should use copy(to, from) instead of a loop

(gosimple)

cmd/list_stacks.go (1)

33-34: Consider adding error handling for the flag parsing.

While the code works, it silently ignores any flag parsing errors. Consider handling these errors to provide better user feedback.

-    formatFlag, _ := cmd.Flags().GetString("format")
-    delimiterFlag, _ := cmd.Flags().GetString("delimiter")
+    formatFlag, err := cmd.Flags().GetString("format")
+    if err != nil {
+        u.PrintMessageInColor(fmt.Sprintf("Error getting format flag: %v", err), theme.Colors.Error)
+        return
+    }
+    delimiterFlag, err := cmd.Flags().GetString("delimiter")
+    if err != nil {
+        u.PrintMessageInColor(fmt.Sprintf("Error getting delimiter flag: %v", err), theme.Colors.Error)
+        return
+    }

Also applies to: 49-49

pkg/list/list_components_test.go (1)

48-55: Consider adding more test cases.

While the current test case is good, consider adding tests for:

  • Different format options (json, csv)
  • Custom delimiters
  • Invalid template expressions
atmos.yaml (1)

79-88: LGTM! Well-structured column configuration.

The list configuration effectively uses Go templates to format the output. The column names and values are clear and meaningful.

Consider adding documentation for template variables.

It would be helpful to add comments explaining the available template variables and their usage.

   list:
+    # Available template variables:
+    # - .atmos_stack: The full stack name
+    # - .vars: Map of stack variables (e.g., tenant, environment)
+    # - .atmos_stack_file: The stack configuration file path
     columns:
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 255a05a and fe422ea.

📒 Files selected for processing (8)
  • atmos.yaml (1 hunks)
  • cmd/list_stacks.go (3 hunks)
  • pkg/list/list_components_test.go (1 hunks)
  • pkg/list/list_stacks.go (2 hunks)
  • pkg/list/list_stacks_test.go (2 hunks)
  • pkg/schema/schema.go (1 hunks)
  • tests/snapshots/TestCLICommands_atmos_describe_config.stdout.golden (2 hunks)
  • tests/snapshots/TestCLICommands_atmos_describe_config_-f_yaml.stdout.golden (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • tests/snapshots/TestCLICommands_atmos_describe_config.stdout.golden
🧰 Additional context used
🪛 golangci-lint (1.62.2)
pkg/list/list_stacks.go

226-226: S1001: should use copy(to, from) instead of a loop

(gosimple)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary
🔇 Additional comments (16)
pkg/list/list_stacks.go (8)

4-4: Imports look good.
No problems found.

Also applies to: 8-8, 10-10


18-30: Validate user inputs and fallback logic.
This section elegantly checks format and falls back to listConfig.Format. Good clarity and error handling.


49-82: Consolidation of stack information is thorough.
Merging vars and component-specific vars ensures comprehensive data in stackInfo. This is well-structured.


85-127: All-stacks listing logic is consistent.
Inclusion of environment details and dynamic fallback to .yaml files aligns with current approach.


129-140: Graceful handling of empty stack lists.
The conditional check returns descriptive messages. Looks good.


141-150: Default columns are adequately defined.
The fallback scenario is well-handled.


151-174: Template parsing and execution.
Error messaging for template issues is concise and clear. Good job.


177-201: JSON and CSV generation is well-structured.
Appending rows is intuitive, and error checks are clean.

tests/snapshots/TestCLICommands_atmos_describe_config_-f_yaml.stdout.golden (2)

28-30: New list configuration section.
This neatly decouples listing requirements under stacks. Good design.


38-44: Path changes applied.
Strong consistency with the new user environment. No issues spotted.

pkg/list/list_stacks_test.go (2)

28-37: Validating empty format and columns in TestListStacks.
The coverage for FilterAndListStacks with a basic listConfig is appropriate.


54-63: Testing component-based listing with columns.
Looked for potential missing negative tests, but the coverage is still acceptable.

cmd/list_stacks.go (2)

22-25: LGTM! Clear and helpful examples.

The examples effectively demonstrate the various output format options available to users.


61-62: LGTM! Well-documented flags.

The flags are properly registered with clear descriptions and appropriate default values.

pkg/schema/schema.go (1)

152-157: LGTM! Well-structured schema definition.

The Stacks struct is properly organized with clear field tags and documentation.

pkg/list/list_components_test.go (1)

57-57: LGTM! Proper test assertion chain.

The test properly validates the output and error handling.

coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 28, 2025
@mergify mergify bot removed the triage Needs triage label Jan 28, 2025
@osterman
Copy link
Member

osterman commented Jan 29, 2025

@osterman
Copy link
Member

osterman commented Jan 29, 2025

  • Update default display to use universal properties of stacks like the stack name, and file. image
  • Provide example in documentation for the current display

atmos.yaml Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (6)
pkg/list/list_stacks_test.go (2)

28-35: Consider extracting duplicated test configuration.

The list configuration is identical in both test functions. Let's refactor this to improve maintainability.

+// Common test configuration
+var defaultTestConfig = schema.ListConfig{
+    Format: "",
+    Columns: []schema.ListColumnConfig{
+        {Name: "Stack", Value: "{{ .atmos_stack }}"},
+        {Name: "File", Value: "{{ .atmos_stack_file }}"},
+    },
+}

 func TestListStacks(t *testing.T) {
     // ...
-    listConfig := schema.ListConfig{
-        Format: "",
-        Columns: []schema.ListColumnConfig{
-            {Name: "Stack", Value: "{{ .atmos_stack }}"},
-            {Name: "File", Value: "{{ .atmos_stack_file }}"},
-        },
-    }
+    listConfig := defaultTestConfig
     // ...
 }

 func TestListStacksWithComponent(t *testing.T) {
     // ...
-    listConfig := schema.ListConfig{
-        Format: "",
-        Columns: []schema.ListColumnConfig{
-            {Name: "Stack", Value: "{{ .atmos_stack }}"},
-            {Name: "File", Value: "{{ .atmos_stack_file }}"},
-        },
-    }
+    listConfig := defaultTestConfig
     // ...
 }

Also applies to: 54-61


111-113: Strengthen error handling tests.

The current error handling is basic. Consider adding specific error case assertions.

 t.Run(test.name, func(t *testing.T) {
     result, err := FilterAndListStacks(context, stacksBasePath, test.config, stackType, component)
-    assert.NoError(t, err)
-    assert.Equal(t, test.expected, result)
+    if test.expectedError != "" {
+        assert.Error(t, err)
+        assert.Contains(t, err.Error(), test.expectedError)
+    } else {
+        assert.NoError(t, err)
+        assert.Equal(t, test.expected, result)
+    }
 })
website/docs/cli/commands/list/list-stacks.mdx (2)

52-60: Enhance documentation with more configuration examples.

The current example is good but limited. Consider adding examples for:

  • Multiple columns configuration
  • Different output formats
  • Complex template expressions
 ```yaml
 stacks:
   display:
     columns:
       - name: Stack
         value: '{{ .atmos_stack }}'
       - name: Tenant
         value: '{{ index .vars "tenant" }}'
+      - name: Environment
+        value: '{{ index .vars "environment" | default "N/A" }}'
+      - name: Components
+        value: '{{ range $key, $value := .components }}{{ $key }}, {{ end }}'
+    format: "table"  # or "json", "csv"

---

`89-95`: **Expand example outputs section.**

Consider adding examples for different output formats and scenarios.

```diff
 $ atmos list stacks
 STACK                  TENANT
 dev                    acme
 staging                acme
 prod                   acme-corp
+
+# JSON output example
+$ atmos list stacks --format json
+[
+  {
+    "Stack": "dev",
+    "Tenant": "acme",
+    "Environment": "development"
+  }
+  ...
+]
+
+# CSV output example
+$ atmos list stacks --format csv
+Stack,Tenant,Environment
+dev,acme,development
+...
pkg/list/list_stacks.go (2)

20-29: Strengthen format validation.

The format validation could be more robust by adding constants for supported formats.

+const (
+    FormatTable = "table"
+    FormatJSON  = "json"
+    FormatCSV   = "csv"
+)

+var supportedFormats = map[string]bool{
+    FormatTable: true,
+    FormatJSON:  true,
+    FormatCSV:   true,
+    "":         true,  // empty string defaults to table
+}

 func ValidateFormat(format string) error {
-    // existing validation
+    if !supportedFormats[format] {
+        return fmt.Errorf("unsupported format %q, supported formats: table, json, csv", format)
+    }
     return nil
 }

223-227: Optimize header copying.

The current implementation uses a loop to copy headers. This can be simplified using the built-in copy function.

-headerRow := make([]string, len(headers))
-for i, h := range headers {
-    headerRow[i] = h
-}
+headerRow := make([]string, len(headers))
+copy(headerRow, headers)
🧰 Tools
🪛 golangci-lint (1.62.2)

224-224: S1001: should use copy(to, from) instead of a loop

(gosimple)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fe422ea and e7d9c73.

📒 Files selected for processing (3)
  • pkg/list/list_stacks.go (2 hunks)
  • pkg/list/list_stacks_test.go (3 hunks)
  • website/docs/cli/commands/list/list-stacks.mdx (1 hunks)
🧰 Additional context used
🪛 LanguageTool
website/docs/cli/commands/list/list-stacks.mdx

[uncategorized] ~68-~68: Possible missing comma found.
Context: ...tion provides a safer way to access map values as it won't cause a template error if t...

(AI_HYDRA_LEO_MISSING_COMMA)

🪛 golangci-lint (1.62.2)
pkg/list/list_stacks.go

224-224: S1001: should use copy(to, from) instead of a loop

(gosimple)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary

pkg/list/list_stacks_test.go Outdated Show resolved Hide resolved
pkg/list/list_stacks.go Outdated Show resolved Hide resolved
pkg/list/list_stacks.go Outdated Show resolved Hide resolved
Copy link
Member

@osterman osterman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, minor nit

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
website/docs/cli/commands/list/list-stacks.mdx (4)

50-60: Add documentation for supported output formats.

The configuration example is clear, but we should also document the supported output formats (JSON, CSV, table) mentioned in the implementation.

Add a section about output formats like this:

 The command output can be customized using template variables in your `atmos.yaml` configuration. Here's an example of how to configure the display format to add a custom field called "Tenant":

+You can specify the output format using the `--output` flag:
+```shell
+atmos list stacks --output json   # JSON format
+atmos list stacks --output csv    # CSV format
+atmos list stacks --output table  # Table format (default)
+```
+
 ```yaml

56-58: Document the .atmos_stack template variable.

The example uses .atmos_stack but doesn't explain what this variable represents.

Add a brief explanation of the .atmos_stack variable and other available template variables.


73-77: Clarify configuration precedence.

The documentation should explain how custom configurations interact with default settings. Do custom columns replace or append to the defaults?

Add a note about configuration behavior:

 You can customize the displayed columns using the `list.stacks.columns` configuration in `atmos.yaml`.
+
+:::note Configuration Behavior
+Custom column configurations will completely replace the default columns. If you want to keep the default columns while adding new ones, you need to include them in your configuration.
+:::

68-68: Add comma for improved readability.

Add a comma after "map values" to improve sentence flow.

-The `index` function provides a safer way to access map values as it won't cause a template error if the key doesn't exist.
+The `index` function provides a safer way to access map values, as it won't cause a template error if the key doesn't exist.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~68-~68: Possible missing comma found.
Context: ...tion provides a safer way to access map values as it won't cause a template error if t...

(AI_HYDRA_LEO_MISSING_COMMA)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e7d9c73 and 3c0282b.

📒 Files selected for processing (1)
  • website/docs/cli/commands/list/list-stacks.mdx (1 hunks)
🧰 Additional context used
🪛 LanguageTool
website/docs/cli/commands/list/list-stacks.mdx

[uncategorized] ~68-~68: Possible missing comma found.
Context: ...tion provides a safer way to access map values as it won't cause a template error if t...

(AI_HYDRA_LEO_MISSING_COMMA)

⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: Build (macos-latest, macos)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Build (ubuntu-latest, linux)
  • GitHub Check: Analyze (go)
  • GitHub Check: website-deploy-preview
  • GitHub Check: Summary

atmos.yaml Outdated Show resolved Hide resolved
Copy link

mergify bot commented Feb 2, 2025

💥 This pull request now has conflicts. Could you fix it @Cerebrovinny? 🙏

@mergify mergify bot added the conflict This PR has conflicts label Feb 2, 2025
coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 2, 2025
cmd/cmd_utils.go Outdated Show resolved Hide resolved
@@ -307,15 +308,22 @@ func ExecuteDescribeStacks(
if err != nil {
return nil, err
}
} else {
} else if atmosConfig.Stacks.NamePattern != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aknysh please check this part. We need to ensure that both name pattern and name templates work.

pkg/list/list_stacks.go Outdated Show resolved Hide resolved
pkg/list/list_stacks.go Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
cmd/cmd_utils.go (1)

708-754: Consider optimizing the stack filtering logic.

The implementation is correct, but we can improve its performance and readability. The current nested loops for path filtering have O(n * m) complexity.

Here's a suggested optimization:

 func stackFlagCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
     configAndStacksInfo := schema.ConfigAndStacksInfo{}
     atmosConfig, err := cfg.InitCliConfig(configAndStacksInfo, true)
     if err != nil {
         return nil, cobra.ShellCompDirectiveNoFileComp
     }

     // Get all stacks
     stacksMap, err := e.ExecuteDescribeStacks(atmosConfig, "", nil, nil, nil, false, true, true, false, nil)
     if err != nil {
         return nil, cobra.ShellCompDirectiveNoFileComp
     }

-    // Extract stack names
-    stackNames := make([]string, 0)
-    for stackName := range stacksMap {
-        // Skip if the stack path is within excluded_paths
-        skip := false
-        for _, excludePath := range atmosConfig.Stacks.ExcludedPaths {
-            if strings.HasPrefix(stackName, excludePath) {
-                skip = true
-                break
-            }
-        }
-        if skip {
-            continue
-        }
-
-        // Only include paths that match included_paths if specified
-        if len(atmosConfig.Stacks.IncludedPaths) > 0 {
-            included := false
-            for _, includePath := range atmosConfig.Stacks.IncludedPaths {
-                if strings.HasPrefix(stackName, includePath) {
-                    included = true
-                    break
-                }
-            }
-            if !included {
-                continue
-            }
-        }
-
-        // Add the stack name if it passes all filters
-        stackNames = append(stackNames, stackName)
-    }
+    // Create a map for O(1) lookups
+    excludedPaths := make(map[string]bool)
+    for _, path := range atmosConfig.Stacks.ExcludedPaths {
+        excludedPaths[path] = true
+    }
+
+    includedPaths := make(map[string]bool)
+    for _, path := range atmosConfig.Stacks.IncludedPaths {
+        includedPaths[path] = true
+    }
+
+    // Helper function to check if a stack should be included
+    isStackIncluded := func(stackName string) bool {
+        // Check excluded paths
+        for excludePath := range excludedPaths {
+            if strings.HasPrefix(stackName, excludePath) {
+                return false
+            }
+        }
+
+        // If no included paths specified, include all non-excluded stacks
+        if len(includedPaths) == 0 {
+            return true
+        }
+
+        // Check included paths
+        for includePath := range includedPaths {
+            if strings.HasPrefix(stackName, includePath) {
+                return true
+            }
+        }
+        return false
+    }
+
+    // Filter stacks
+    stackNames := make([]string, 0, len(stacksMap))
+    for stackName := range stacksMap {
+        if isStackIncluded(stackName) {
+            stackNames = append(stackNames, stackName)
+        }
+    }

     return stackNames, cobra.ShellCompDirectiveNoFileComp
 }

This optimization:

  1. Uses maps for O(1) lookups
  2. Extracts filtering logic into a helper function
  3. Pre-allocates the slice with capacity
  4. Improves readability and maintainability
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8ee545c and 0de25ea.

📒 Files selected for processing (1)
  • cmd/cmd_utils.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (3)
cmd/cmd_utils.go (1)

714-718: Consider caching stack information for better performance.

The ExecuteDescribeStacks call might be expensive for large numbers of stacks. Since this is used for command completion, performance is crucial for user experience.

Consider caching the results with a short TTL to improve responsiveness:

+var stackCache struct {
+    stacks map[string]interface{}
+    expiry time.Time
+}
+
 func stackFlagCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
+    // Check cache first
+    if stackCache.stacks != nil && time.Now().Before(stackCache.expiry) {
+        return extractStackNames(stackCache.stacks), cobra.ShellCompDirectiveNoFileComp
+    }
+
     configAndStacksInfo := schema.ConfigAndStacksInfo{}
     atmosConfig, err := cfg.InitCliConfig(configAndStacksInfo, true)
     if err != nil {
         return nil, cobra.ShellCompDirectiveNoFileComp
     }

     // Get all stacks
     stacksMap, err := e.ExecuteDescribeStacks(atmosConfig, "", nil, nil, nil, false, true, true, false, nil)
     if err != nil {
         return nil, cobra.ShellCompDirectiveNoFileComp
     }
+
+    // Update cache with 5-minute expiry
+    stackCache.stacks = stacksMap
+    stackCache.expiry = time.Now().Add(5 * time.Minute)
pkg/list/list_stacks.go (2)

19-31: Add parameter documentation for improved clarity.

The function signature has been updated with new parameters, but lacks documentation for these parameters.

Add parameter documentation:

 // FilterAndListStacks filters and lists stacks based on the given configuration
+// Parameters:
+//   - stacksMap: Map of stack configurations
+//   - component: Optional component filter
+//   - listConfig: Configuration for list display
+//   - format: Output format (json, csv, tsv, or empty for auto-detect)
+//   - delimiter: Custom delimiter for csv/tsv formats
+// Returns:
+//   - Formatted string output
+//   - Error if any
 func FilterAndListStacks(stacksMap map[string]any, component string, listConfig schema.ListConfig, format string, delimiter string) (string, error) {

356-372: Use consistent table generation approach.

The code uses charmbracelet's table package for TTY output but falls back to manual table generation for non-TTY output. Consider using the same table package for both cases to maintain consistency.

I can help implement a consistent table generation approach using the charmbracelet library. Would you like me to provide the implementation?

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0de25ea and 43fcbaa.

📒 Files selected for processing (2)
  • cmd/cmd_utils.go (1 hunks)
  • pkg/list/list_stacks.go (1 hunks)
🧰 Additional context used
🪛 GitHub Check: Build (macos-latest, macos)
pkg/list/list_stacks.go

[failure] 267-267:
declared and not used: fieldDelimiter


[failure] 271-271:
undefined: fileDelimiter


[failure] 273-273:
undefined: fileDelimiter


[failure] 283-283:
undefined: fileDelimiter

🪛 GitHub Check: Build (windows-latest, windows)
pkg/list/list_stacks.go

[failure] 267-267:
declared and not used: fieldDelimiter


[failure] 271-271:
undefined: fileDelimiter


[failure] 273-273:
undefined: fileDelimiter


[failure] 283-283:
undefined: fileDelimiter

🪛 GitHub Check: Build (ubuntu-latest, linux)
pkg/list/list_stacks.go

[failure] 267-267:
declared and not used: fieldDelimiter


[failure] 271-271:
undefined: fileDelimiter


[failure] 273-273:
undefined: fileDelimiter


[failure] 283-283:
undefined: fileDelimiter

🪛 GitHub Actions: Tests
pkg/list/list_stacks.go

[error] 267-267: Declared and not used: fieldDelimiter.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary
🔇 Additional comments (2)
pkg/list/list_stacks.go (2)

70-74: Add validation for stage extraction from stack name.

The stage extraction logic doesn't validate the stack name format before using it as a stage value.


305-326: Improve TSV handling using CSV writer.

The TSV implementation could be simplified by reusing the CSV writer with a tab delimiter.

cmd/cmd_utils.go Outdated Show resolved Hide resolved
pkg/list/list_stacks.go Outdated Show resolved Hide resolved
- Introduced `BuildStackMetadata` for standardized stack data
- Added `ContainsComponent` to check component existence
- Implemented `HasNonEmptyTemplateValues` for column value checks
- Simplified CSV/TSV formatting logic
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
pkg/list/list_stacks.go (1)

94-97: 🛠️ Refactor suggestion

Improve error handling in template parsing.

The template parsing error is silently ignored by returning false. This could mask configuration issues.

 tmpl, err := template.New(columnName).Parse(templateStr)
 if err != nil {
+    log.Printf("Warning: Failed to parse template for column %s: %v", columnName, err)
     return false
 }
🧹 Nitpick comments (1)
pkg/list/list_stacks.go (1)

296-317: Improve TSV handling using CSV writer.

The TSV implementation could be simplified by reusing the CSV writer with a tab delimiter.

 case FormatTSV:
-    // For TSV, replace tabs with spaces in values and use tab as delimiter
-    // Write headers
-    for i, header := range nonEmptyHeaders {
-        if i > 0 {
-            output.WriteString("\t")
-        }
-        output.WriteString(strings.ReplaceAll(header, "\t", " "))
-    }
-    output.WriteString(utils.GetLineEnding())
-
-    // Write rows
-    for _, row := range rows {
-        for j, i := range nonEmptyColumnIndexes {
-            if j > 0 {
-                output.WriteString("\t")
-            }
-            output.WriteString(strings.ReplaceAll(row[i], "\t", " "))
-        }
-        output.WriteString(utils.GetLineEnding())
-    }
+    // Use CSV writer with tab delimiter for TSV
+    writer := csv.NewWriter(&output)
+    writer.Comma = '\t'
+
+    // Write headers
+    if err := writer.Write(nonEmptyHeaders); err != nil {
+        return "", fmt.Errorf("error writing TSV headers: %w", err)
+    }
+
+    // Write rows
+    for _, row := range rows {
+        tsvRow := make([]string, len(nonEmptyColumnIndexes))
+        for j, i := range nonEmptyColumnIndexes {
+            // Replace tabs with spaces in values
+            tsvRow[j] = strings.ReplaceAll(row[i], "\t", " ")
+        }
+        if err := writer.Write(tsvRow); err != nil {
+            return "", fmt.Errorf("error writing TSV row: %w", err)
+        }
+    }
+    writer.Flush()
+    if err := writer.Error(); err != nil {
+        return "", fmt.Errorf("error flushing TSV writer: %w", err)
+    }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 43fcbaa and 132d9a0.

📒 Files selected for processing (1)
  • pkg/list/list_stacks.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary

pkg/list/list_stacks.go Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
cmd/cmd_utils.go (1)

720-737: Simplify stack completion logic.

The function has been improved but could be further simplified:

  1. Consider using early returns for error cases
  2. Add error logging for better debugging
 func stackFlagCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 	configAndStacksInfo := schema.ConfigAndStacksInfo{}
 	atmosConfig, err := cfg.InitCliConfig(configAndStacksInfo, true)
 	if err != nil {
+		u.LogDebug(fmt.Sprintf("Failed to initialize CLI config: %v", err))
 		return nil, cobra.ShellCompDirectiveNoFileComp
 	}
 
 	// Get all stacks
 	stacksMap, err := e.ExecuteDescribeStacks(atmosConfig, "", nil, nil, nil, false, true, true, false, nil)
 	if err != nil {
+		u.LogDebug(fmt.Sprintf("Failed to execute describe stacks: %v", err))
 		return nil, cobra.ShellCompDirectiveNoFileComp
 	}
 
 	// Filter stack names using configured base path
 	stackNames := filterStackNames(stacksMap, atmosConfig.Stacks.BasePath)
+	if len(stackNames) == 0 {
+		u.LogDebug("No stack names found after filtering")
+	}
 
 	return stackNames, cobra.ShellCompDirectiveNoFileComp
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 132d9a0 and 7b403ba.

📒 Files selected for processing (3)
  • cmd/cmd_utils.go (1 hunks)
  • cmd/cmd_utils_test.go (1 hunks)
  • pkg/list/list_stacks.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary
🔇 Additional comments (6)
cmd/cmd_utils_test.go (1)

1-59: Well-structured test cases with good coverage!

The test implementation is thorough and covers essential scenarios:

  • Filtering with standard base path
  • Handling custom base paths
  • Edge case with empty base path
pkg/list/list_stacks.go (4)

19-72: Well-documented metadata builder with comprehensive error handling.

The function effectively organizes stack data into a consistent structure while handling nil values and nested components appropriately.


57-62: Add validation for stage extraction from stack name.

The stage extraction logic should validate the stack name format before using it as a stage value.


86-111: Improve error handling in template parsing.

The function silently returns false on template parsing errors, which could mask configuration issues.


296-317: Improve TSV handling using CSV writer.

The TSV implementation could be simplified by reusing the CSV writer with a tab delimiter.

cmd/cmd_utils.go (1)

707-718: Avoid hardcoding path checks.

The hardcoded check for base path prefix is not aligned with Atmos's configurable nature. This could break if users configure different stack paths.

@@ -704,12 +704,36 @@ func showUsageExample(cmd *cobra.Command, details string) {
u.PrintInvalidUsageErrorAndExit(errors.New(details), suggestion)
}

// filterStackNames filters out file paths and returns valid stack names
func filterStackNames(stacksMap map[string]any, basePath string) []string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s revise this function name. We want to add proper filtering later (not this PR), but this is filtering on paths. Also, it’s not clear to me why we need this. Can you add a comment explaining why we need this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this wold get replaced once we refactor the full stacks path on the discussion bellow

stackNames := make([]string, 0)
for stackName := range stacksMap {
// Skip if it's a file path (starts with base path)
if basePath != "" && strings.HasPrefix(stackName, basePath+"/") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is not right either. Stacks can exist anywhere, what we include and exclude is entirely config driven. We are not consulting that configuration, and deriving an implementation that doesn’t correspond to the documentation. We should have a centralized function for this and it relates to stacks not general utils, IMO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I think on this case would be best having that function inside stacks pkg and then reuse FindAllStackConfigsInPaths from config to find the paths

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aknysh please let me know your opinion of whats the best approach to refactor this, I see that we also have getConfigAndStacksInfo in utils

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Needs triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants