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

Fix: Flags reported as falsy despite being set by the user when invoking a connector #234

Merged
merged 3 commits into from
Oct 2, 2024

Conversation

shackra
Copy link
Contributor

@shackra shackra commented Oct 2, 2024

when passing values throught the CLI, the connector will report that no value was passed, example:

baton-ldap --user-dn "CN=admin,DC=example,DC=org" --base-dn "dc=example,dc=org" --domain localhost --password admin
at least one field was expected, any of: ('domain' and 'url')

Please note that --domain was set with localhost

This PR fixes that. Proof with baton-ldap:

[nix-shell:~/code/work/baton-ldap]$ ./dist/linux_amd64/baton-ldap --user-dn "CN=admin,DC=example,DC=org" --base-dn "dc=example,dc=org" --domain localhost --password admin
{"level":"info","ts":1727827677.0156295,"caller":"grpclog/component.go:36","msg":"[core][Server #1]Server created","system":"grpc","grpc_log":true}
{"level":"info","ts":1727827677.0157099,"caller":"grpclog/component.go:36","msg":"[core][Server #1 ListenSocket #2]ListenSocket created","system":"grpc","grpc_log":true}
{"level":"info","ts":1727827677.023758,"caller":"sync/syncer.go:346","msg":"Syncing resource types..."}
{"level":"info","ts":1727827677.0251496,"caller":"sync/syncer.go:410","msg":"Syncing resources..."}
{"level":"info","ts":1727827677.0282617,"caller":"sync/syncer.go:576","msg":"Syncing entitlements..."}
{"level":"info","ts":1727827677.0285046,"caller":"sync/syncer.go:936","msg":"Syncing grants..."}
{"level":"info","ts":1727827677.028829,"caller":"sync/syncer.go:291","msg":"Sync complete."}
{"level":"info","ts":1727827677.0289242,"caller":"dotc1z/sync_runs.go:435","msg":"Cleaning up old sync data..."}
{"level":"info","ts":1727827677.0293791,"caller":"dotc1z/sync_runs.go:441","msg":"Removed old sync data.","sync_date":"2024-09-20T16:02:08Z","sync_id":"2mLxbT8oeMbFr5T4kA2fC3LCmgk"}

[nix-shell:~/code/work/baton-ldap]$

Summary by CodeRabbit

  • New Features

    • Enhanced command-line interface with improved clarity in command handling.
    • Simplified flag binding process, ensuring better integration with configuration settings.
  • Bug Fixes

    • Resolved potential errors related to missing flags when values are passed through environment variables.
  • Documentation

    • Updated comments for better understanding of the new flag binding logic.

`command.Flags()` already lists all persistent and regular flags
@shackra shackra requested a review from jirwin as a code owner October 2, 2024 00:15
Copy link

coderabbitai bot commented Oct 2, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The pull request introduces updates to the MakeMainCommand, MakeGRPCServerCommand, and MakeCapabilitiesCommand functions in pkg/cli/commands.go, changing the function signatures for clarity. Additionally, command flags are now bound to the Viper instance using v.BindPFlags(cmd.Flags()), enhancing configuration consistency. In pkg/config/config.go, the DefineConfiguration function has been modified to simplify the flag binding process by removing persistent flag bindings and instead using the VisitAll method to set flag values directly from Viper. Comments have been updated for clarity.

Changes

File Change Summary
pkg/cli/commands.go Updated function signatures in MakeMainCommand, MakeGRPCServerCommand, and MakeCapabilitiesCommand for clarity. Added flag binding to Viper using v.BindPFlags(cmd.Flags()) with error handling.
pkg/config/config.go Removed binding of persistent flags to Viper; implemented setting flag values directly from Viper using VisitAll. Updated comments for clarity.

Possibly related PRs

  • Use correct name for additional commands. #229: The changes in this PR involve modifications to the command structure in pkg/config/config.go, which may relate to the command handling improvements made in the main PR, particularly regarding how commands are defined and configured.

Suggested reviewers

  • jirwin

Poem

In the land of code where rabbits play,
Functions updated in a clearer way.
Flags now dance with Viper's grace,
Simplifying tasks in this happy space.
Hops of joy as we code anew,
A brighter path for all to pursue! 🐇✨


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

Copy link
Contributor

@ggreer ggreer left a comment

Choose a reason for hiding this comment

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

It'd be nice to have a test case that fails if we somehow get this behavior again, but this is definitely better than what's in master.

Copy link
Contributor

@ggreer ggreer left a comment

Choose a reason for hiding this comment

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

Actually, it looks like if I use this branch with baton-ldap, global options like --log-level=debug don't get applied. Config options specified in the connector (like --base-dn and --password) do work correctly now though.

Copy link

@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

🧹 Outside diff range and nitpick comments (1)
pkg/config/config.go (1)

97-99: Consider handling potential errors from the Set method.

The current implementation ignores the error returned by the Set method. While this approach works, it might hide potential issues if a flag fails to be set correctly.

Consider handling these errors, perhaps by logging them or returning an error from the DefineConfiguration function if any flag fails to be set. This would provide better visibility into any issues that might occur during the flag-setting process.

Here's a suggestion for handling the error:

if err := mainCMD.Flags().Set(f.Name, v.GetString(f.Name)); err != nil {
    return nil, nil, fmt.Errorf("failed to set flag %s: %w", f.Name, err)
}

Apply similar changes to the other two command blocks as well.

Also applies to: 103-105, 109-111

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 43972fc and 5082a78.

📒 Files selected for processing (2)
  • pkg/cli/commands.go (6 hunks)
  • pkg/config/config.go (1 hunks)
🔇 Additional comments (5)
pkg/config/config.go (1)

91-114: LGTM! This change addresses the PR objective.

The new code block effectively resolves the issue of flags being reported as falsy despite being set by the user when invoking a connector. By iterating through all flags for each command and setting their values from Viper, it ensures that flags set through environment variables are properly reflected in the Cobra command flags.

This implementation prevents Cobra from raising errors about missing flags when values are passed through environment variables, which aligns with the PR objective.

pkg/cli/commands.go (4)

36-36: Approve changes: Flag binding implementation addresses the reported issue.

The modifications to MakeMainCommand effectively address the issue of flags being reported as falsy despite being set by the user. Key improvements include:

  1. Updated function signature for better readability (line 36).
  2. Implementation of flag binding to Viper (lines 57-65), ensuring all flags (both persistent and regular) are properly communicated from Cobra to Viper.

These changes should resolve the reported issue by guaranteeing that flag values are correctly propagated throughout the command execution.

Also applies to: 57-65


189-189: Approve changes: Consistent flag binding implementation.

The modifications to MakeGRPCServerCommand are consistent with those in MakeMainCommand:

  1. Updated function signature for better readability (line 189).
  2. Implementation of flag binding to Viper (lines 200-208), ensuring all flags (both persistent and regular) are properly communicated from Cobra to Viper.

These changes maintain consistency across the codebase and contribute to resolving the reported issue with flag values.

Also applies to: 200-208


303-303: Approve changes: Consistent flag binding implementation across all commands.

The modifications to MakeCapabilitiesCommand complete the consistent implementation of flag binding across all three main commands:

  1. Updated function signature for better readability (line 303).
  2. Implementation of flag binding to Viper (lines 314-322), ensuring all flags (both persistent and regular) are properly communicated from Cobra to Viper.

These changes maintain consistency across the entire codebase and should fully resolve the reported issue with flag values.

Also applies to: 314-322


Line range hint 36-322: Suggestion: Consider additional testing to validate the fix.

The changes implemented across all three main functions (MakeMainCommand, MakeGRPCServerCommand, and MakeCapabilitiesCommand) consistently address the reported issue of flags being reported as falsy despite being set by the user. The new flag binding implementation should ensure that all flag values are correctly propagated from Cobra to Viper.

To further validate the effectiveness of this fix:

  1. Consider adding unit tests that specifically check the flag binding behavior.
  2. Perform integration tests with various flag combinations to ensure they are correctly recognized across different command scenarios.
  3. If possible, reproduce the original issue in a test case and verify that it no longer occurs with these changes.

These additional tests will help ensure the robustness of the fix and prevent potential regressions in the future.

@shackra
Copy link
Contributor Author

shackra commented Oct 2, 2024

It'd be nice to have a test case that fails if we somehow get this behavior again, but this is definitely better than what's in master.

that's the thing, I think internal/tests/passingvalues_test.go covers that, and regardless of the issue the tests were passing 😆 that's why I was freaked out by this bug haha.

@shackra
Copy link
Contributor Author

shackra commented Oct 2, 2024

Actually, it looks like if I use this branch with baton-ldap, global options like --log-level=debug don't get applied. Config options specified in the connector (like --base-dn and --password) do work correctly now though.

huh

@shackra shackra enabled auto-merge (squash) October 2, 2024 00:31
Copy link
Contributor

@ggreer ggreer left a comment

Choose a reason for hiding this comment

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

I tested this and it behaves correctly now.

@shackra shackra merged commit 0c4dac7 into main Oct 2, 2024
4 checks passed
@shackra shackra deleted the shackra/bb-132 branch October 2, 2024 00:32
@coderabbitai coderabbitai bot mentioned this pull request Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants