Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(preview): merge duplicated positive int env parsing (#411) (#412)

fx-kurumi73b8218e7d26
diff --git a/internal/preview/config.go b/internal/preview/config.go
index 1de41ec3..1d28fa78 100644
--- a/internal/preview/config.go
+++ b/internal/preview/config.go
@@ -75,23 +75,22 @@ func LoadConfig() Config {
}
func intFromEnv(key string, fallback int) int {
- raw := strings.TrimSpace(os.Getenv(key))
- if raw == "" {
- return fallback
- }
- n, err := strconv.Atoi(raw)
- if err != nil || n <= 0 {
- return fallback
- }
- return n
+ return int(positiveIntFromEnv(key, int64(fallback), strconv.IntSize))
}
func int64FromEnv(key string, fallback int64) int64 {
+ return positiveIntFromEnv(key, fallback, 64)
+}
+
+// positiveIntFromEnv reads a positive base-10 integer of the given bit size
+// from the environment, returning fallback when unset, malformed, out of range
+// or not positive.
+func positiveIntFromEnv(key string, fallback int64, bitSize int) int64 {
raw := strings.TrimSpace(os.Getenv(key))
if raw == "" {
return fallback
}
- n, err := strconv.ParseInt(raw, 10, 64)
+ n, err := strconv.ParseInt(raw, 10, bitSize)
if err != nil || n <= 0 {
return fallback
}
diff --git a/internal/preview/config_env_int_test.go b/internal/preview/config_env_int_test.go
new file mode 100644
index 00000000..3c5ee58d
--- /dev/null
+++ b/internal/preview/config_env_int_test.go
@@ -0,0 +1,46 @@
+package preview
+
+import "testing"
+
+func TestPositiveIntFromEnvHelpers(t *testing.T) {
+ const key = "AGENTFLOW_PREVIEW_TEST_ENV_INT"
+ cases := []struct {
+ name string
+ raw string
+ wantInt int
+ want64 int64
+ }{
+ {name: "unset-empty", raw: "", wantInt: 7, want64: 8},
+ {name: "blank", raw: " ", wantInt: 7, want64: 8},
+ {name: "valid", raw: "42", wantInt: 42, want64: 42},
+ {name: "trimmed", raw: " 13 ", wantInt: 13, want64: 13},
+ {name: "zero", raw: "0", wantInt: 7, want64: 8},
+ {name: "negative", raw: "-5", wantInt: 7, want64: 8},
+ {name: "malformed", raw: "12abc", wantInt: 7, want64: 8},
+ {name: "float", raw: "1.5", wantInt: 7, want64: 8},
+ {name: "large-int64", raw: "9999999999", wantInt: 9999999999, want64: 9999999999},
+ {name: "overflow-int64", raw: "99999999999999999999", wantInt: 7, want64: 8},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Setenv(key, tc.raw)
+ if got := intFromEnv(key, 7); got != tc.wantInt {
+ t.Fatalf("intFromEnv(%q) = %d, want %d", tc.raw, got, tc.wantInt)
+ }
+ if got := int64FromEnv(key, 8); got != tc.want64 {
+ t.Fatalf("int64FromEnv(%q) = %d, want %d", tc.raw, got, tc.want64)
+ }
+ })
+ }
+}
+
+func TestPositiveIntFromEnvRespectsBitSize(t *testing.T) {
+ const key = "AGENTFLOW_PREVIEW_TEST_ENV_BITS"
+ t.Setenv(key, "70000")
+ if got := positiveIntFromEnv(key, 3, 16); got != 3 {
+ t.Fatalf("16-bit overflow should fall back, got %d", got)
+ }
+ if got := positiveIntFromEnv(key, 3, 32); got != 70000 {
+ t.Fatalf("32-bit value = %d, want 70000", got)
+ }
+}