refactor: consolidate environment variable reading using envutil helpers - #1237
Merged
Conversation
…ig_env.go - Replace GetGatewayDomainFromEnv with envutil.GetEnvString - Replace GetGatewayAPIKeyFromEnv with envutil.GetEnvString - Update GetGatewayPortFromEnv to use envutil.GetEnvString for raw read - Add envutil import to config_env.go - Remove redundant os import - All existing tests pass without modification Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
lpcox
marked this pull request as ready for review
February 21, 2026 18:34
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors environment variable reading in internal/config/config_env.go to use the standard envutil helper functions instead of direct os.Getenv() calls. This consolidates duplicate environment variable reading logic and ensures consistency with the rest of the codebase.
Changes:
- Replaced direct
os.Getenv()calls withenvutil.GetEnvString()in three functions - Removed unused
osimport and addedenvutilimport - Preserved all existing behavior including port validation logic
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
internal/config/config_env.goduplicated environment variable reading logic that already exists ininternal/envutil. Three functions used directos.Getenv()calls instead of the standardenvutil.GetEnvString()utility.Changes
GetGatewayDomainFromEnv()andGetGatewayAPIKeyFromEnv()with single-lineenvutil.GetEnvString()callsGetGatewayPortFromEnv()to useenvutil.GetEnvString()for raw environment variable read while preserving port validation logicosimport, addedenvutilimportBefore/After
Behavior unchanged - all existing tests pass without modification.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
example.com/tmp/go-build3717154652/b275/launcher.test /tmp/go-build3717154652/b275/launcher.test -test.testlogfile=/tmp/go-build3717154652/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -unreachable=false din}} x_amd64/vet --short(dns block)invalid-host-that-does-not-exist-12345.com/tmp/go-build289446620/b001/config.test /tmp/go-build289446620/b001/config.test -test.testlogfile=/tmp/go-build289446620/b001/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true go k1BRF76vb .12/x64/as(dns block)nonexistent.local/tmp/go-build3717154652/b275/launcher.test /tmp/go-build3717154652/b275/launcher.test -test.testlogfile=/tmp/go-build3717154652/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -unreachable=false din}} x_amd64/vet --short(dns block)slow.example.com/tmp/go-build3717154652/b275/launcher.test /tmp/go-build3717154652/b275/launcher.test -test.testlogfile=/tmp/go-build3717154652/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -unreachable=false din}} x_amd64/vet --short(dns block)this-host-does-not-exist-12345.com/tmp/go-build3717154652/b284/mcp.test /tmp/go-build3717154652/b284/mcp.test -test.testlogfile=/tmp/go-build3717154652/b284/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -unreachable=false /tmp/go-build1892238626/b145/vet.cfg x_amd64/vet --short(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
This section details on the original issue you should resolve
<issue_title>[duplicate-code] Duplicate Code Pattern:
config/config_env.goReimplementsenvutilFunctionality</issue_title><issue_description>Part of duplicate code analysis: #1218
Summary
internal/config/config_env.goreads environment variables usingos.Getenvdirectly with manualstrconv.Atoiparsing, while the rest of the codebase uses theinternal/envutilpackage for the same operations.GetGatewayDomainFromEnvandGetGatewayAPIKeyFromEnvare trivialos.Getenvwrappers that duplicate whatenvutil.GetEnvStringalready provides.GetGatewayPortFromEnvreimplements integer parsing logic thatenvutil.GetEnvIntalready handles, but adds port-range validation on top — mixing concerns.Duplication Details
Pattern: Manual env-var reading duplicating
envutilhelpersSeverity: Medium
Occurrences: 3 functions in
config_env.govs equivalent patterns inenvutilLocations:
internal/config/config_env.go(lines 1–38): all three getter functionsinternal/envutil/envutil.go(lines 9–43):GetEnvString,GetEnvInt,GetEnvBoolCode Sample:
GetGatewayDomainFromEnvandGetGatewayAPIKeyFromEnvare pure one-lineos.Getenvwrappers that could simply beenvutil.GetEnvString("MCP_GATEWAY_DOMAIN", "").GetGatewayPortFromEnvadds error-return semantics and port validation thatenvutil.GetEnvIntdoesn't provide, making it partially legitimate — but the int-parsing logic is duplicated.Impact Analysis
envutil.GetEnvIntsilently returns the default if parsing fails;GetGatewayPortFromEnvreturns an error. These behave differently, which can cause subtle inconsistencies if future callers mix approachesconfig_env.gothat could be replaced withenvutilcalls or a thin validation wrapperRefactoring Recommendations
Replace
GetGatewayDomainFromEnvandGetGatewayAPIKeyFromEnvwithenvutil.GetEnvStringRefactor
GetGatewayPortFromEnvto separate parsing from validationenvutil.GetEnvStringto read the raw value, then applystrconv.Atoi+rules.PortRangeImplementation Checklist
GetGatewayDomainFromEnvbody withenvutil.GetEnvString("MCP_GATEWAY_DOMAIN", "")GetGatewayAPIKeyFromEnvbody withenvutil.GetEnvString("MCP_GATEWAY_API_KEY", "")GetGatewayPortFromEnvto useenvutil.GetEnvStringfor the raw readenvutilimport toconfig_env.gomake test)Parent Issue
See parent analysis report: #1218
Related to #1218
config/config_env.goReimplementsenvutilFunctionality #1220