Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions internal/cmd/flags_difc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package cmd
// DIFC (Decentralized Information Flow Control) related flags

import (
"os"
"strings"

"github.com/github/gh-aw-mcpg/internal/envutil"
"github.com/spf13/cobra"
)

Expand All @@ -28,11 +26,5 @@ func init() {
// getDefaultEnableDIFC returns the default DIFC setting, checking MCP_GATEWAY_ENABLE_DIFC
// environment variable first, then falling back to the hardcoded default (false)
func getDefaultEnableDIFC() bool {
if envDIFC := os.Getenv("MCP_GATEWAY_ENABLE_DIFC"); envDIFC != "" {
switch strings.ToLower(envDIFC) {
case "1", "true", "yes", "on":
return true
}
}
return defaultEnableDIFC
return envutil.GetEnvBool("MCP_GATEWAY_ENABLE_DIFC", defaultEnableDIFC)
}
24 changes: 4 additions & 20 deletions internal/cmd/flags_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package cmd
// Logging-related flags

import (
"fmt"
"os"

"github.com/github/gh-aw-mcpg/internal/envutil"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -34,31 +32,17 @@ func init() {
// getDefaultLogDir returns the default log directory, checking MCP_GATEWAY_LOG_DIR
// environment variable first, then falling back to the hardcoded default
func getDefaultLogDir() string {
if envLogDir := os.Getenv("MCP_GATEWAY_LOG_DIR"); envLogDir != "" {
return envLogDir
}
return defaultLogDir
return envutil.GetEnvString("MCP_GATEWAY_LOG_DIR", defaultLogDir)
}

// getDefaultPayloadDir returns the default payload directory, checking MCP_GATEWAY_PAYLOAD_DIR
// environment variable first, then falling back to the hardcoded default
func getDefaultPayloadDir() string {
if envPayloadDir := os.Getenv("MCP_GATEWAY_PAYLOAD_DIR"); envPayloadDir != "" {
return envPayloadDir
}
return defaultPayloadDir
return envutil.GetEnvString("MCP_GATEWAY_PAYLOAD_DIR", defaultPayloadDir)
}

// getDefaultPayloadSizeThreshold returns the default payload size threshold, checking
// MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD environment variable first, then falling back to the hardcoded default
func getDefaultPayloadSizeThreshold() int {
if envThreshold := os.Getenv("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD"); envThreshold != "" {
// Try to parse as integer
var threshold int
if _, err := fmt.Sscanf(envThreshold, "%d", &threshold); err == nil && threshold > 0 {
return threshold
}
// Invalid value, use default
}
return defaultPayloadSizeThreshold
return envutil.GetEnvInt("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD", defaultPayloadSizeThreshold)
}
46 changes: 46 additions & 0 deletions internal/envutil/envutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package envutil

import (
"os"
"strconv"
"strings"
)

// GetEnvString returns the value of the environment variable specified by envKey.
// If the environment variable is not set or is empty, it returns the defaultValue.
func GetEnvString(envKey, defaultValue string) string {
if value := os.Getenv(envKey); value != "" {
return value
}
return defaultValue
}

// GetEnvInt returns the integer value of the environment variable specified by envKey.
// If the environment variable is not set, is empty, cannot be parsed as an integer,
// or is not positive (> 0), it returns the defaultValue.
// This function validates that the value is a positive integer.
func GetEnvInt(envKey string, defaultValue int) int {
if envValue := os.Getenv(envKey); envValue != "" {
if value, err := strconv.Atoi(envValue); err == nil && value > 0 {
return value
Comment on lines +24 to +25
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

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

GetEnvInt previously used fmt.Sscanf("%d") in the flag getter, which accepts leading/trailing whitespace (and newlines). Switching to strconv.Atoi changes behavior so values like " 4096 " will now be treated as invalid and fall back to default. To avoid a subtle regression for env var parsing, trim surrounding whitespace before Atoi (and keep the >0 validation).

Suggested change
if value, err := strconv.Atoi(envValue); err == nil && value > 0 {
return value
envValue = strings.TrimSpace(envValue)
if envValue != "" {
if value, err := strconv.Atoi(envValue); err == nil && value > 0 {
return value
}

Copilot uses AI. Check for mistakes.
}
}
return defaultValue
}

// GetEnvBool returns the boolean value of the environment variable specified by envKey.
// If the environment variable is not set or is empty, it returns the defaultValue.
// Truthy values (case-insensitive): "1", "true", "yes", "on"
// Falsy values (case-insensitive): "0", "false", "no", "off"
// Any other value returns the defaultValue.
func GetEnvBool(envKey string, defaultValue bool) bool {
if envValue := os.Getenv(envKey); envValue != "" {
switch strings.ToLower(envValue) {
case "1", "true", "yes", "on":
return true
case "0", "false", "no", "off":
return false
}
}
return defaultValue
}
Loading
Loading