From b3b1f6af3ba1b860f8199b08b940bb2f1050786d Mon Sep 17 00:00:00 2001 From: Ryan VanGundy <85766511+rmvangun@users.noreply.github.com> Date: Sun, 2 Nov 2025 19:32:16 -0500 Subject: [PATCH] refactor(cmd): Use the context for hook functionality Drops the use of the runtime mechanism in favor of the new context manager. Signed-off-by: Ryan VanGundy <85766511+rmvangun@users.noreply.github.com> --- cmd/hook.go | 22 +++++++++++++--------- cmd/hook_test.go | 25 +++++++++++++++++++++++-- pkg/context/context.go | 36 ++++++++++++++++++------------------ 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/cmd/hook.go b/cmd/hook.go index 8e2507100..9ba12f020 100644 --- a/cmd/hook.go +++ b/cmd/hook.go @@ -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{ @@ -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 }, } diff --git a/cmd/hook_test.go b/cmd/hook_test.go index 914a57679..8a02a3d51 100644 --- a/cmd/hook_test.go +++ b/cmd/hook_test.go @@ -2,6 +2,8 @@ package cmd import ( "bytes" + "context" + "fmt" "testing" ) @@ -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"}) @@ -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"}) @@ -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 diff --git a/pkg/context/context.go b/pkg/context/context.go index d64dc0ce7..313a59e0f 100644 --- a/pkg/context/context.go +++ b/pkg/context/context.go @@ -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 { @@ -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) }