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
8 changes: 0 additions & 8 deletions internal/approve/ioctl_windows.go

This file was deleted.

27 changes: 26 additions & 1 deletion internal/approve/prompt_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package approve

import (
"fmt"
"os"
"strings"

"github.com/php-workx/fuse/internal/sanitize"
)

var errNonInteractive = fmt.Errorf("fuse:NON_INTERACTIVE_MODE STOP. Approval requires an interactive terminal (/dev/tty unavailable)")
var errNonInteractive = fmt.Errorf("fuse:NON_INTERACTIVE_MODE STOP. Approval requires an interactive terminal (console unavailable)")

var errPromptTimeout = fmt.Errorf("fuse:TIMEOUT_WAITING_FOR_USER STOP. The user did not approve this action in time")

Expand All @@ -19,3 +20,27 @@ func sanitizePrompt(s string) string {
s = strings.ReplaceAll(s, "\r", " ")
return s
}

// getContextVars returns relevant environment variables for the prompt.
// Used by both Unix and Windows prompt implementations.
func getContextVars() string {
relevantVars := []string{
"AWS_PROFILE", "AWS_REGION", "AWS_DEFAULT_REGION",
"TF_WORKSPACE", "TF_VAR_environment",
"KUBECONFIG", "KUBECONTEXT",
"GCP_PROJECT", "GOOGLE_CLOUD_PROJECT",
"AZURE_SUBSCRIPTION",
}

var result string
for _, v := range relevantVars {
val := os.Getenv(v)
if val != "" {
if result != "" {
result += ", "
}
result += v + "=" + val
}
}
return result
}
52 changes: 51 additions & 1 deletion internal/approve/prompt_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package approve

import "testing"
import (
"strings"
"testing"
)

// Comprehensive sanitization tests are in internal/sanitize/sanitize_test.go.
// This test verifies the delegation wrapper works.
Expand Down Expand Up @@ -29,3 +32,50 @@ func TestSanitizePrompt_StripsNewlines(t *testing.T) {
t.Errorf("newlines not replaced: got %q", got)
}
}

// clearTrackedVars blanks all env vars that getContextVars monitors,
// ensuring test isolation regardless of the host environment.
func clearTrackedVars(t *testing.T) {
t.Helper()
for _, v := range []string{
"AWS_PROFILE", "AWS_REGION", "AWS_DEFAULT_REGION",
"TF_WORKSPACE", "TF_VAR_environment",
"KUBECONFIG", "KUBECONTEXT",
"GCP_PROJECT", "GOOGLE_CLOUD_PROJECT",
"AZURE_SUBSCRIPTION",
} {
t.Setenv(v, "")
}
}

func TestGetContextVars_Empty(t *testing.T) {
clearTrackedVars(t)

got := getContextVars()
if got != "" {
t.Errorf("expected empty string, got %q", got)
}
}

func TestGetContextVars_SingleVar(t *testing.T) {
clearTrackedVars(t)
t.Setenv("AWS_PROFILE", "prod")

got := getContextVars()
if got != "AWS_PROFILE=prod" {
t.Errorf("expected AWS_PROFILE=prod, got %q", got)
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

func TestGetContextVars_MultipleVars(t *testing.T) {
t.Setenv("AWS_PROFILE", "staging")
t.Setenv("KUBECONFIG", "/home/user/.kube/config")
got := getContextVars()
// Both should appear, comma-separated.
if !strings.Contains(got, "AWS_PROFILE=staging") {
t.Errorf("missing AWS_PROFILE in %q", got)
}
if !strings.Contains(got, "KUBECONFIG=/home/user/.kube/config") {
t.Errorf("missing KUBECONFIG in %q", got)
}
}
25 changes: 1 addition & 24 deletions internal/approve/prompt_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func readApprovalDecision(ctx context.Context, tty *os.File, deadline time.Time,
select {
case <-ctx.Done():
fmt.Fprintf(tty, "\n Denied (shutdown).\n\n")
return false, "", nil
return false, "", fmt.Errorf("approval interrupted: %w", ctx.Err())
Comment thread
php-workx marked this conversation as resolved.
case <-sigCh:
fmt.Fprintf(tty, "\n Denied (signal received).\n\n")
return false, "", nil
Expand Down Expand Up @@ -249,26 +249,3 @@ func renderPrompt(tty *os.File, command, reason string) {
fmt.Fprintf(tty, " \033[1;32m[A]pprove\033[0m | \033[1;31m[D]eny\033[0m\n")
fmt.Fprintf(tty, " > ")
}

// getContextVars returns relevant environment variables for the prompt.
func getContextVars() string {
relevantVars := []string{
"AWS_PROFILE", "AWS_REGION", "AWS_DEFAULT_REGION",
"TF_WORKSPACE", "TF_VAR_environment",
"KUBECONFIG", "KUBECONTEXT",
"GCP_PROJECT", "GOOGLE_CLOUD_PROJECT",
"AZURE_SUBSCRIPTION",
}

var result string
for _, v := range relevantVars {
val := os.Getenv(v)
if val != "" {
if result != "" {
result += ", "
}
result += v + "=" + val
}
}
return result
}
Loading
Loading