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

add utility method to list installed plugins by index #549

Merged
merged 1 commit into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 37 additions & 9 deletions internal/installation/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,52 @@ import (

"sigs.k8s.io/krew/internal/installation/receipt"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
)

// ListInstalledPlugins returns a list of all install plugins in a
// name:version format based on the install receipts at the specified dir.
func ListInstalledPlugins(receiptsDir string) (map[string]string, error) {
matches, err := filepath.Glob(filepath.Join(receiptsDir, "*"+constants.ManifestExtension))
installed := make(map[string]string)
receipts, err := getInstalledPluginReceipts(receiptsDir)
if err != nil {
return nil, errors.Wrapf(err, "failed to grab receipts directory (%s) for manifests", receiptsDir)
return nil, err
}
klog.V(4).Infof("Found %d install receipts in %s", len(matches), receiptsDir)
installed := make(map[string]string)
for _, m := range matches {
r, err := receipt.Load(m)
for _, r := range receipts {
installed[r.GetObjectMeta().GetName()] = r.Spec.Version
}
return installed, nil
}

// InstalledPluginsFromIndex returns a list of all install plugins from a particular index.
func InstalledPluginsFromIndex(receiptsDir, indexName string) ([]index.Receipt, error) {
var out []index.Receipt
receipts, err := getInstalledPluginReceipts(receiptsDir)
if err != nil {
return nil, err
}
for _, r := range receipts {
if r.Status.Source.Name == indexName {
out = append(out, r)
}
}
return out, nil
}

func getInstalledPluginReceipts(receiptsDir string) ([]index.Receipt, error) {
files, err := filepath.Glob(filepath.Join(receiptsDir, "*"+constants.ManifestExtension))
if err != nil {
return nil, errors.Wrapf(err, "failed to glob receipts directory (%s) for manifests", receiptsDir)
}
out := make([]index.Receipt, 0, len(files))
for _, f := range files {
r, err := receipt.Load(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse plugin install receipt %s", m)
return nil, errors.Wrapf(err, "failed to parse plugin install receipt %s", f)
}
out = append(out, r)
klog.V(4).Infof("parsed receipt for %s: version=%s", r.GetObjectMeta().GetName(), r.Spec.Version)
installed[r.GetObjectMeta().GetName()] = r.Spec.Version

}
return installed, nil
return out, nil
}
32 changes: 32 additions & 0 deletions internal/installation/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,35 @@ func TestListInstalledPlugins(t *testing.T) {
})
}
}

func TestInstalledPluginsFromIndex(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chriskim06 I'm hoping this is a good test example for reproducing elsewhere.

tempDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

indexA := index.ReceiptStatus{Source: index.SourceIndex{Name: "a"}}
var indexNone index.ReceiptStatus

for _, testReceipt := range []index.Receipt{
testutil.NewReceipt().WithPlugin(testutil.NewPlugin().WithName("a1").V()).WithStatus(indexA).V(),
testutil.NewReceipt().WithPlugin(testutil.NewPlugin().WithName("a2").V()).WithStatus(indexA).V(),
testutil.NewReceipt().WithPlugin(testutil.NewPlugin().WithName("default1").V()).WithStatus(indexNone).V(),
} {
tempDir.WriteYAML(testReceipt.Name+constants.ManifestExtension, testReceipt)
}

v, err := InstalledPluginsFromIndex(tempDir.Root(), "a")
if err != nil {
t.Fatal(err)
}
if expected, got := 2, len(v); expected != got {
t.Fatalf("expected %d, got: %d for index 'a'", expected, got)
}

v, err = InstalledPluginsFromIndex(tempDir.Root(), constants.DefaultIndexName)
if err != nil {
t.Fatal(err)
}
if expected, got := 1, len(v); expected != got {
t.Fatalf("expected %d, got: %d for index 'a'", expected, got)
}
}