Skip to content

Commit 8fe4967

Browse files
Copilotlpcox
andcommitted
Create envutil package and refactor duplicate environment variable getters
- Created internal/envutil package with GetEnvString, GetEnvInt, and GetEnvBool utilities - Added comprehensive test coverage for envutil package (46 test cases) - Refactored flags_logging.go to use envutil (reduced from ~35 lines to ~12 lines) - Refactored flags_difc.go to use envutil (reduced from ~9 lines to ~2 lines) - All existing tests pass Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
1 parent d44ff28 commit 8fe4967

4 files changed

Lines changed: 503 additions & 30 deletions

File tree

internal/cmd/flags_difc.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package cmd
33
// DIFC (Decentralized Information Flow Control) related flags
44

55
import (
6-
"os"
7-
"strings"
8-
6+
"github.com/github/gh-aw-mcpg/internal/envutil"
97
"github.com/spf13/cobra"
108
)
119

@@ -28,11 +26,5 @@ func init() {
2826
// getDefaultEnableDIFC returns the default DIFC setting, checking MCP_GATEWAY_ENABLE_DIFC
2927
// environment variable first, then falling back to the hardcoded default (false)
3028
func getDefaultEnableDIFC() bool {
31-
if envDIFC := os.Getenv("MCP_GATEWAY_ENABLE_DIFC"); envDIFC != "" {
32-
switch strings.ToLower(envDIFC) {
33-
case "1", "true", "yes", "on":
34-
return true
35-
}
36-
}
37-
return defaultEnableDIFC
29+
return envutil.GetEnvBool("MCP_GATEWAY_ENABLE_DIFC", defaultEnableDIFC)
3830
}

internal/cmd/flags_logging.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package cmd
33
// Logging-related flags
44

55
import (
6-
"fmt"
7-
"os"
8-
6+
"github.com/github/gh-aw-mcpg/internal/envutil"
97
"github.com/spf13/cobra"
108
)
119

@@ -34,31 +32,17 @@ func init() {
3432
// getDefaultLogDir returns the default log directory, checking MCP_GATEWAY_LOG_DIR
3533
// environment variable first, then falling back to the hardcoded default
3634
func getDefaultLogDir() string {
37-
if envLogDir := os.Getenv("MCP_GATEWAY_LOG_DIR"); envLogDir != "" {
38-
return envLogDir
39-
}
40-
return defaultLogDir
35+
return envutil.GetEnvString("MCP_GATEWAY_LOG_DIR", defaultLogDir)
4136
}
4237

4338
// getDefaultPayloadDir returns the default payload directory, checking MCP_GATEWAY_PAYLOAD_DIR
4439
// environment variable first, then falling back to the hardcoded default
4540
func getDefaultPayloadDir() string {
46-
if envPayloadDir := os.Getenv("MCP_GATEWAY_PAYLOAD_DIR"); envPayloadDir != "" {
47-
return envPayloadDir
48-
}
49-
return defaultPayloadDir
41+
return envutil.GetEnvString("MCP_GATEWAY_PAYLOAD_DIR", defaultPayloadDir)
5042
}
5143

5244
// getDefaultPayloadSizeThreshold returns the default payload size threshold, checking
5345
// MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD environment variable first, then falling back to the hardcoded default
5446
func getDefaultPayloadSizeThreshold() int {
55-
if envThreshold := os.Getenv("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD"); envThreshold != "" {
56-
// Try to parse as integer
57-
var threshold int
58-
if _, err := fmt.Sscanf(envThreshold, "%d", &threshold); err == nil && threshold > 0 {
59-
return threshold
60-
}
61-
// Invalid value, use default
62-
}
63-
return defaultPayloadSizeThreshold
47+
return envutil.GetEnvInt("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD", defaultPayloadSizeThreshold)
6448
}

internal/envutil/envutil.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package envutil
2+
3+
import (
4+
"os"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
// GetEnvString returns the value of the environment variable specified by envKey.
10+
// If the environment variable is not set or is empty, it returns the defaultValue.
11+
func GetEnvString(envKey, defaultValue string) string {
12+
if value := os.Getenv(envKey); value != "" {
13+
return value
14+
}
15+
return defaultValue
16+
}
17+
18+
// GetEnvInt returns the integer value of the environment variable specified by envKey.
19+
// If the environment variable is not set, is empty, cannot be parsed as an integer,
20+
// or is not positive (> 0), it returns the defaultValue.
21+
// This function validates that the value is a positive integer.
22+
func GetEnvInt(envKey string, defaultValue int) int {
23+
if envValue := os.Getenv(envKey); envValue != "" {
24+
if value, err := strconv.Atoi(envValue); err == nil && value > 0 {
25+
return value
26+
}
27+
}
28+
return defaultValue
29+
}
30+
31+
// GetEnvBool returns the boolean value of the environment variable specified by envKey.
32+
// If the environment variable is not set or is empty, it returns the defaultValue.
33+
// Truthy values (case-insensitive): "1", "true", "yes", "on"
34+
// Falsy values (case-insensitive): "0", "false", "no", "off"
35+
// Any other value returns the defaultValue.
36+
func GetEnvBool(envKey string, defaultValue bool) bool {
37+
if envValue := os.Getenv(envKey); envValue != "" {
38+
switch strings.ToLower(envValue) {
39+
case "1", "true", "yes", "on":
40+
return true
41+
case "0", "false", "no", "off":
42+
return false
43+
}
44+
}
45+
return defaultValue
46+
}

0 commit comments

Comments
 (0)