Skip to content

Conversation

Galoretka
Copy link
Contributor

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.

rootulp
rootulp previously approved these changes Jul 18, 2025
@rootulp rootulp changed the title fix: pollTime flag and environment variable priority logic fix(txsim): pollTime CLI flag should override environment variable Jul 18, 2025
evan-forbes
evan-forbes previously approved these changes Jul 21, 2025
@evan-forbes
Copy link
Member

evan-forbes commented Jul 21, 2025

utack

@Galoretka out of curiousity what you are using txsim for or how did you come across this?

// 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 {
Copy link
Member

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

Copy link
Contributor Author

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

@Galoretka Galoretka dismissed stale reviews from evan-forbes and rootulp via 523561a July 26, 2025 12:28
Copy link
Collaborator

@rootulp rootulp left a 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?

@Galoretka
Copy link
Contributor Author

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'
Testing poll time priority logic...
Priority: flag > env > default

Test 1: Default value (no flag, no env)
Result: 3s

Test 2: Environment variable only
Result: 5s

Test 3: Flag only
Result: 10s

Test 4: Flag and env (flag should take priority)
Result: 15s

Test 5: Invalid environment variable
Error: parsing poll time from env: time: invalid duration "invalid"

Test 6: Flag with same value as default (should still use flag)
Result: 3s

Manual testing completed!

@Galoretka
Copy link
Contributor Author

Code changes LGTM. @Galoretka are you able to try testing this manually to verify it works as expected?

should I show the code of tests?

@Galoretka
Copy link
Contributor Author

Comparison between old and new approaches

Scenario: User sets --poll-time=3s (same as default)
Flag value: 3s
Env value: 5s
Default value: 3s

Old approach (comparing with default):
Result: 5s

New approach (using flag.Changed()):
Result: 3s

Key difference:

  • Old approach: Cannot distinguish between 'user set flag to default value' and 'user didn't set flag'
  • New approach: Correctly identifies when user explicitly set the flag, regardless of value

This is why the new approach is better:

  1. It respects user intent - if they explicitly set a flag, it should be used
  2. It's more explicit and less error-prone

Also comparison

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants