From f711cfe0e09490365aed06f7ab9c2de36935ea76 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Wed, 15 Apr 2020 17:15:41 -0700 Subject: [PATCH 1/2] Show new/updated plugins list after index updates * Adjusted the showUpdatedPlugins logic to behave well with multi-index mode * Adjusted the "Updated the local copy of plugin index" message for multi-index mode. (We won't show index name if it's 'default' index.) Signed-off-by: Ahmet Alp Balkan --- cmd/krew/cmd/update.go | 87 ++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/cmd/krew/cmd/update.go b/cmd/krew/cmd/update.go index cb451dde..003dc134 100644 --- a/cmd/krew/cmd/update.go +++ b/cmd/krew/cmd/update.go @@ -30,7 +30,6 @@ import ( "sigs.k8s.io/krew/internal/index/indexscanner" "sigs.k8s.io/krew/internal/installation" "sigs.k8s.io/krew/pkg/constants" - "sigs.k8s.io/krew/pkg/index" ) // updateCmd represents the update command @@ -59,27 +58,28 @@ func showFormattedPluginsInfo(out io.Writer, header string, plugins []string) { fmt.Fprintf(out, "%s", b.String()) } -func showUpdatedPlugins(out io.Writer, preUpdate, posUpdate []index.Plugin, installedPlugins map[string]string) { - var newPlugins []index.Plugin - var updatedPlugins []index.Plugin +func showUpdatedPlugins(out io.Writer, preUpdate, postUpdate []pluginEntry, installedPlugins map[string]string) { + var newPlugins []pluginEntry + var updatedPlugins []pluginEntry - oldIndex := make(map[string]index.Plugin) + oldIndexMap := make(map[string]pluginEntry) for _, p := range preUpdate { - oldIndex[p.Name] = p + oldIndexMap[canonicalName(p.p, p.indexName)] = p } - for _, p := range posUpdate { - old, ok := oldIndex[p.Name] + for _, p := range postUpdate { + cName := canonicalName(p.p, p.indexName) + old, ok := oldIndexMap[cName] if !ok { newPlugins = append(newPlugins, p) continue } - if _, ok := installedPlugins[p.Name]; !ok { + if _, ok := installedPlugins[cName]; !ok { continue } - if old.Spec.Version != p.Spec.Version { + if old.p.Spec.Version != p.p.Spec.Version { updatedPlugins = append(updatedPlugins, p) } } @@ -87,31 +87,48 @@ func showUpdatedPlugins(out io.Writer, preUpdate, posUpdate []index.Plugin, inst if len(newPlugins) > 0 { var s []string for _, p := range newPlugins { - s = append(s, p.Name) + s = append(s, displayName(p.p, p.indexName)) } - showFormattedPluginsInfo(out, "New plugins available", s) } if len(updatedPlugins) > 0 { var s []string for _, p := range updatedPlugins { - old := oldIndex[p.Name] - s = append(s, fmt.Sprintf("%s %s -> %s", p.Name, old.Spec.Version, p.Spec.Version)) + old := oldIndexMap[canonicalName(p.p, p.indexName)] + name := displayName(p.p, p.indexName) + s = append(s, fmt.Sprintf("%s %s -> %s", name, old.p.Spec.Version, p.p.Spec.Version)) } - showFormattedPluginsInfo(out, "Upgrades available for installed plugins", s) } } -func ensureIndexesUpdated(_ *cobra.Command, _ []string) error { - preUpdateIndex, _ := indexscanner.LoadPluginListFromFS(paths.IndexPluginsPath(constants.DefaultIndexName)) +// loadPlugins loads plugin entries from specified indexes. Parse errors +// are ignored and logged. +func loadPlugins(indexes []indexoperations.Index) []pluginEntry { + var out []pluginEntry + for _, idx := range indexes { + list, err := indexscanner.LoadPluginListFromFS(paths.IndexPluginsPath(idx.Name)) + if err != nil { + klog.V(1).Infof("WARNING: failed to load plugin list from %q: %v", idx.Name, err) + continue + } + for _, v := range list { + out = append(out, pluginEntry{indexName: idx.Name, p: v}) + } + } + return out +} +func ensureIndexesUpdated(_ *cobra.Command, _ []string) error { indexes, err := indexoperations.ListIndexes(paths) if err != nil { return errors.Wrap(err, "failed to list indexes") } + // collect list of existing plugins + preUpdatePlugins := loadPlugins(indexes) + var failed []string var returnErr error for _, idx := range indexes { @@ -124,31 +141,27 @@ func ensureIndexesUpdated(_ *cobra.Command, _ []string) error { returnErr = err } } - } - - fmt.Fprintln(os.Stderr, "Updated the local copy of plugin index.") - if len(preUpdateIndex) == 0 { - return nil + if os.Getenv(constants.EnableMultiIndexSwitch) == "" || isDefaultIndex(idx.Name) { + fmt.Fprintln(os.Stderr, "Updated the local copy of plugin index.") + } else { + fmt.Fprintf(os.Stderr, "Updated the local copy of plugin index %q.\n", idx.Name) + } } - posUpdateIndex, err := indexscanner.LoadPluginListFromFS(paths.IndexPluginsPath(constants.DefaultIndexName)) - if err != nil { - return errors.Wrap(err, "failed to load plugin index after update") - } + if len(preUpdatePlugins) != 0 { + postUpdatePlugins := loadPlugins(indexes) - receipts, err := installation.GetInstalledPluginReceipts(paths.InstallReceiptsPath()) - if err != nil { - return errors.Wrap(err, "failed to load installed plugins list after update") - } - installedPlugins := make(map[string]string) - for _, receipt := range receipts { - installedPlugins[receipt.Name] = receipt.Spec.Version + receipts, err := installation.GetInstalledPluginReceipts(paths.InstallReceiptsPath()) + if err != nil { + return errors.Wrap(err, "failed to load installed plugins list after update") + } + installedPlugins := make(map[string]string) + for _, receipt := range receipts { + installedPlugins[canonicalName(receipt.Plugin, indexOf(receipt))] = receipt.Spec.Version + } + showUpdatedPlugins(os.Stderr, preUpdatePlugins, postUpdatePlugins, installedPlugins) } - - // TODO(chriskim06) consider commenting this out when refactoring for custom indexes - showUpdatedPlugins(os.Stderr, preUpdateIndex, posUpdateIndex, installedPlugins) - return errors.Wrapf(returnErr, "failed to update the following indexes: %s\n", strings.Join(failed, ", ")) } From 37f9692e3db8d354137365b2e48ec0821fb914ba Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Sat, 18 Apr 2020 20:12:27 -0700 Subject: [PATCH 2/2] Fix bug printing success after error Signed-off-by: Ahmet Alp Balkan --- cmd/krew/cmd/update.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/krew/cmd/update.go b/cmd/krew/cmd/update.go index 003dc134..1e053dd1 100644 --- a/cmd/krew/cmd/update.go +++ b/cmd/krew/cmd/update.go @@ -140,6 +140,7 @@ func ensureIndexesUpdated(_ *cobra.Command, _ []string) error { if returnErr == nil { returnErr = err } + continue } if os.Getenv(constants.EnableMultiIndexSwitch) == "" || isDefaultIndex(idx.Name) {