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
43 changes: 28 additions & 15 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
},
}
Expand Down
17 changes: 15 additions & 2 deletions cmd/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,22 @@ 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
mockConfigHandler := config.NewMockConfigHandler()
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
Expand All @@ -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")
}
Expand Down Expand Up @@ -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)
}
})
}
4 changes: 3 additions & 1 deletion pkg/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading