Skip to content

fix(server): improve flag parsing #48

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

Merged
merged 3 commits into from
Apr 16, 2025
Merged

fix(server): improve flag parsing #48

merged 3 commits into from
Apr 16, 2025

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Apr 15, 2025

Overview

Summary by CodeRabbit

  • Refactor
    • Simplified server startup by removing dependency on external command objects and improving configuration loading.
  • Chores
    • Updated dependency versions related to core modules for improved stability and performance.

Copy link
Contributor

coderabbitai bot commented Apr 15, 2025

Walkthrough

This update modifies the server startup process by removing the rootCmd *cobra.Command parameter from the StartHandler, startInProcess, and startNode functions. Configuration loading now relies solely on the Viper instance embedded in the server context instead of using a Cobra command. The import of the Cobra package is removed accordingly. Additionally, the go.mod file updates the versions of three github.com/rollkit/rollkit related dependencies to newer commits. No new exported entities are introduced.

Changes

File(s) Change Summary
go.mod Updated versions of github.com/rollkit/rollkit, github.com/rollkit/rollkit/core, and github.com/rollkit/rollkit/da to newer commits.
server/start.go Removed rootCmd *cobra.Command parameter from StartHandler, startInProcess, and startNode. Configuration loading now uses Viper from server context. Removed Cobra import.

Possibly related PRs

  • refactor: improve start handler api #41: Modifies server/start.go by removing the rootCmd *cobra.Command parameter from startup functions and switching configuration loading to use Viper in server context.

Suggested reviewers

  • facundomedica
  • tac0turtle

Poem

In the warren, code takes flight,
Root commands now out of sight.
Flags and configs hop in line,
Viper whispers, “All is fine.”
Startup’s simpler, less to dread—
A rabbit’s joy: clean code ahead!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 10328c0 and 1602d37.

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

🪧 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.
  • @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 to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

server/start.go Outdated
@@ -272,7 +273,14 @@ func startNode(

nodeKey := &key.NodeKey{PrivKey: signingKey, PubKey: signingKey.GetPublic()}

rollkitcfg, err := config.Load(rootCmd)
cmd := &cobra.Command{}
Copy link
Member Author

Choose a reason for hiding this comment

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

I went this way, Load would work if it was called on start directly.
I think for other protocols loading directly the flags from the command is what makes sense. In the SDK we have another pre-processing flow and create dynamically the command.
I was thinking, maybe adding a LoadFromViper in rollkit as well, but I think it doesn't really makes sense either.

@julienrbrt julienrbrt marked this pull request as ready for review April 15, 2025 14:57
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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 512a6a1 and 10328c0.

📒 Files selected for processing (2)
  • go.mod (1 hunks)
  • server/start.go (7 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
server/start.go (1)
rpc/rpc.go (1)
  • RPCServer (57-65)
🪛 golangci-lint (1.64.8)
server/start.go

279-279: Error return value of flags.Set is not checked

(errcheck)

🔇 Additional comments (7)
go.mod (1)

253-253: Marking pflag as a direct dependency is appropriate.

This change aligns with the refactoring in server/start.go, where pflag is now explicitly used for constructing the flag set. No further concerns here.

server/start.go (6)

32-32: New pflag import is consistent with usage.

The import is required for dynamically constructing the flag set. This looks good.


63-63: Refactored StartHandler signature.

Removing the rootCmd parameter and returning a StartCommandHandler promotes clearer separation of concerns and centralizes flag construction in one place. Good approach.


83-83: Delegating to startInProcess is consistent with the new design.

Replacing direct references to an external command with an in-process function call matches the refactoring goal of avoiding rootCmd usage.


123-123: Updated call to startNode without rootCmd.

The simplified invocation demonstrates the intended decoupling from any externally defined command. No issues found.


261-261: Parameter refactoring for startNode.

Accepting srvCtx directly instead of separate parameters is a clean approach, consolidating the server context in one place.


265-265: Using a dedicated logger for Rollkit.

Defining logger := srvCtx.Logger.With("module", "rollkit") helps isolate logs for debugging the Rollkit node. No issues here.

server/start.go Outdated
Comment on lines 276 to 283
cmd := &cobra.Command{}
flags := &pflag.FlagSet{}
for key, value := range srvCtx.Viper.AllSettings() {
flags.Set(key, fmt.Sprintf("%v", value))
}
cmd.Flags().AddFlagSet(flags)

rollkitcfg, err := config.Load(cmd)
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Check the error returned by flags.Set.

Ignoring this error may lead to subtle misconfigurations if the flag setting fails. Handle or log the error to improve reliability.

Here is a possible fix:

 for key, value := range srvCtx.Viper.AllSettings() {
-	flags.Set(key, fmt.Sprintf("%v", value))
+	if err := flags.Set(key, fmt.Sprintf("%v", value)); err != nil {
+		logger.Error("failed to set flag", "key", key, "value", value, "err", err)
+	}
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
cmd := &cobra.Command{}
flags := &pflag.FlagSet{}
for key, value := range srvCtx.Viper.AllSettings() {
flags.Set(key, fmt.Sprintf("%v", value))
}
cmd.Flags().AddFlagSet(flags)
rollkitcfg, err := config.Load(cmd)
cmd := &cobra.Command{}
flags := &pflag.FlagSet{}
for key, value := range srvCtx.Viper.AllSettings() {
if err := flags.Set(key, fmt.Sprintf("%v", value)); err != nil {
logger.Error("failed to set flag", "key", key, "value", value, "err", err)
}
}
cmd.Flags().AddFlagSet(flags)
rollkitcfg, err := config.Load(cmd)
🧰 Tools
🪛 golangci-lint (1.64.8)

279-279: Error return value of flags.Set is not checked

(errcheck)

@tac0turtle tac0turtle requested a review from Copilot April 15, 2025 18:52
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.

Files not reviewed (1)
  • go.mod: Language not supported
Comments suppressed due to low confidence (1)

server/start.go:277

  • [nitpick] Consider using pflag.NewFlagSet() instead of directly instantiating a FlagSet, and handle potential errors returned by flags.Set to ensure robust flag parsing.
flags := &pflag.FlagSet{}

@julienrbrt julienrbrt marked this pull request as draft April 15, 2025 19:38
julienrbrt added a commit to rollkit/rollkit that referenced this pull request Apr 16, 2025
Ref:
rollkit/go-execution-abci#48 (comment)
I eventually went with `LoadFromViper`, as the flags are added in the
start command and i would have had to duplicate most of the logic
otherwise.

`LoadFromViper` should be used for the SDK, as it does some
prepossessing of flags. `Load` should be used for any other use case
(non SDK based chains).
@julienrbrt julienrbrt marked this pull request as ready for review April 16, 2025 08:15
@julienrbrt julienrbrt merged commit b266d3e into main Apr 16, 2025
9 of 11 checks passed
@julienrbrt julienrbrt deleted the julien/fix-flags branch April 16, 2025 09:18
@github-project-automation github-project-automation bot moved this to Done in Rollkit Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants