Skip to content

Commit 0bb0ebe

Browse files
committed
Enhance plugin installation UX
PR updates the plugin installation UX, - As part of plugin installation in-progress message, it adds total plugins needs to installed and the number of plugins being installed The in-progress message will disappear once the installation completes. Next plugin in-progress will be shown. - No log messages will be showed once the plugin installation completes.
1 parent 397ea59 commit 0bb0ebe

4 files changed

Lines changed: 65 additions & 20 deletions

File tree

pkg/command/context.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,22 @@ func syncContextPlugins(cmd *cobra.Command, contextType configtypes.ContextType,
317317

318318
errList := make([]error, 0)
319319
log.Infof("Installing the following plugins recommended by context '%s':", ctxName)
320+
pluginmanager.SetTotalPluginsToInstall(len(pluginsNeedToBeInstalled))
320321
displayToBeInstalledPluginsAsTable(plugins, cmd.ErrOrStderr())
321322
for i := range pluginsNeedToBeInstalled {
322323
err = pluginmanager.InstallStandalonePlugin(pluginsNeedToBeInstalled[i].Name, pluginsNeedToBeInstalled[i].RecommendedVersion, pluginsNeedToBeInstalled[i].Target)
323324
if err != nil {
324325
errList = append(errList, err)
326+
} else {
327+
pluginmanager.IncrementPluginsInstalledCount(1)
325328
}
326329
}
327330
err = kerrors.NewAggregate(errList)
328331
if err == nil {
329332
log.Success("Successfully installed all recommended plugins.")
330333
}
331-
334+
pluginmanager.SetTotalPluginsToInstall(0)
335+
pluginmanager.SetPluginsInstalledCount(0)
332336
return err
333337
}
334338

pkg/command/plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ func newInstallPluginCmd() *cobra.Command {
276276
return fmt.Errorf("the '%s' argument can only be used with the '--group' flag", cli.AllPlugins)
277277
}
278278

279+
pluginmanager.SetTotalPluginsToInstall(1)
279280
pluginVersion := version
280281
err = pluginmanager.InstallStandalonePlugin(pluginName, pluginVersion, getTarget())
281282
if err != nil {

pkg/pluginmanager/essentials.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func installPluginsFromEssentialPluginGroup(name, version string) (string, error
6969
if err != nil {
7070
return "", fmt.Errorf("failed to install plugins from group: %w", err)
7171
}
72+
log.Successf("successfully installed all plugins from group '%s'", groupWithVersion)
7273

7374
// If the installation is successful, return the group with version.
7475
return groupWithVersion, nil

pkg/pluginmanager/manager.go

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ const (
5656
errorNoActiveContexForGivenContextType = "there is no active context for the given context type `%v`"
5757
)
5858

59+
var totalPluginsToInstall = 0
60+
var pluginsInstalledCount = 0
5961
var execCommand = exec.Command
6062

6163
type DeletePluginOptions struct {
@@ -475,6 +477,21 @@ func InstallStandalonePlugin(pluginName, version string, target configtypes.Targ
475477
return installPlugin(pluginName, version, target, "")
476478
}
477479

480+
// SetTotalPluginsToInstall sets the total number of plugins to install
481+
func SetTotalPluginsToInstall(total int) {
482+
totalPluginsToInstall = total
483+
}
484+
485+
// SetPluginsInstalledCount sets the number of plugins installed already
486+
func SetPluginsInstalledCount(count int) {
487+
pluginsInstalledCount = count
488+
}
489+
490+
// IncrementPluginsInstalledCount increments the number of plugins installed
491+
func IncrementPluginsInstalledCount(count int) {
492+
pluginsInstalledCount += count
493+
}
494+
478495
// installs a plugin by name, version and target.
479496
// If the contextName is not empty, it implies the plugin is a context-scope plugin, otherwise
480497
// we are installing a standalone plugin.
@@ -560,7 +577,7 @@ func installPlugin(pluginName, version string, target configtypes.Target, contex
560577
if len(matchedPlugins) == 1 {
561578
return installOrUpgradePlugin(&matchedPlugins[0], matchedPlugins[0].RecommendedVersion, false)
562579
}
563-
580+
// there can be only one plugin with the same name and target, so we can safely return the first one
564581
for i := range matchedPlugins {
565582
if matchedPlugins[i].Target == target {
566583
return installOrUpgradePlugin(&matchedPlugins[i], matchedPlugins[i].RecommendedVersion, false)
@@ -599,24 +616,31 @@ func InstallPluginsFromGroup(pluginName, groupIDAndVersion string, options ...Pl
599616
// InstallPluginsFromGivenPluginGroup installs either the specified plugin or all plugins from given plugin group plugins.
600617
func InstallPluginsFromGivenPluginGroup(pluginName, groupIDAndVersion string, pg *plugininventory.PluginGroup) (string, error) {
601618
numErrors := 0
602-
numInstalled := 0
603619
mandatoryPluginsExist := false
604620
pluginExist := false
621+
pluginsInstalledCount = 0
622+
623+
pluginsToInstall := make([]*plugininventory.PluginGroupPluginEntry, 0)
605624
for _, plugin := range pg.Versions[pg.RecommendedVersion] {
606625
if pluginName == cli.AllPlugins || pluginName == plugin.Name {
607626
pluginExist = true
608627
if plugin.Mandatory {
609628
mandatoryPluginsExist = true
610-
err := InstallStandalonePlugin(plugin.Name, plugin.Version, plugin.Target)
611-
if err != nil {
612-
numErrors++
613-
log.Warningf("unable to install plugin '%s': %v", plugin.Name, err.Error())
614-
} else {
615-
numInstalled++
616-
}
629+
pluginsToInstall = append(pluginsToInstall, plugin) // Add mandatory plugin to the slice
617630
}
618631
}
619632
}
633+
SetTotalPluginsToInstall(len(pluginsToInstall))
634+
SetPluginsInstalledCount(0)
635+
for _, plugin := range pluginsToInstall {
636+
err := InstallStandalonePlugin(plugin.Name, plugin.Version, plugin.Target)
637+
if err != nil {
638+
numErrors++
639+
log.Warningf("unable to install plugin '%s': %v", plugin.Name, err.Error())
640+
} else {
641+
IncrementPluginsInstalledCount(1)
642+
}
643+
}
620644

621645
if !pluginExist {
622646
return groupIDAndVersion, fmt.Errorf("plugin '%s' is not part of the group '%s'", pluginName, groupIDAndVersion)
@@ -633,9 +657,11 @@ func InstallPluginsFromGivenPluginGroup(pluginName, groupIDAndVersion string, pg
633657
return groupIDAndVersion, fmt.Errorf("could not install %d plugin(s) from group '%s'", numErrors, groupIDAndVersion)
634658
}
635659

636-
if numInstalled == 0 {
660+
if pluginsInstalledCount == 0 {
637661
return groupIDAndVersion, fmt.Errorf("plugin '%s' is not part of the group '%s'", pluginName, groupIDAndVersion)
638662
}
663+
SetTotalPluginsToInstall(0)
664+
SetPluginsInstalledCount(0)
639665

640666
return groupIDAndVersion, nil
641667
}
@@ -704,7 +730,7 @@ func getPluginInstallationMessage(p *discovery.Discovered, version string, isPlu
704730
installingMsg = fmt.Sprintf("Installing plugin '%v:%v' %v(from cache)", p.Name, version, withTarget)
705731
installedMsg = fmt.Sprintf("Installed plugin '%v:%v' %v(from cache)", p.Name, version, withTarget)
706732
} else {
707-
installingMsg = fmt.Sprintf("Plugin '%v:%v' %vis already installed. Reinitializing...", p.Name, version, withTarget)
733+
installingMsg = fmt.Sprintf("Already installed : Plugin '%v:%v' %v", p.Name, version, withTarget)
708734
installedMsg = fmt.Sprintf("Reinitialized plugin '%v:%v' %v", p.Name, version, withTarget)
709735
}
710736
} else {
@@ -734,8 +760,13 @@ func installOrUpgradePlugin(p *discovery.Discovered, version string, installTest
734760
}
735761

736762
// Log message based on different installation conditions
737-
installingMsg, installedMsg, errMsg := getPluginInstallationMessage(p, version, plugin != nil, isPluginAlreadyInstalled)
763+
installingMsg, _, errMsg := getPluginInstallationMessage(p, version, plugin != nil, isPluginAlreadyInstalled)
738764

765+
installingMsg = fmt.Sprintf("[%v/%v] %v", pluginsInstalledCount, totalPluginsToInstall, installingMsg)
766+
errMsg = fmt.Sprintf("[%v/%v] %v", pluginsInstalledCount, totalPluginsToInstall, errMsg)
767+
numPluginsInstalled := fmt.Sprintf("%d plugins installed out of %d", pluginsInstalledCount, totalPluginsToInstall)
768+
numPluginsInstalledWithLog := fmt.Sprintf("%s%s", log.GetLogTypeIndicator(log.LogTypeERROR), numPluginsInstalled)
769+
errMsg = fmt.Sprintf("%s\n%s", errMsg, numPluginsInstalledWithLog)
739770
var spinner component.OutputWriterSpinner
740771

741772
// Initialize the spinner if the spinner is allowed
@@ -752,7 +783,8 @@ func installOrUpgradePlugin(p *discovery.Discovered, version string, installTest
752783

753784
pluginErr := verifyInstallAndInitializePlugin(plugin, p, version, installTestPlugin)
754785
if pluginErr == nil && spinner != nil {
755-
spinner.SetFinalText(installedMsg, log.LogTypeINFO)
786+
// unset the final text to avoid the spinner from showing the final text
787+
spinner.SetFinalText("", "")
756788
}
757789
return pluginErr
758790
}
@@ -1104,7 +1136,6 @@ func InstallPluginsFromLocalSource(pluginName, version string, target configtype
11041136
if err != nil {
11051137
return errors.Wrap(err, "unable to discover plugins")
11061138
}
1107-
11081139
var errList []error
11091140

11101141
var matchedPlugins []discovery.Discovered
@@ -1132,16 +1163,24 @@ func InstallPluginsFromLocalSource(pluginName, version string, target configtype
11321163
return installOrUpgradePlugin(&matchedPlugins[0], version, installTestPlugin)
11331164
}
11341165

1166+
var pluginsToInstall []*discovery.Discovered
11351167
for i := range matchedPlugins {
11361168
// Install all plugins otherwise include all matching plugins
11371169
if pluginName == cli.AllPlugins || matchedPlugins[i].Target == target {
1138-
err = installOrUpgradePlugin(&matchedPlugins[i], version, installTestPlugin)
1139-
if err != nil {
1140-
errList = append(errList, err)
1141-
}
1170+
plugin := matchedPlugins[i]
1171+
pluginsToInstall = append(pluginsToInstall, &plugin)
11421172
}
11431173
}
1144-
1174+
SetTotalPluginsToInstall(len(pluginsToInstall))
1175+
SetPluginsInstalledCount(0)
1176+
for _, plugin := range pluginsToInstall {
1177+
err = installOrUpgradePlugin(plugin, version, installTestPlugin)
1178+
if err != nil {
1179+
errList = append(errList, err)
1180+
}
1181+
}
1182+
SetTotalPluginsToInstall(0)
1183+
SetPluginsInstalledCount(0)
11451184
err = kerrors.NewAggregate(errList)
11461185
if err != nil {
11471186
return err

0 commit comments

Comments
 (0)