-
Notifications
You must be signed in to change notification settings - Fork 129
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
chore(cli): refactor and cleanup config #1793
Conversation
WalkthroughThe changes introduce a comprehensive overhaul of the configuration management within the CLI application. Key updates include the refactoring of context initialization, enhancement of configuration handling through new modules, and a shift to improved type usage in function signatures. This restructuring aims to streamline processes, increase maintainability, and foster better integration of application settings, all while adhering to modern Go conventions. Changes
Sequence Diagram(s)sequenceDiagram
participant CLIBuilder
participant Config
participant Viper
participant Logger
participant ServerContext
CLIBuilder->>Config: Call SetupConfigAndContext()
Config->>Viper: Initialize new Viper instance
Viper->>Config: Read configurations
Config->>Logger: Bind flags and set up logging
Config->>ServerContext: Create Server Context
ServerContext-->>CLIBuilder: Return initialized context
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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 as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1793 +/- ##
=======================================
Coverage 25.42% 25.43%
=======================================
Files 327 329 +2
Lines 14334 14332 -2
Branches 19 19
=======================================
Hits 3645 3645
+ Misses 10566 10564 -2
Partials 123 123
|
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Files selected for processing (6)
- mod/cli/pkg/builder/builder.go (2 hunks)
- mod/cli/pkg/config/app.go (1 hunks)
- mod/cli/pkg/config/client.go (1 hunks)
- mod/cli/pkg/config/comet.go (1 hunks)
- mod/cli/pkg/config/server.go (1 hunks)
- mod/cli/pkg/utils/context/server_context.go (2 hunks)
Additional comments not posted (16)
mod/cli/pkg/config/client.go (3)
23-23
: LGTM! The import alias improves clarity.Aliasing the import helps in distinguishing between the package's functions and types when referenced in the code.
27-27
: LGTM! The return type update enhances readability.Updating the return type from
interface{}
toany
improves code readability and aligns with Go's newer type conventions.
28-28
: LGTM! The variable renaming improves clarity.Renaming the variable to
clientConfig
makes it more descriptive of its purpose.mod/cli/pkg/utils/context/server_context.go (3)
24-30
: LGTM! The updated import statements reflect new dependencies.The updated import statements include the
viper
package and rename thelog
package tosdklog
, reflecting the new dependencies and the shift towards a more structured logging approach.
33-44
: LGTM! The new function enhances modularity.The
CreateServerContext
function centralizes the context initialization logic, enhancing modularity and reusability.
54-54
: LGTM! The modification simplifies the code.The
GetServerContextFromCmd
function now callsCreateServerContext
, simplifying the code and ensuring consistent setup of the logger and configuration across different parts of the application.mod/cli/pkg/config/comet.go (2)
32-71
: LGTM! The function handles comet config effectively.The
handleCometConfig
function reads the comet config file into the provided viper instance and updates the comet config with the latest values from viper, with appropriate error handling.
74-104
: LGTM! The function ensures the comet config is created and validated.The
writeCometConfig
function creates a new comet config file with default values or reads and merges it into the provided viper instance if it exists, with appropriate error handling for validation and file operations.mod/cli/pkg/config/app.go (2)
33-63
: Ensure proper error handling for file operations.The function correctly handles file operations and errors, ensuring that configurations are either written or merged appropriately.
65-107
: Ensure consistency in handlingappTemplate
andappConfig
.The function correctly ensures that both
appTemplate
andappConfig
are either provided or not. Error handling is appropriately managed for unmarshalling and writing configurations.mod/cli/pkg/builder/builder.go (1)
163-169
: Verify the correctness ofconfig.SetupConfigAndContext
usage.The function now uses
config.SetupConfigAndContext
to handle configuration and context setup. Ensure that this new function call correctly replaces the previous functionality.mod/cli/pkg/config/server.go (5)
41-69
: Ensure proper context initialization and error handling.The function correctly initializes the server context and handles configuration files. Error handling is appropriately managed.
72-95
: Ensure proper context initialization and flag binding.The function correctly initializes the server context and binds flags to the viper instance. Error handling is appropriately managed.
97-105
: Ensure proper viper instance initialization.The function correctly initializes a new viper instance with an environment prefix and sets up environment variable handling.
107-115
: Ensure proper base name extraction and error handling.The function correctly retrieves the executable name and extracts the base name. Error handling is appropriately managed.
117-150
: Ensure proper flag binding and error handling.The function correctly binds command line flags to the viper instance and handles errors appropriately.
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Files selected for processing (1)
- mod/cli/pkg/config/app.go (1 hunks)
Additional comments not posted (2)
mod/cli/pkg/config/app.go (2)
36-63
: Ensure proper error handling and edge case management.The function
handleAppConfig
appears to handle configuration files correctly, but consider the following improvements:
- Error Handling: Ensure that all potential errors are logged or handled appropriately.
- Edge Cases: Consider scenarios where the
configDirPath
is invalid or inaccessible.Verification successful
The
handleAppConfig
function is called inmod/cli/pkg/config/server.go
, and its error is handled appropriately there. However, we need to ensure thathandleAppConfig
itself manages edge cases and errors correctly, particularly for invalid or inaccessibleconfigDirPath
.Ensure proper error handling and edge case management.
The function
handleAppConfig
appears to handle configuration files correctly, but consider the following improvements:
- Error Handling: Ensure that all potential errors are logged or handled appropriately.
- Edge Cases: Consider scenarios where the
configDirPath
is invalid or inaccessible.
- File:
mod/cli/pkg/config/app.go
- Function:
handleAppConfig
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify error handling and edge cases in `handleAppConfig`. # Test: Search for function calls to `handleAppConfig` and check for proper error handling. rg --type go -A 5 $'handleAppConfig'Length of output: 873
67-106
: Improve error handling and code readability.The function
writeAppConfig
appears to handle the configuration writing process correctly, but consider the following improvements:
- Error Handling: Ensure that all potential errors are logged or handled appropriately.
- Edge Cases: Consider scenarios where the
appConfigFilePath
is invalid or inaccessible.- Code Readability: The nested if-else blocks can be simplified for better readability.
if appTemplatePopulated { // set the config template if err = config.SetConfigTemplate(appTemplate); err != nil { return fmt.Errorf("failed to set config template: %w", err) } // populate appConfig with the values from the viper instance if err = rootViper.Unmarshal(&appConfig); err != nil { return fmt.Errorf("failed to unmarshal app config: %w", err) } } else { // read the appConfig from the file at appConfigFilePath appConfig, err = config.ParseConfig(rootViper) if err != nil { return fmt.Errorf("failed to parse %s: %w", appConfigFilePath, err) } } // write the appConfig to the file at appConfigFilePath if err = config.WriteConfigFile(appConfigFilePath, appConfig); err != nil { return fmt.Errorf("failed to write %s: %w", appConfigFilePath, err) }Verification successful
Error handling is properly implemented.
The
writeAppConfig
function's errors are correctly propagated throughhandleAppConfig
and checked in the calling function. This ensures that any errors are not ignored and are handled appropriately.
mod/cli/pkg/config/server.go
: Errors fromhandleAppConfig
are checked and returned.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify error handling and edge cases in `writeAppConfig`. # Test: Search for function calls to `writeAppConfig` and check for proper error handling. rg --type go -A 5 $'writeAppConfig'Length of output: 734
Script:
#!/bin/bash # Description: Retrieve more context around the call to `writeAppConfig` to check for error handling. # Get 10 lines before and after the call to `writeAppConfig` in mod/cli/pkg/config/app.go rg --type go -C 10 $'writeAppConfig' mod/cli/pkg/config/app.goLength of output: 1150
Script:
#!/bin/bash # Description: Retrieve context around calls to `handleAppConfig` to check for error handling. # Search for calls to `handleAppConfig` and get 10 lines before and after each call. rg --type go -C 10 $'handleAppConfig'Length of output: 2252
Summary by CodeRabbit
New Features
Bug Fixes
Refactor