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
20 changes: 14 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,26 @@ func preRunEInitializeCommonComponents(cmd *cobra.Command, args []string) error
return fmt.Errorf("Error creating common components: %w", err)
}

// Resolve the shell and config handler
shell := controller.ResolveShell()
if shell != nil {
shell.SetVerbosity(verbose)
}

// Check if we're in a trusted directory, but only for needed commands
cmdName := cmd.Name()
if cmdName != "hook" && cmdName != "init" && (cmdName != "env" || !cmd.Flags().Changed("decrypt")) {
if err := shell.CheckTrustedDirectory(); err != nil {
fmt.Fprintf(os.Stderr, "\033[33mWarning: You are not in a trusted directory. If you are in a Windsor project, run 'windsor init' to approve.\033[0m\n")
}
}

// Resolve the config handler
configHandler := controller.ResolveConfigHandler()
if configHandler == nil {
return fmt.Errorf("No config handler found")
}

// Set the verbosity
shell := controller.ResolveShell()
if shell != nil {
shell.SetVerbosity(verbose)
}

// Determine the cliConfig path
var cliConfigPath string
if cliConfigPath = os.Getenv("WINDSORCONFIG"); cliConfigPath == "" {
Expand Down
140 changes: 140 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,144 @@ func TestRoot_preRunEInitializeCommonComponents(t *testing.T) {
}
})

t.Run("WarningInUntrustedDirectory", func(t *testing.T) {
// Setup mocks
mocks := setupSafeRootMocks()
mocks.Controller.ResolveShellFunc = func() shell.Shell {
return mocks.Shell
}
mocks.Shell.CheckTrustedDirectoryFunc = func() error {
return fmt.Errorf("Current directory not in the trusted list")
}

// Create a command and register the controller
cmd := &cobra.Command{}
cmd.SetContext(context.WithValue(context.Background(), controllerKey, mocks.Controller))

// Capture stderr
output := captureStderr(func() {
// Run preRunEInitializeCommonComponents
err := preRunEInitializeCommonComponents(cmd, []string{})
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
})

// Check for warning message
expectedWarning := "Warning: You are not in a trusted directory"
if !strings.Contains(output, expectedWarning) {
t.Errorf("Expected output to contain %q, got %q", expectedWarning, output)
}
})

t.Run("NoWarningForHookCommand", func(t *testing.T) {
// Setup mocks
mocks := setupSafeRootMocks()
mocks.Shell.CheckTrustedDirectoryFunc = func() error {
return fmt.Errorf("Current directory not in the trusted list")
}

// Capture stderr
output := captureStderr(func() {
// Create a hook command
cmd := hookCmd
cmd.SetArgs([]string{"bash"})

// Run preRunEInitializeCommonComponents
err := preRunEInitializeCommonComponents(cmd, []string{})
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
})

// Check that no warning was printed
unexpectedWarning := "Warning: You are not in a trusted directory"
if strings.Contains(output, unexpectedWarning) {
t.Errorf("Expected output to not contain %q, got %q", unexpectedWarning, output)
}
})

t.Run("NoWarningForEnvCommand", func(t *testing.T) {
// Setup mocks
mocks := setupSafeRootMocks()
mocks.Shell.CheckTrustedDirectoryFunc = func() error {
return fmt.Errorf("Current directory not in the trusted list")
}

// Capture stderr
output := captureStderr(func() {
// Create an env command
cmd := envCmd
cmd.SetArgs([]string{})

// Run preRunEInitializeCommonComponents
err := preRunEInitializeCommonComponents(cmd, []string{})
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
})

// Check that no warning was printed
unexpectedWarning := "Warning: You are not in a trusted directory"
if strings.Contains(output, unexpectedWarning) {
t.Errorf("Expected output to not contain %q, got %q", unexpectedWarning, output)
}
})

t.Run("NoWarningForEnvCommandWithDecrypt", func(t *testing.T) {
// Setup mocks
mocks := setupSafeRootMocks()
mocks.Shell.CheckTrustedDirectoryFunc = func() error {
return fmt.Errorf("Current directory not in the trusted list")
}

// Capture stderr
output := captureStderr(func() {
// Create an env command with --decrypt flag
cmd := envCmd
cmd.SetArgs([]string{"--decrypt"})

// Run preRunEInitializeCommonComponents
err := preRunEInitializeCommonComponents(cmd, []string{})
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
})

// Check that no warning was printed
unexpectedWarning := "Warning: You are not in a trusted directory"
if strings.Contains(output, unexpectedWarning) {
t.Errorf("Expected output to not contain %q, got %q", unexpectedWarning, output)
}
})

t.Run("WarningForEnvCommandWithoutDecrypt", func(t *testing.T) {
// Setup mocks
mocks := setupSafeRootMocks()
mocks.Controller.ResolveShellFunc = func() shell.Shell {
return mocks.Shell
}
mocks.Shell.CheckTrustedDirectoryFunc = func() error {
return fmt.Errorf("Current directory not in the trusted list")
}

// Create a command and register the controller
cmd := &cobra.Command{Use: "env"}
cmd.SetContext(context.WithValue(context.Background(), controllerKey, mocks.Controller))

// Capture stderr
output := captureStderr(func() {
// Run preRunEInitializeCommonComponents
err := preRunEInitializeCommonComponents(cmd, []string{})
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
})

// Check for warning message
expectedWarning := "Warning: You are not in a trusted directory"
if !strings.Contains(output, expectedWarning) {
t.Errorf("Expected output to contain %q, got %q", expectedWarning, output)
}
})
}
Loading