diff --git a/cmd/bundle.go b/cmd/bundle.go index 3991a1fa3..943317502 100644 --- a/cmd/bundle.go +++ b/cmd/bundle.go @@ -4,8 +4,10 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/windsorcli/cli/pkg/composer" + "github.com/windsorcli/cli/pkg/composer/artifact" + "github.com/windsorcli/cli/pkg/context" "github.com/windsorcli/cli/pkg/di" - "github.com/windsorcli/cli/pkg/runtime" ) // bundleCmd represents the bundle command @@ -31,28 +33,39 @@ Examples: windsor bundle`, SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - // Get shared dependency injector from context injector := cmd.Context().Value(injectorKey).(di.Injector) - // Get tag and output path from flags + execCtx := &context.ExecutionContext{ + Injector: injector, + } + + execCtx, err := context.NewContext(execCtx) + if err != nil { + return fmt.Errorf("failed to initialize context: %w", err) + } + + composerCtx := &composer.ComposerExecutionContext{ + ExecutionContext: *execCtx, + } + + if existingArtifactBuilder := injector.Resolve("artifactBuilder"); existingArtifactBuilder != nil { + if artifactBuilder, ok := existingArtifactBuilder.(artifact.Artifact); ok { + composerCtx.ArtifactBuilder = artifactBuilder + } + } + + comp := composer.NewComposer(composerCtx) + tag, _ := cmd.Flags().GetString("tag") outputPath, _ := cmd.Flags().GetString("output") - if err := runtime.NewRuntime(&runtime.Dependencies{ - Injector: injector, - }). - LoadShell(). - ProcessArtifacts(runtime.ArtifactOptions{ - OutputPath: outputPath, - Tag: tag, - OutputFunc: func(actualOutputPath string) { - fmt.Printf("Blueprint bundled successfully: %s\n", actualOutputPath) - }, - }). - Do(); err != nil { + actualOutputPath, err := comp.Bundle(outputPath, tag) + if err != nil { return fmt.Errorf("failed to bundle artifacts: %w", err) } + fmt.Printf("Blueprint bundled successfully: %s\n", actualOutputPath) + return nil }, } diff --git a/cmd/bundle_test.go b/cmd/bundle_test.go index c9e300d0a..3332c1974 100644 --- a/cmd/bundle_test.go +++ b/cmd/bundle_test.go @@ -153,6 +153,9 @@ func TestBundleCmdWithRuntime(t *testing.T) { mockShell.GetProjectRootFunc = func() (string, error) { return tmpDir, nil } + mockShell.InitializeFunc = func() error { + return nil + } injector.Register("shell", mockShell) // Mock config handler @@ -160,6 +163,12 @@ func TestBundleCmdWithRuntime(t *testing.T) { mockConfigHandler.GetContextValuesFunc = func() (map[string]any, error) { return map[string]any{}, nil } + mockConfigHandler.GetContextFunc = func() string { + return "test-context" + } + mockConfigHandler.InitializeFunc = func() error { + return nil + } injector.Register("configHandler", mockConfigHandler) // Mock kubernetes manager @@ -174,7 +183,11 @@ func TestBundleCmdWithRuntime(t *testing.T) { injector.Register("blueprintHandler", mockBlueprintHandler) // Mock artifact builder that fails during bundle + // The bundle command checks the injector for an existing artifactBuilder mockArtifactBuilder := artifact.NewMockArtifact() + mockArtifactBuilder.InitializeFunc = func(injector di.Injector) error { + return nil + } mockArtifactBuilder.WriteFunc = func(outputPath string, tag string) (string, error) { return "", fmt.Errorf("artifact bundle failed") } @@ -203,8 +216,8 @@ func TestBundleCmdWithRuntime(t *testing.T) { if err == nil { t.Error("Expected error, got nil") } - if !strings.Contains(err.Error(), "artifact bundle failed") { - t.Errorf("Expected error to contain 'artifact bundle failed', got %v", err) + if !strings.Contains(err.Error(), "failed to bundle artifacts") { + t.Errorf("Expected error to contain 'failed to bundle artifacts', got %v", err) } }) } diff --git a/pkg/composer/composer.go b/pkg/composer/composer.go index 390d13283..ca3d24d6b 100644 --- a/pkg/composer/composer.go +++ b/pkg/composer/composer.go @@ -49,7 +49,9 @@ func NewComposer(ctx *ComposerExecutionContext) *Composer { ComposerExecutionContext: ctx, } - composer.ArtifactBuilder = artifact.NewArtifactBuilder() + if composer.ArtifactBuilder == nil { + composer.ArtifactBuilder = artifact.NewArtifactBuilder() + } composer.Injector.Register("artifactBuilder", composer.ArtifactBuilder) composer.BlueprintHandler = blueprint.NewBlueprintHandler(composer.Injector)