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

refactor checkAtmosConfig #861

Closed
wants to merge 25 commits into from
Closed

Conversation

mcalhoun
Copy link
Member

@mcalhoun mcalhoun commented Dec 16, 2024

what

Refactor the checkAtmosConfig code so that the config is loaded once in the root command, checked and added to the context for other commands to use.

why

This code was being called multiple times, which decreased performance in was unnecessary.

Summary by CodeRabbit

  • New Features

    • Enhanced handling of Atmos configuration across multiple commands, ensuring correct retrieval and type-checking.
  • Bug Fixes

    • Improved error handling when Atmos configuration is missing or incorrectly typed.
  • Documentation

    • Comments refined for clarity in the handling of Atmos configuration.
  • Refactor

    • Removed unnecessary configuration checks from various commands to streamline execution flow.

@mcalhoun mcalhoun marked this pull request as ready for review December 16, 2024 17:50
@mcalhoun mcalhoun requested review from a team as code owners December 16, 2024 17:50
@mcalhoun mcalhoun requested review from Gowiem and jamengual December 16, 2024 17:50
@osterman osterman requested review from aknysh and removed request for Gowiem and jamengual December 16, 2024 17:52
Copy link
Contributor

coderabbitai bot commented Dec 16, 2024

📝 Walkthrough

Walkthrough

This pull request introduces a comprehensive refactoring of command configuration checks across multiple Atmos CLI commands. The primary modification involves removing the checkAtmosConfig() function calls from various command implementations, replacing them with a more centralized configuration retrieval mechanism using command context. The changes aim to streamline configuration handling and error management while preserving the existing command execution logic.

Changes

File Change Summary
cmd/cmd_utils.go Updated checkAtmosConfig function signature to accept *schema.CliConfiguration
cmd/root.go Introduced context-based configuration handling and renamed cliConfig to atmosConfig
Multiple command files Removed checkAtmosConfig() calls and added context-based configuration retrieval

Possibly related PRs

Suggested Labels

patch

Suggested Reviewers

  • osterman
  • aknysh
  • Gowiem

By Sparta's shield, this refactoring looks clean and methodical! The changes maintain the spirit of the original implementation while introducing a more flexible configuration retrieval mechanism. Onward! 🛡️


📜 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 5dab26e and e457aa9.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (1)
  • cmd/vendor_pull.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • cmd/vendor_pull.go

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 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.

@mcalhoun mcalhoun changed the title refactor checkAtmoConfig refactor checkAtmosConfig Dec 16, 2024
@mcalhoun mcalhoun added the no-release Do not create a new release (wait for additional code changes) label Dec 16, 2024
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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 44b2384 and 5b74f03.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (6)
  • cmd/cmd_utils.go (3 hunks)
  • cmd/helmfile.go (1 hunks)
  • cmd/root.go (5 hunks)
  • cmd/terraform.go (1 hunks)
  • cmd/version.go (1 hunks)
  • go.mod (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • cmd/terraform.go
🧰 Additional context used
📓 Learnings (2)
cmd/version.go (1)
Learnt from: Listener430
PR: cloudposse/atmos#844
File: cmd/version.go:34-44
Timestamp: 2024-12-13T15:28:13.630Z
Learning: In `cmd/version.go`, when handling the `--check` flag in the `versionCmd`, avoid using `CheckForAtmosUpdateAndPrintMessage(cliConfig)` as it updates the cache timestamp, which may not be desired in this context.
cmd/cmd_utils.go (1)
Learnt from: osterman
PR: cloudposse/atmos#740
File: cmd/cmd_utils.go:340-359
Timestamp: 2024-11-12T05:52:05.088Z
Learning: In the Go codebase for Atmos, when reviewing functions like `checkAtmosConfig` in `cmd/cmd_utils.go`, avoid suggesting refactoring to return errors instead of calling `os.Exit` if such changes would significantly increase the scope due to the need to update multiple call sites.
🔇 Additional comments (6)
cmd/root.go (3)

22-23: Strong type safety for context keys!

Good practice using a custom type for context keys to prevent accidental collisions.


127-131: Excellent refactoring of configuration handling!

The changes effectively centralize config validation and make it available throughout the command tree. This is a significant improvement that:

  1. Reduces redundant config checks
  2. Ensures consistent config state
  3. Makes the config easily accessible to all subcommands

95-98: Verify error handling for config initialization

The error handling looks correct, but we should ensure the true parameter passed to InitCliConfig is documented.

Consider adding a named constant for the boolean parameter to improve code readability:

-atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true)
+const loadCustomCommands = true
+atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, loadCustomCommands)
✅ Verification successful

Boolean parameter is well-documented, but could be more explicit

The processStacks parameter is documented in the function's context, but let's make it more explicit for better maintainability:

-atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true)
+// Set to true to process custom commands from config
+const processCustomCommands = true
+atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, processCustomCommands)

The function's documentation in pkg/config/config.go clearly shows its purpose of finding and merging CLI configurations from various sources (system dir, home dir, current dir, ENV vars). The boolean parameter is consistently used across the codebase, with false only used in describe_config.go where stack processing isn't needed.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if the parameter is documented in the function definition
ast-grep --pattern 'func InitCliConfig($_, $bool) {
  $$$
}'

Length of output: 62


Script:

#!/bin/bash
# Let's search for the InitCliConfig function definition with ripgrep for better context
rg "func InitCliConfig" -A 5

# Also search for any documentation comments above the function
rg "InitCliConfig" -B 3

Length of output: 18711

go.mod (1)

89-89: Verify AWS SSM SDK dependency usage.

The addition of the AWS SSM SDK suggests new AWS Systems Manager functionality. Let's verify its usage in the codebase.

cmd/cmd_utils.go (2)

Line range hint 367-383: LGTM! Good refactoring of configuration handling.

The refactoring improves the function by:

  1. Removing global state dependency
  2. Making configuration flow explicit through parameters
  3. Maintaining existing error handling pattern (as per previous feedback)

479-481: LGTM! Consistent with the configuration refactoring.

The changes align with the broader refactoring by retrieving the configuration from the command context instead of global state.

cmd/helmfile.go Outdated Show resolved Hide resolved
cmd/version.go Outdated Show resolved Hide resolved
coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 16, 2024
coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 16, 2024
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 (2)
cmd/root.go (2)

87-90: Request for clarity on the boolean parameter, fellow warrior! 🤔

While moving the configuration initialization to the root level is a solid improvement, the true parameter in cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true) needs clarification. Consider adding a comment explaining its purpose or using a named constant to make the intent clear.


119-123: Excellent refactoring of configuration flow, warrior! 🎯

Moving the config validation to the root level and sharing it via context is a strong architectural improvement. Consider adding error handling for the context setup to maintain robustness:

 checkAtmosConfig(&atmosConfig)

 // Now that we have fully parsed the Atmos config, add it to the context so it can be used by subcommands
 ctx := context.WithValue(context.Background(), contextKey("atmos_config"), atmosConfig)
+if ctx == nil {
+    u.LogErrorAndExit(atmosConfig, errors.New("failed to create context with atmos config"))
+}
 RootCmd.SetContext(ctx)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0662bec and fec4a7c.

📒 Files selected for processing (5)
  • cmd/cmd_utils.go (3 hunks)
  • cmd/helmfile.go (2 hunks)
  • cmd/root.go (4 hunks)
  • cmd/terraform.go (2 hunks)
  • cmd/version.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • cmd/helmfile.go
  • cmd/terraform.go
  • cmd/cmd_utils.go
  • cmd/version.go
🔇 Additional comments (2)
cmd/root.go (2)

22-23: Strong work on type safety, warrior! 💪

The contextKey type alias is a solid defensive programming practice that prevents accidental context key collisions. This follows Go's best practices for context value keys.


107-116: Verify error handling consistency across command processing! 🛡️

The error handling for custom commands and aliases looks robust. Let's verify it's consistent across the codebase.

✅ Verification successful

Error handling is consistent and well-implemented! 💪

The error handling in both processCustomCommands and processCommandAliases follows a robust and consistent pattern:

  • processCustomCommands properly propagates errors up the call chain
  • processCommandAliases correctly uses LogErrorAndExit for critical execution failures
  • Both functions maintain consistent error handling with the rest of the codebase in cmd/root.go
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other error handling patterns in command processing
rg "LogErrorAndExit.*atmosConfig" --type go

Length of output: 45


Script:

#!/bin/bash
# Let's search for error handling patterns more broadly
rg "LogErrorAndExit" -A 2 --type go

# Also check for the processCustomCommands and processCommandAliases usage
ast-grep --pattern 'processCustomCommands($$$)'
ast-grep --pattern 'processCommandAliases($$$)'

# Look for similar error handling patterns
rg "if err != nil \{" -A 2 -B 2 --type go

Length of output: 80007


Script:

#!/bin/bash
# Let's check the error handling in processCustomCommands and processCommandAliases
ast-grep --pattern 'func processCustomCommands($$$)'
ast-grep --pattern 'func processCommandAliases($$$)'

# Also check for similar error handling patterns in cmd/root.go
rg "LogErrorAndExit" -B 2 -A 2 cmd/root.go

Length of output: 7190

coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 17, 2024
@aknysh
Copy link
Member

aknysh commented Dec 18, 2024

@mcalhoun please resolve the conflicts

Copy link

mergify bot commented Dec 18, 2024

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

@mergify mergify bot added the conflict This PR has conflicts label Dec 18, 2024
@mergify mergify bot removed the conflict This PR has conflicts label Dec 18, 2024
Copy link

mergify bot commented Dec 18, 2024

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

@mergify mergify bot added the conflict This PR has conflicts label Dec 18, 2024
@mcalhoun
Copy link
Member Author

Closing this PR in favor of #869

@mcalhoun mcalhoun closed this Dec 19, 2024
@mcalhoun mcalhoun deleted the chore/refactor-checkatmosconfig branch December 19, 2024 02:25
@mergify mergify bot removed the conflict This PR has conflicts label Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-release Do not create a new release (wait for additional code changes)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants