Skip to content

Commit 52159d1

Browse files
committed
signal handling
1 parent f5a9151 commit 52159d1

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

pkg/airgapped/plugin_bundle_upload.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
package airgapped
55

66
import (
7+
"fmt"
78
"os"
9+
"os/signal"
810
"path/filepath"
911

1012
"github.com/pkg/errors"
@@ -16,6 +18,7 @@ import (
1618
"github.com/vmware-tanzu/tanzu-cli/pkg/carvelhelpers"
1719
"github.com/vmware-tanzu/tanzu-cli/pkg/plugininventory"
1820
"github.com/vmware-tanzu/tanzu-cli/pkg/utils"
21+
"github.com/vmware-tanzu/tanzu-plugin-runtime/component"
1922
"github.com/vmware-tanzu/tanzu-plugin-runtime/log"
2023
)
2124

@@ -55,19 +58,43 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error {
5558
return errors.Wrap(err, "error while parsing plugin migration manifest")
5659
}
5760

61+
totalNumberOfImages := len(manifest.ImagesToCopy)
62+
imagesCompletedUpload := 0
63+
64+
var uploadingMessage, uploadedMessage, errMessage string
65+
var spinner component.OutputWriterSpinner
66+
5867
// Iterate through all the images and publish them to the remote repository
5968
for _, ic := range manifest.ImagesToCopy {
6069
imageTar := filepath.Join(pluginBundleDir, ic.SourceTarFilePath)
6170
repoImagePath, err := utils.JoinURL(o.DestinationRepo, ic.RelativeImagePath)
6271
if err != nil {
6372
return errors.Wrap(err, "error while constructing the repo image path")
6473
}
65-
log.Infof("---------------------------")
66-
log.Infof("uploading image %q", repoImagePath)
74+
uploadingMessage = fmt.Sprintf("[%v/%v] uploading image %s", imagesCompletedUpload, totalNumberOfImages, repoImagePath)
75+
errMessage = fmt.Sprintf("[%v/%v] failed to upload image %s", imagesCompletedUpload, totalNumberOfImages, repoImagePath)
76+
uploadedMessage = fmt.Sprintf("[%v/%v] uploaded image %s", imagesCompletedUpload+1, totalNumberOfImages, repoImagePath)
77+
spinner = component.NewOutputWriterSpinner(component.WithOutputStream(os.Stderr),
78+
component.WithSpinnerText(uploadingMessage),
79+
component.WithSpinnerStarted(), component.WithSpinnerFinalText(uploadedMessage, log.LogTypeINFO))
80+
81+
signalChannel := make(chan os.Signal, 1)
82+
defer func() {
83+
signal.Stop(signalChannel)
84+
close(signalChannel)
85+
spinner.StopSpinner()
86+
component.StopAllSpinners()
87+
}()
88+
// Initialize the signal catcher
89+
go utils.SignalCatcherInitialization(signalChannel, spinner, errMessage, log.LogTypeERROR, "")
90+
91+
//log.Infof("---------------------------")
92+
//log.Infof("uploading image %q", repoImagePath)
6793
err = o.ImageProcessor.CopyImageFromTar(imageTar, repoImagePath)
6894
if err != nil {
6995
return errors.Wrap(err, "error while uploading image")
7096
}
97+
imagesCompletedUpload++
7198
}
7299
log.Infof("---------------------------")
73100
log.Infof("---------------------------")

pkg/utils/signal_handler.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2022 VMware, Inc. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package utils
5+
6+
import (
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
11+
"github.com/vmware-tanzu/tanzu-plugin-runtime/component"
12+
"github.com/vmware-tanzu/tanzu-plugin-runtime/log"
13+
)
14+
15+
// SignalCatcherInitialization initializes a signal catcher to catch OS signals and stop the spinner
16+
func SignalCatcherInitialization(signalChannel chan os.Signal, s component.OutputWriterSpinner, spinnerFinalText string, spinnerFinalTextLogType log.LogType, errorMsgAfterSpinnerStop string) {
17+
// Register the channel to receive interrupt signals (e.g., Ctrl+C)
18+
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
19+
20+
sig := <-signalChannel
21+
if sig != nil {
22+
if s != nil {
23+
s.SetFinalText(spinnerFinalText, spinnerFinalTextLogType)
24+
s.StopSpinner()
25+
}
26+
if errorMsgAfterSpinnerStop != "" {
27+
log.Errorf(errorMsgAfterSpinnerStop)
28+
}
29+
os.Exit(128 + int(sig.(syscall.Signal)))
30+
}
31+
}

0 commit comments

Comments
 (0)