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
22 changes: 13 additions & 9 deletions cmd/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

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

var hookCmd = &cobra.Command{
Expand All @@ -15,17 +15,21 @@ var hookCmd = &cobra.Command{
SilenceUsage: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
deps := &runtime.Dependencies{
Injector: cmd.Context().Value(injectorKey).(di.Injector),
injector := cmd.Context().Value(injectorKey).(di.Injector)

execCtx := &context.ExecutionContext{
Injector: injector,
}

execCtx, err := context.NewContext(execCtx)
if err != nil {
return fmt.Errorf("failed to initialize context: %w", err)
}

// Create Runtime and execute hook installation
if err := runtime.NewRuntime(deps).
LoadShell().
InstallHook(args[0]).
Do(); err != nil {
return fmt.Errorf("Error installing hook: %w", err)
if err := execCtx.Shell.InstallHook(args[0]); err != nil {
return fmt.Errorf("error installing hook: %w", err)
}

return nil
},
}
Expand Down
25 changes: 23 additions & 2 deletions cmd/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"bytes"
"context"
"fmt"
"testing"
)

Expand All @@ -15,8 +17,13 @@ func TestHookCmd(t *testing.T) {
}

t.Run("Success", func(t *testing.T) {
// Given proper output capture
// Given proper output capture and mock setup
_, stderr := setup(t)
mocks := setupMocks(t)

// Set up command context with injector
ctx := context.WithValue(context.Background(), injectorKey, mocks.Injector)
rootCmd.SetContext(ctx)

rootCmd.SetArgs([]string{"hook", "zsh"})

Expand Down Expand Up @@ -56,8 +63,21 @@ func TestHookCmd(t *testing.T) {
})

t.Run("UnsupportedShell", func(t *testing.T) {
// Given proper output capture
// Given proper output capture and mock setup
setup(t)
mocks := setupMocks(t)

// Configure mock shell to return error for unsupported shell
mocks.Shell.InstallHookFunc = func(shellName string) error {
if shellName == "unsupported" {
return fmt.Errorf("Unsupported shell: %s", shellName)
}
return nil
}

// Set up command context with injector
ctx := context.WithValue(context.Background(), injectorKey, mocks.Injector)
rootCmd.SetContext(ctx)

rootCmd.SetArgs([]string{"hook", "unsupported"})

Expand All @@ -67,6 +87,7 @@ func TestHookCmd(t *testing.T) {
// Then an error should occur
if err == nil {
t.Error("Expected error, got nil")
return
}

// And error should contain unsupported shell message
Expand Down
36 changes: 18 additions & 18 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,6 @@ func NewContext(ctx *ExecutionContext) (*ExecutionContext, error) {
}
injector := ctx.Injector

if ctx.ConfigHandler == nil {
if existing := injector.Resolve("configHandler"); existing != nil {
if configHandler, ok := existing.(config.ConfigHandler); ok {
ctx.ConfigHandler = configHandler
} else {
ctx.ConfigHandler = config.NewConfigHandler(injector)
injector.Register("configHandler", ctx.ConfigHandler)
}
} else {
ctx.ConfigHandler = config.NewConfigHandler(injector)
injector.Register("configHandler", ctx.ConfigHandler)
}

if err := ctx.ConfigHandler.Initialize(); err != nil {
return nil, fmt.Errorf("failed to initialize config handler: %w", err)
}
}

if ctx.Shell == nil {
if existing := injector.Resolve("shell"); existing != nil {
if shellInstance, ok := existing.(shell.Shell); ok {
Expand All @@ -121,6 +103,24 @@ func NewContext(ctx *ExecutionContext) (*ExecutionContext, error) {
}
}

if ctx.ConfigHandler == nil {
if existing := injector.Resolve("configHandler"); existing != nil {
if configHandler, ok := existing.(config.ConfigHandler); ok {
ctx.ConfigHandler = configHandler
} else {
ctx.ConfigHandler = config.NewConfigHandler(injector)
injector.Register("configHandler", ctx.ConfigHandler)
}
} else {
ctx.ConfigHandler = config.NewConfigHandler(injector)
injector.Register("configHandler", ctx.ConfigHandler)
}

if err := ctx.ConfigHandler.Initialize(); err != nil {
return nil, fmt.Errorf("failed to initialize config handler: %w", err)
}
}

if ctx.envVars == nil {
ctx.envVars = make(map[string]string)
}
Expand Down
Loading