From 62cf61d3639c02621d5dae8f004f32a2c31badeb Mon Sep 17 00:00:00 2001 From: Chandra Pamuluri Date: Wed, 20 Mar 2024 09:55:52 -0500 Subject: [PATCH 1/8] first cut --- go.mod | 2 +- pkg/registry/client.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index ecfd2a03b..ef4c853bc 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/vmware-tanzu/tanzu-cli go 1.21 replace cloud.google.com/go => cloud.google.com/go v0.102.1 - +replace github.com/vmware-tanzu/tanzu-plugin-runtime => ../../runtime-code/tanzu-plugin-runtime replace github.com/vmware-tanzu/tanzu-cli/test/e2e/framework => ./test/e2e/framework require ( diff --git a/pkg/registry/client.go b/pkg/registry/client.go index 570b775aa..a6649fc38 100644 --- a/pkg/registry/client.go +++ b/pkg/registry/client.go @@ -7,6 +7,7 @@ import ( "archive/tar" "bytes" "io" + "os" "github.com/cppforlife/go-cli-ui/ui" regname "github.com/google/go-containerregistry/pkg/name" @@ -221,6 +222,18 @@ func (r *registry) CopyImageFromTar(sourceTarFile, destImageRepo string) error { Insecure: r.opts.Insecure, } } + originalStdout := os.Stderr + pr, pw, pipeErr := os.Pipe() + if pipeErr != nil { + return pipeErr + } + defer func() { + pr.Close() + pw.Close() + os.Stderr = originalStdout + }() + os.Stderr = pw + err := copyOptions.Run() if err != nil { return err From a93374eee613575c1a32abd124d6687db70e6c97 Mon Sep 17 00:00:00 2001 From: Chandra Pamuluri Date: Mon, 22 Apr 2024 14:29:33 -0500 Subject: [PATCH 2/8] go --- go.mod | 1 - 1 file changed, 1 deletion(-) diff --git a/go.mod b/go.mod index ef4c853bc..5cfd8fa65 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/vmware-tanzu/tanzu-cli go 1.21 replace cloud.google.com/go => cloud.google.com/go v0.102.1 -replace github.com/vmware-tanzu/tanzu-plugin-runtime => ../../runtime-code/tanzu-plugin-runtime replace github.com/vmware-tanzu/tanzu-cli/test/e2e/framework => ./test/e2e/framework require ( From f7f61fd7ccfe31648df016b65e7ff8f774d00720 Mon Sep 17 00:00:00 2001 From: Chandra Pamuluri Date: Tue, 23 Apr 2024 15:49:10 -0500 Subject: [PATCH 3/8] spinner --- pkg/airgapped/plugin_bundle_upload.go | 31 ++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/pkg/airgapped/plugin_bundle_upload.go b/pkg/airgapped/plugin_bundle_upload.go index 5d344ca4d..9d82a4918 100644 --- a/pkg/airgapped/plugin_bundle_upload.go +++ b/pkg/airgapped/plugin_bundle_upload.go @@ -4,6 +4,7 @@ package airgapped import ( + "fmt" "os" "path/filepath" @@ -16,6 +17,7 @@ import ( "github.com/vmware-tanzu/tanzu-cli/pkg/carvelhelpers" "github.com/vmware-tanzu/tanzu-cli/pkg/plugininventory" "github.com/vmware-tanzu/tanzu-cli/pkg/utils" + "github.com/vmware-tanzu/tanzu-plugin-runtime/component" "github.com/vmware-tanzu/tanzu-plugin-runtime/log" ) @@ -55,6 +57,11 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error { return errors.Wrap(err, "error while parsing plugin migration manifest") } + totalImages := len(manifest.ImagesToCopy) + imagesUploaded := 0 + uploadingMsg := "[%d/%d] uploading image %d" + uploadedMsg := "[%d/%d] uploaded image %d" + errorMsg := "error while uploading image %q" // Iterate through all the images and publish them to the remote repository for _, ic := range manifest.ImagesToCopy { imageTar := filepath.Join(pluginBundleDir, ic.SourceTarFilePath) @@ -62,11 +69,29 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error { if err != nil { return errors.Wrap(err, "error while constructing the repo image path") } - log.Infof("---------------------------") - log.Infof("uploading image %q", repoImagePath) + errorMsg = fmt.Sprintf(errorMsg, repoImagePath) + uploadingMsg = fmt.Sprintf(uploadingMsg, totalImages, imagesUploaded, imagesUploaded) + uploadedMsg = fmt.Sprintf(uploadedMsg, totalImages, imagesUploaded, imagesUploaded) + var spinner component.OutputWriterSpinner + if !component.IsTTYEnabled() { + // Initialize the spinner + spinner = component.NewOutputWriterSpinner(component.WithOutputStream(os.Stderr), + component.WithSpinnerText(uploadingMsg), + component.WithSpinnerStarted()) + spinner.SetFinalText(errorMsg, log.LogTypeERROR) + defer spinner.StopSpinner() + } else { + log.Infof(uploadingMsg, totalImages, imagesUploaded, imagesUploaded) + } err = o.ImageProcessor.CopyImageFromTar(imageTar, repoImagePath) if err != nil { - return errors.Wrap(err, "error while uploading image") + return errors.Wrap(err, errorMsg) + } + imagesUploaded = imagesUploaded+1 + if spinner != nil { + spinner.SetFinalText(uploadedMsg, log.LogTypeINFO) + } else { + log.Infof(uploadedMsg, totalImages, imagesUploaded, repoImagePath) } } log.Infof("---------------------------") From 7c94a0665437440b4d482de0c5d72050d5d4cafd Mon Sep 17 00:00:00 2001 From: Chandra Pamuluri Date: Tue, 23 Apr 2024 16:57:47 -0500 Subject: [PATCH 4/8] spinner2 --- pkg/airgapped/plugin_bundle_upload.go | 66 ++++++++++++++++----------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/pkg/airgapped/plugin_bundle_upload.go b/pkg/airgapped/plugin_bundle_upload.go index 9d82a4918..3cb549ea3 100644 --- a/pkg/airgapped/plugin_bundle_upload.go +++ b/pkg/airgapped/plugin_bundle_upload.go @@ -59,40 +59,18 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error { totalImages := len(manifest.ImagesToCopy) imagesUploaded := 0 - uploadingMsg := "[%d/%d] uploading image %d" - uploadedMsg := "[%d/%d] uploaded image %d" - errorMsg := "error while uploading image %q" // Iterate through all the images and publish them to the remote repository + var repoImagePath string for _, ic := range manifest.ImagesToCopy { imageTar := filepath.Join(pluginBundleDir, ic.SourceTarFilePath) - repoImagePath, err := utils.JoinURL(o.DestinationRepo, ic.RelativeImagePath) + repoImagePath, err = utils.JoinURL(o.DestinationRepo, ic.RelativeImagePath) if err != nil { return errors.Wrap(err, "error while constructing the repo image path") } - errorMsg = fmt.Sprintf(errorMsg, repoImagePath) - uploadingMsg = fmt.Sprintf(uploadingMsg, totalImages, imagesUploaded, imagesUploaded) - uploadedMsg = fmt.Sprintf(uploadedMsg, totalImages, imagesUploaded, imagesUploaded) - var spinner component.OutputWriterSpinner - if !component.IsTTYEnabled() { - // Initialize the spinner - spinner = component.NewOutputWriterSpinner(component.WithOutputStream(os.Stderr), - component.WithSpinnerText(uploadingMsg), - component.WithSpinnerStarted()) - spinner.SetFinalText(errorMsg, log.LogTypeERROR) - defer spinner.StopSpinner() - } else { - log.Infof(uploadingMsg, totalImages, imagesUploaded, imagesUploaded) - } - err = o.ImageProcessor.CopyImageFromTar(imageTar, repoImagePath) - if err != nil { - return errors.Wrap(err, errorMsg) - } - imagesUploaded = imagesUploaded+1 - if spinner != nil { - spinner.SetFinalText(uploadedMsg, log.LogTypeINFO) - } else { - log.Infof(uploadedMsg, totalImages, imagesUploaded, repoImagePath) + if err = o.uploadImage(imageTar, repoImagePath, totalImages, imagesUploaded); err != nil { + return err } + imagesUploaded++ } log.Infof("---------------------------") log.Infof("---------------------------") @@ -126,6 +104,40 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error { return nil } +func (o *UploadPluginBundleOptions) uploadImage(imageTar, repoImagePath string, totalImages, imagesUploaded int) error { + uploadingMsg := fmt.Sprintf("[%d/%d] uploading image %q", totalImages, imagesUploaded, repoImagePath) + errorMsg := fmt.Sprintf("[%d/%d] error while uploading image %q", totalImages, imagesUploaded, repoImagePath) + uploadedMsg := "[%d/%d] uploaded image %q" + + var spinner component.OutputWriterSpinner + if component.IsTTYEnabled() { + // Initialize the spinner + spinner = component.NewOutputWriterSpinner( + component.WithOutputStream(os.Stderr), + component.WithSpinnerText(uploadingMsg), + component.WithSpinnerStarted(), + ) + spinner.SetFinalText(errorMsg, log.LogTypeERROR) + defer spinner.StopSpinner() + } else { + log.Infof(uploadingMsg, totalImages, imagesUploaded, repoImagePath) + } + + if err := o.ImageProcessor.CopyImageFromTar(imageTar, repoImagePath); err != nil { + return errors.Wrapf(err, errorMsg, repoImagePath) + } + + uploadedMsg = fmt.Sprintf(uploadedMsg, totalImages, imagesUploaded+1, repoImagePath) + if spinner != nil { + spinner.SetFinalText(uploadedMsg, log.LogTypeINFO) + spinner.StopSpinner() + } else { + log.Infof(uploadedMsg, totalImages, imagesUploaded, repoImagePath) + } + + return nil +} + // mergePluginInventoryMetadata merges the downloaded plugin inventory metadata with // existing plugin inventory metadata available on the remote repository func (o *UploadPluginBundleOptions) mergePluginInventoryMetadata(pluginInventoryMetadataImageWithTag, bundledPluginInventoryMetadataDBFilePath, tempDir string) error { From 02866381a3891791b1059bbf75940dd7457ff816 Mon Sep 17 00:00:00 2001 From: Chandra Pamuluri Date: Wed, 24 Apr 2024 09:59:07 -0500 Subject: [PATCH 5/8] spinner --- go.mod | 1 + pkg/airgapped/plugin_bundle_upload.go | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 5cfd8fa65..ecfd2a03b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/vmware-tanzu/tanzu-cli go 1.21 replace cloud.google.com/go => cloud.google.com/go v0.102.1 + replace github.com/vmware-tanzu/tanzu-cli/test/e2e/framework => ./test/e2e/framework require ( diff --git a/pkg/airgapped/plugin_bundle_upload.go b/pkg/airgapped/plugin_bundle_upload.go index 3cb549ea3..df0dc2e45 100644 --- a/pkg/airgapped/plugin_bundle_upload.go +++ b/pkg/airgapped/plugin_bundle_upload.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "path/filepath" + "time" "github.com/pkg/errors" @@ -67,9 +68,10 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error { if err != nil { return errors.Wrap(err, "error while constructing the repo image path") } - if err = o.uploadImage(imageTar, repoImagePath, totalImages, imagesUploaded); err != nil { - return err + if uploadErr := o.uploadImage(imageTar, repoImagePath, totalImages, imagesUploaded); uploadErr != nil { + return uploadErr } + time.Sleep(3 * time.Second) imagesUploaded++ } log.Infof("---------------------------") @@ -130,7 +132,6 @@ func (o *UploadPluginBundleOptions) uploadImage(imageTar, repoImagePath string, uploadedMsg = fmt.Sprintf(uploadedMsg, totalImages, imagesUploaded+1, repoImagePath) if spinner != nil { spinner.SetFinalText(uploadedMsg, log.LogTypeINFO) - spinner.StopSpinner() } else { log.Infof(uploadedMsg, totalImages, imagesUploaded, repoImagePath) } From b9dd3dafd05e39841e68852d4ea8f299526335d3 Mon Sep 17 00:00:00 2001 From: Chandra Pamuluri Date: Thu, 25 Apr 2024 16:45:18 -0500 Subject: [PATCH 6/8] global spinner --- pkg/airgapped/plugin_bundle_upload.go | 26 ++++++++++---------------- pkg/registry/client.go | 1 + 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/pkg/airgapped/plugin_bundle_upload.go b/pkg/airgapped/plugin_bundle_upload.go index df0dc2e45..a7a88e558 100644 --- a/pkg/airgapped/plugin_bundle_upload.go +++ b/pkg/airgapped/plugin_bundle_upload.go @@ -62,16 +62,17 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error { imagesUploaded := 0 // Iterate through all the images and publish them to the remote repository var repoImagePath string + spinner := component.NewOutputWriterSpinner(component.WithOutputStream(os.Stderr)) + defer spinner.StopSpinner() for _, ic := range manifest.ImagesToCopy { imageTar := filepath.Join(pluginBundleDir, ic.SourceTarFilePath) repoImagePath, err = utils.JoinURL(o.DestinationRepo, ic.RelativeImagePath) if err != nil { return errors.Wrap(err, "error while constructing the repo image path") } - if uploadErr := o.uploadImage(imageTar, repoImagePath, totalImages, imagesUploaded); uploadErr != nil { + if uploadErr := o.uploadImage(imageTar, repoImagePath, totalImages, imagesUploaded, spinner); uploadErr != nil { return uploadErr } - time.Sleep(3 * time.Second) imagesUploaded++ } log.Infof("---------------------------") @@ -106,32 +107,25 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error { return nil } -func (o *UploadPluginBundleOptions) uploadImage(imageTar, repoImagePath string, totalImages, imagesUploaded int) error { +func (o *UploadPluginBundleOptions) uploadImage(imageTar, repoImagePath string, totalImages, imagesUploaded int, spinner component.OutputWriterSpinner) error { uploadingMsg := fmt.Sprintf("[%d/%d] uploading image %q", totalImages, imagesUploaded, repoImagePath) errorMsg := fmt.Sprintf("[%d/%d] error while uploading image %q", totalImages, imagesUploaded, repoImagePath) uploadedMsg := "[%d/%d] uploaded image %q" - var spinner component.OutputWriterSpinner - if component.IsTTYEnabled() { - // Initialize the spinner - spinner = component.NewOutputWriterSpinner( - component.WithOutputStream(os.Stderr), - component.WithSpinnerText(uploadingMsg), - component.WithSpinnerStarted(), - ) + if !component.IsTTYEnabled() { + spinner.SetText(uploadingMsg) spinner.SetFinalText(errorMsg, log.LogTypeERROR) - defer spinner.StopSpinner() + spinner.StartSpinner() } else { log.Infof(uploadingMsg, totalImages, imagesUploaded, repoImagePath) } - if err := o.ImageProcessor.CopyImageFromTar(imageTar, repoImagePath); err != nil { return errors.Wrapf(err, errorMsg, repoImagePath) } - + time.Sleep(1 * time.Second) uploadedMsg = fmt.Sprintf(uploadedMsg, totalImages, imagesUploaded+1, repoImagePath) - if spinner != nil { - spinner.SetFinalText(uploadedMsg, log.LogTypeINFO) + if spinner == nil { + spinner.SetFinalText("", "") } else { log.Infof(uploadedMsg, totalImages, imagesUploaded, repoImagePath) } diff --git a/pkg/registry/client.go b/pkg/registry/client.go index a6649fc38..f5ba983b8 100644 --- a/pkg/registry/client.go +++ b/pkg/registry/client.go @@ -222,6 +222,7 @@ func (r *registry) CopyImageFromTar(sourceTarFile, destImageRepo string) error { Insecure: r.opts.Insecure, } } + originalStdout := os.Stderr pr, pw, pipeErr := os.Pipe() if pipeErr != nil { From ed8655bf152f6a4b3c3efc22248aa61d783b8e98 Mon Sep 17 00:00:00 2001 From: Chandra Pamuluri Date: Thu, 25 Apr 2024 16:46:44 -0500 Subject: [PATCH 7/8] sleep --- pkg/airgapped/plugin_bundle_upload.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/airgapped/plugin_bundle_upload.go b/pkg/airgapped/plugin_bundle_upload.go index a7a88e558..b5d2bd25d 100644 --- a/pkg/airgapped/plugin_bundle_upload.go +++ b/pkg/airgapped/plugin_bundle_upload.go @@ -7,7 +7,6 @@ import ( "fmt" "os" "path/filepath" - "time" "github.com/pkg/errors" @@ -122,7 +121,6 @@ func (o *UploadPluginBundleOptions) uploadImage(imageTar, repoImagePath string, if err := o.ImageProcessor.CopyImageFromTar(imageTar, repoImagePath); err != nil { return errors.Wrapf(err, errorMsg, repoImagePath) } - time.Sleep(1 * time.Second) uploadedMsg = fmt.Sprintf(uploadedMsg, totalImages, imagesUploaded+1, repoImagePath) if spinner == nil { spinner.SetFinalText("", "") From 67741693e9e47b6ec8fddf55634b8d42d1e8a245 Mon Sep 17 00:00:00 2001 From: Chandra Pamuluri Date: Tue, 30 Apr 2024 10:33:00 -0500 Subject: [PATCH 8/8] revert --- pkg/airgapped/plugin_bundle_upload.go | 22 +++++++++------------- pkg/registry/client.go | 14 -------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/pkg/airgapped/plugin_bundle_upload.go b/pkg/airgapped/plugin_bundle_upload.go index b5d2bd25d..44e87854f 100644 --- a/pkg/airgapped/plugin_bundle_upload.go +++ b/pkg/airgapped/plugin_bundle_upload.go @@ -4,7 +4,6 @@ package airgapped import ( - "fmt" "os" "path/filepath" @@ -17,7 +16,6 @@ import ( "github.com/vmware-tanzu/tanzu-cli/pkg/carvelhelpers" "github.com/vmware-tanzu/tanzu-cli/pkg/plugininventory" "github.com/vmware-tanzu/tanzu-cli/pkg/utils" - "github.com/vmware-tanzu/tanzu-plugin-runtime/component" "github.com/vmware-tanzu/tanzu-plugin-runtime/log" ) @@ -57,22 +55,19 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error { return errors.Wrap(err, "error while parsing plugin migration manifest") } - totalImages := len(manifest.ImagesToCopy) - imagesUploaded := 0 // Iterate through all the images and publish them to the remote repository - var repoImagePath string - spinner := component.NewOutputWriterSpinner(component.WithOutputStream(os.Stderr)) - defer spinner.StopSpinner() for _, ic := range manifest.ImagesToCopy { imageTar := filepath.Join(pluginBundleDir, ic.SourceTarFilePath) - repoImagePath, err = utils.JoinURL(o.DestinationRepo, ic.RelativeImagePath) + repoImagePath, err := utils.JoinURL(o.DestinationRepo, ic.RelativeImagePath) if err != nil { return errors.Wrap(err, "error while constructing the repo image path") } - if uploadErr := o.uploadImage(imageTar, repoImagePath, totalImages, imagesUploaded, spinner); uploadErr != nil { - return uploadErr + log.Infof("---------------------------") + log.Infof("uploading image %q", repoImagePath) + err = o.ImageProcessor.CopyImageFromTar(imageTar, repoImagePath) + if err != nil { + return errors.Wrap(err, "error while uploading image") } - imagesUploaded++ } log.Infof("---------------------------") log.Infof("---------------------------") @@ -106,12 +101,13 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error { return nil } +/* func (o *UploadPluginBundleOptions) uploadImage(imageTar, repoImagePath string, totalImages, imagesUploaded int, spinner component.OutputWriterSpinner) error { uploadingMsg := fmt.Sprintf("[%d/%d] uploading image %q", totalImages, imagesUploaded, repoImagePath) errorMsg := fmt.Sprintf("[%d/%d] error while uploading image %q", totalImages, imagesUploaded, repoImagePath) uploadedMsg := "[%d/%d] uploaded image %q" - if !component.IsTTYEnabled() { + if component.IsTTYEnabled() { spinner.SetText(uploadingMsg) spinner.SetFinalText(errorMsg, log.LogTypeERROR) spinner.StartSpinner() @@ -130,7 +126,7 @@ func (o *UploadPluginBundleOptions) uploadImage(imageTar, repoImagePath string, return nil } - +*/ // mergePluginInventoryMetadata merges the downloaded plugin inventory metadata with // existing plugin inventory metadata available on the remote repository func (o *UploadPluginBundleOptions) mergePluginInventoryMetadata(pluginInventoryMetadataImageWithTag, bundledPluginInventoryMetadataDBFilePath, tempDir string) error { diff --git a/pkg/registry/client.go b/pkg/registry/client.go index f5ba983b8..570b775aa 100644 --- a/pkg/registry/client.go +++ b/pkg/registry/client.go @@ -7,7 +7,6 @@ import ( "archive/tar" "bytes" "io" - "os" "github.com/cppforlife/go-cli-ui/ui" regname "github.com/google/go-containerregistry/pkg/name" @@ -222,19 +221,6 @@ func (r *registry) CopyImageFromTar(sourceTarFile, destImageRepo string) error { Insecure: r.opts.Insecure, } } - - originalStdout := os.Stderr - pr, pw, pipeErr := os.Pipe() - if pipeErr != nil { - return pipeErr - } - defer func() { - pr.Close() - pw.Close() - os.Stderr = originalStdout - }() - os.Stderr = pw - err := copyOptions.Run() if err != nil { return err