Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show new/updated plugins list after index updates #593

Merged
merged 2 commits into from
Apr 23, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 50 additions & 37 deletions cmd/krew/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -59,59 +58,77 @@ 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)
}
}

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)]
ahmetb marked this conversation as resolved.
Show resolved Hide resolved
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 {
Expand All @@ -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)
}
corneliusweig marked this conversation as resolved.
Show resolved Hide resolved
}

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, ", "))
}

Expand Down