From aa2b628319fced0c3d1c5990e3c824c3e1a87806 Mon Sep 17 00:00:00 2001 From: Tanmay Jain Date: Mon, 8 Dec 2025 16:08:49 +0530 Subject: [PATCH] Adding `ui` object support in Bucket for Logs --- internal/hcp/registry/hcl.go | 2 +- internal/hcp/registry/json.go | 2 +- internal/hcp/registry/types.bucket.go | 14 +++++++------- internal/hcp/registry/types.bucket_test.go | 20 +++++++++++++++++--- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/internal/hcp/registry/hcl.go b/internal/hcp/registry/hcl.go index 5c6e9d15f..e569e4ddd 100644 --- a/internal/hcp/registry/hcl.go +++ b/internal/hcp/registry/hcl.go @@ -83,7 +83,7 @@ func (h *HCLRegistry) CompleteBuild( if err != nil { return nil, err } - return h.bucket.completeBuild(ctx, buildName, artifacts, buildErr) + return h.bucket.completeBuild(ctx, buildName, artifacts, h.ui, buildErr) } // VersionStatusSummary prints a status report in the UI if the version is not yet done diff --git a/internal/hcp/registry/json.go b/internal/hcp/registry/json.go index f3f142ee0..173308689 100644 --- a/internal/hcp/registry/json.go +++ b/internal/hcp/registry/json.go @@ -101,7 +101,7 @@ func (h *JSONRegistry) CompleteBuild( if err != nil { return nil, err } - return h.bucket.completeBuild(ctx, buildName, artifacts, buildErr) + return h.bucket.completeBuild(ctx, buildName, artifacts, h.ui, buildErr) } // VersionStatusSummary prints a status report in the UI if the version is not yet done diff --git a/internal/hcp/registry/types.bucket.go b/internal/hcp/registry/types.bucket.go index c81f03412..77775bff4 100644 --- a/internal/hcp/registry/types.bucket.go +++ b/internal/hcp/registry/types.bucket.go @@ -246,23 +246,21 @@ func (bucket *Bucket) uploadSbom(ctx context.Context, buildName string, sbom pac return bucket.client.UploadSbom(ctx, bucket.Name, bucket.Version.Fingerprint, buildToUpdate.ID, sbom) } -func (bucket *Bucket) updateChannels(ctx context.Context) error { +func (bucket *Bucket) updateChannels(ctx context.Context, ui packerSDK.Ui) error { if len(bucket.Channels) == 0 { return nil } - log.Printf("[INFO] Updating %d channel(s) to point to version %s", len(bucket.Channels), bucket.Version.ID) - body := &hcpPackerModels.HashicorpCloudPacker20230101UpdateChannelBody{ VersionFingerprint: bucket.Version.Fingerprint, UpdateMask: "versionFingerprint", } for _, channel := range bucket.Channels { - log.Printf("[INFO] Updating channel %s to version %s", channel, bucket.Version.ID) + ui.Say(fmt.Sprintf("Assigning version %s to channel `%s`", bucket.Version.ID, channel)) _, err := bucket.client.UpdateChannel(ctx, bucket.Name, channel, body) if err != nil { - log.Printf("[ERROR] Failed to update channel %s: %s", channel, err) + ui.Error(fmt.Sprintf("Failed to update channel %s: %s", channel, err)) return fmt.Errorf("failed to update channel %s: %w", channel, err) } } @@ -653,6 +651,7 @@ func (bucket *Bucket) completeBuild( ctx context.Context, buildName string, packerSDKArtifacts []packerSDK.Artifact, + ui packerSDK.Ui, buildErr error, ) ([]packerSDK.Artifact, error) { doneCh, ok := bucket.RunningBuilds[buildName] @@ -677,7 +676,7 @@ func (bucket *Bucket) completeBuild( return packerSDKArtifacts, fmt.Errorf("build failed, not uploading artifacts") } - artifacts, err := bucket.doCompleteBuild(ctx, buildName, packerSDKArtifacts, buildErr) + artifacts, err := bucket.doCompleteBuild(ctx, buildName, packerSDKArtifacts, ui, buildErr) if err != nil { err := bucket.UpdateBuildStatus(ctx, buildName, hcpPackerModels.HashicorpCloudPacker20230101BuildStatusBUILDFAILED) if err != nil { @@ -692,6 +691,7 @@ func (bucket *Bucket) doCompleteBuild( ctx context.Context, buildName string, packerSDKArtifacts []packerSDK.Artifact, + ui packerSDK.Ui, buildErr error, ) ([]packerSDK.Artifact, error) { for _, art := range packerSDKArtifacts { @@ -753,7 +753,7 @@ func (bucket *Bucket) doCompleteBuild( } // Update channels after build is marked complete - channelErr := bucket.updateChannels(ctx) + channelErr := bucket.updateChannels(ctx, ui) if channelErr != nil { log.Printf("[ERROR] Failed to update channels after completing build %s: %s", buildName, channelErr) } diff --git a/internal/hcp/registry/types.bucket_test.go b/internal/hcp/registry/types.bucket_test.go index fe5c1bd4f..9d7dcedb1 100644 --- a/internal/hcp/registry/types.bucket_test.go +++ b/internal/hcp/registry/types.bucket_test.go @@ -5,6 +5,8 @@ package registry import ( "context" + "io" + "os" "reflect" "strconv" "sync" @@ -522,7 +524,11 @@ func TestCompleteBuild(t *testing.T) { Status: models.HashicorpCloudPacker20230101BuildStatusBUILDRUNNING, }) - _, err := dummyBucket.completeBuild(context.Background(), "test-build", tt.artifactsToUse, nil) + _, err := dummyBucket.completeBuild(context.Background(), "test-build", tt.artifactsToUse, &packer.BasicUi{ + Reader: os.Stdin, + Writer: io.Discard, + ErrorWriter: io.Discard, + }, nil) if err != nil != tt.expectError { t.Errorf("expected %t error; got %t", tt.expectError, err != nil) t.Logf("error was: %s", err) @@ -584,7 +590,11 @@ func TestBucket_UpdateChannels(t *testing.T) { Fingerprint: "test-fingerprint", } - err := b.updateChannels(context.Background()) + err := b.updateChannels(context.Background(), &packer.BasicUi{ + Reader: os.Stdin, + Writer: io.Discard, + ErrorWriter: io.Discard, + }) if (err != nil) != tt.wantErr { t.Errorf("updateChannels() error = %v, wantErr %v", err, tt.wantErr) @@ -651,7 +661,11 @@ func TestBucket_DoCompleteBuild_WithChannels(t *testing.T) { } // Complete the build - _, err = b.doCompleteBuild(context.TODO(), "happycloud.image", mockArtifacts, nil) + _, err = b.doCompleteBuild(context.TODO(), "happycloud.image", mockArtifacts, &packer.BasicUi{ + Reader: os.Stdin, + Writer: io.Discard, + ErrorWriter: io.Discard, + }, nil) if err != nil { t.Errorf("doCompleteBuild() should have completed successfully for build happycloud.image, got err: %v", err) }