-
Notifications
You must be signed in to change notification settings - Fork 473
fix(txsim): pollTime CLI flag should override environment variable #5268
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
base: main
Are you sure you want to change the base?
fix(txsim): pollTime CLI flag should override environment variable #5268
Conversation
utack @Galoretka out of curiousity what you are using txsim for or how did you come across this? |
test/cmd/txsim/cli.go
Outdated
// getPollTime returns the pollTime value based on CLI flag, environment variable, and default. | ||
// Priority: flag > env > default. | ||
func getPollTime(flagValue time.Duration, envValue string, defaultValue time.Duration) (time.Duration, error) { | ||
if flagValue != defaultValue { |
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.
this can be simplified using cmd.Flags().Changed()
which would return true if the flag has been changed
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.
this can be simplified using
cmd.Flags().Changed()
which would return true if the flag has been changed
yes
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.
Code changes LGTM. @Galoretka are you able to try testing this manually to verify it works as expected?
Yes, tested, 'go run test_poll_time_priority.go' Test 1: Default value (no flag, no env) Test 2: Environment variable only Test 3: Flag only Test 4: Flag and env (flag should take priority) Test 5: Invalid environment variable Test 6: Flag with same value as default (should still use flag) Manual testing completed! |
should I show the code of tests? |
Comparison between old and new approachesScenario: User sets --poll-time=3s (same as default) Old approach (comparing with default): New approach (using flag.Changed()): Key difference:
This is why the new approach is better:
Also comparison |
Previously, the logic for setting the pollTime parameter did not properly prioritize the CLI flag over the TXSIM_POLL environment variable. If both were set, the environment variable could override the flag, which is not the expected behavior for CLI tools.
This commit updates the logic so that the --poll-time flag always takes precedence. The value from the TXSIM_POLL environment variable is only used if the flag is not explicitly set (i.e., when pollTime is still at its default value). This ensures consistent and predictable configuration behavior for users.