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
65 changes: 21 additions & 44 deletions cmd/context.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package cmd

import (
"context"
"fmt"

"github.com/spf13/cobra"
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/pipelines"
"github.com/windsorcli/cli/pkg/runtime"
)

// getContextCmd represents the get command
Expand All @@ -16,29 +15,21 @@ var getContextCmd = &cobra.Command{
Long: "Retrieve and display the current context from the configuration",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
// Get shared dependency injector from context
injector := cmd.Context().Value(injectorKey).(di.Injector)
deps := &runtime.Dependencies{
Injector: cmd.Context().Value(injectorKey).(di.Injector),
}

// Create output function
outputFunc := func(output string) {
fmt.Fprintln(cmd.OutOrStdout(), output)
}

// Create execution context with operation and output function
ctx := context.WithValue(cmd.Context(), "operation", "get")
ctx = context.WithValue(ctx, "output", outputFunc)

// Set up the context pipeline
pipeline, err := pipelines.WithPipeline(injector, ctx, "contextPipeline")
if err != nil {
return fmt.Errorf("failed to set up context pipeline: %w", err)
}

// Execute the pipeline
if err := pipeline.Execute(ctx); err != nil {
return fmt.Errorf("Error executing context pipeline: %w", err)
if err := runtime.NewRuntime(deps).
LoadShell().
LoadConfigHandler().
PrintContext(outputFunc).
Do(); err != nil {
return fmt.Errorf("Error getting context: %w", err)
}

return nil
},
}
Expand All @@ -48,33 +39,20 @@ var setContextCmd = &cobra.Command{
Use: "set [context]",
Short: "Set the current context",
Long: "Set the current context in the configuration and save it",
Args: cobra.ExactArgs(1), // Ensure exactly one argument is provided
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
// Get shared dependency injector from context
injector := cmd.Context().Value(injectorKey).(di.Injector)

// Create output function
outputFunc := func(output string) {
fmt.Fprintln(cmd.OutOrStdout(), output)
}

// Create execution context with operation, context name, and output function
ctx := context.WithValue(cmd.Context(), "operation", "set")
ctx = context.WithValue(ctx, "contextName", args[0])
ctx = context.WithValue(ctx, "output", outputFunc)

// Set up the context pipeline
pipeline, err := pipelines.WithPipeline(injector, ctx, "contextPipeline")
if err != nil {
return fmt.Errorf("failed to set up context pipeline: %w", err)
deps := &runtime.Dependencies{
Injector: cmd.Context().Value(injectorKey).(di.Injector),
}

// Execute the pipeline
if err := pipeline.Execute(ctx); err != nil {
return fmt.Errorf("Error executing context pipeline: %w", err)
if err := runtime.NewRuntime(deps).
LoadShell().
LoadConfigHandler().
WriteResetToken().
SetContext(args[0]).
Do(); err != nil {
return fmt.Errorf("Error setting context: %w", err)
}

return nil
},
}
Expand All @@ -97,7 +75,7 @@ var setContextAliasCmd = &cobra.Command{
Short: "Alias for 'context set'",
SilenceUsage: true,
Long: "Alias for 'context set'",
Args: cobra.ExactArgs(1), // Ensure exactly one argument is provided
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
rootCmd.SetArgs(append([]string{"context", "set"}, args...))
return rootCmd.Execute()
Expand All @@ -115,7 +93,6 @@ func init() {
contextCmd.AddCommand(getContextCmd)
contextCmd.AddCommand(setContextCmd)

// Add alias commands to rootCmd
rootCmd.AddCommand(getContextAliasCmd)
rootCmd.AddCommand(setContextAliasCmd)
}
80 changes: 42 additions & 38 deletions cmd/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ func TestContextCmd(t *testing.T) {
setup := func(t *testing.T) (*bytes.Buffer, *bytes.Buffer) {
t.Helper()

// Clear environment variables that could affect tests
origContext := os.Getenv("WINDSOR_CONTEXT")
os.Unsetenv("WINDSOR_CONTEXT")
t.Cleanup(func() {
if origContext != "" {
os.Setenv("WINDSOR_CONTEXT", origContext)
}
})

// Change to a temporary directory without a config file
origDir, err := os.Getwd()
if err != nil {
Expand All @@ -34,30 +43,33 @@ func TestContextCmd(t *testing.T) {
return stdout, stderr
}

t.Run("GetContextNoConfig", func(t *testing.T) {
t.Run("GetContext", func(t *testing.T) {
// Given proper output capture in a directory without config
_, _ = setup(t)
stdout, _ := setup(t)
// Don't set up mocks - we want to test real behavior

rootCmd.SetArgs([]string{"context", "get"})

// When executing the command
err := Execute()

// Then an error should occur
if err == nil {
t.Error("Expected error, got nil")
// Then no error should occur
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

// And error should contain init message
expectedError := "Error executing context pipeline: No context is available. Have you run `windsor init`?"
if err.Error() != expectedError {
t.Errorf("Expected error %q, got %q", expectedError, err.Error())
// And should output default context (real behavior)
output := stdout.String()
expectedOutput := "local\n"
if output != expectedOutput {
t.Errorf("Expected output %q, got %q", expectedOutput, output)
}
})

t.Run("SetContextNoArgs", func(t *testing.T) {
// Given proper output capture in a directory without config
_, _ = setup(t)
setupMocks(t)

rootCmd.SetArgs([]string{"context", "set"})

Expand All @@ -76,51 +88,48 @@ func TestContextCmd(t *testing.T) {
}
})

t.Run("SetContextNoConfig", func(t *testing.T) {
t.Run("SetContext", func(t *testing.T) {
// Given proper output capture in a directory without config
_, _ = setup(t)
setupMocks(t)

rootCmd.SetArgs([]string{"context", "set", "new-context"})

// When executing the command
err := Execute()

// Then an error should occur
if err == nil {
t.Error("Expected error, got nil")
}

// And error should contain init message
expectedError := "Error executing context pipeline: No context is available. Have you run `windsor init`?"
if err.Error() != expectedError {
t.Errorf("Expected error %q, got %q", expectedError, err.Error())
// Then no error should occur
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})

t.Run("GetContextAliasNoConfig", func(t *testing.T) {
t.Run("GetContextAlias", func(t *testing.T) {
// Given proper output capture in a directory without config
_, _ = setup(t)
stdout, _ := setup(t)
// Don't set up mocks - we want to test real behavior

rootCmd.SetArgs([]string{"get-context"})

// When executing the command
err := Execute()

// Then an error should occur
if err == nil {
t.Error("Expected error, got nil")
// Then no error should occur
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

// And error should contain init message
expectedError := "Error executing context pipeline: No context is available. Have you run `windsor init`?"
if err.Error() != expectedError {
t.Errorf("Expected error %q, got %q", expectedError, err.Error())
// And should output the current context (may be "local" or previously set context)
output := stdout.String()
if output == "" {
t.Error("Expected some output, got empty string")
}
})

t.Run("SetContextAliasNoArgs", func(t *testing.T) {
// Given proper output capture in a directory without config
_, _ = setup(t)
setupMocks(t)

rootCmd.SetArgs([]string{"set-context"})

Expand All @@ -139,24 +148,19 @@ func TestContextCmd(t *testing.T) {
}
})

t.Run("SetContextAliasNoConfig", func(t *testing.T) {
t.Run("SetContextAlias", func(t *testing.T) {
// Given proper output capture in a directory without config
_, _ = setup(t)
setupMocks(t)

rootCmd.SetArgs([]string{"set-context", "new-context"})

// When executing the command
err := Execute()

// Then an error should occur
if err == nil {
t.Error("Expected error, got nil")
}

// And error should contain init message
expectedError := "Error executing context pipeline: No context is available. Have you run `windsor init`?"
if err.Error() != expectedError {
t.Errorf("Expected error %q, got %q", expectedError, err.Error())
// Then no error should occur
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})
}
126 changes: 0 additions & 126 deletions pkg/pipelines/context.go

This file was deleted.

Loading
Loading