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

cmd/info: Support for multiple indexes #563

Merged
merged 2 commits into from
Mar 29, 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
33 changes: 19 additions & 14 deletions cmd/krew/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,38 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

"sigs.k8s.io/krew/internal/info"
"sigs.k8s.io/krew/internal/index/indexscanner"
"sigs.k8s.io/krew/internal/installation"
"sigs.k8s.io/krew/internal/pathutil"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
)

// infoCmd represents the info command
var infoCmd = &cobra.Command{
Use: "info",
Short: "Show information about a kubectl plugin",
Long: `Show information about a kubectl plugin.

This command can be used to print information such as its download URL, last
available version, platform availability and the caveats.

Example:
kubectl krew info PLUGIN`,
Use: "info",
Short: "Show information about an available plugin",
Long: `Show detailed information about an available plugin.`,
Example: "kubectl krew info PLUGIN",
RunE: func(cmd *cobra.Command, args []string) error {
plugin, err := info.LoadManifestFromReceiptOrIndex(paths, args[0])
index, plugin := pathutil.CanonicalPluginName(args[0])

p, err := indexscanner.LoadPluginByName(paths.IndexPluginsPath(index), plugin)
if os.IsNotExist(err) {
return errors.Errorf("plugin %q not found", args[0])
return errors.Errorf("plugin %q not found in index %q", args[0], index)
} else if err != nil {
return errors.Wrap(err, "failed to load plugin manifest")
}
printPluginInfo(os.Stdout, plugin)
printPluginInfo(os.Stdout, index, p)
return nil
},
PreRunE: checkIndex,
Args: cobra.ExactArgs(1),
}

func printPluginInfo(out io.Writer, plugin index.Plugin) {
func printPluginInfo(out io.Writer, indexName string, plugin index.Plugin) {
fmt.Fprintf(out, "NAME: %s\n", plugin.Name)
fmt.Fprintf(out, "INDEX: %s\n", indexName)
if platform, ok, err := installation.GetMatchingPlatform(plugin.Spec.Platforms); err == nil && ok {
if platform.URI != "" {
fmt.Fprintf(out, "URI: %s\n", platform.URI)
Expand Down Expand Up @@ -95,5 +95,10 @@ func indent(s string) string {
}

func init() {
if os.Getenv(constants.EnableMultiIndexSwitch) != "" {
// TODO(ahmetb) move back into Example field above (with 2-space indent) once feature gate is removed.
infoCmd.Example += "\n kubectl krew info INDEX/PLUGIN"
}

rootCmd.AddCommand(infoCmd)
}
32 changes: 30 additions & 2 deletions integration_test/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@

package integrationtest

import "testing"
import (
"strings"
"testing"

"sigs.k8s.io/krew/pkg/constants"
)

func TestKrewInfo(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()

test.WithIndex().Krew("info", validPlugin).RunOrFail()
out := string(test.WithIndex().Krew("info", validPlugin).RunOrFailOutput())
expected := `INDEX: default`
if !strings.Contains(out, expected) {
t.Fatalf("info output doesn't have %q. output=%q", expected, out)
}
}

func TestKrewInfoInvalidPlugin(t *testing.T) {
Expand All @@ -37,3 +46,22 @@ func TestKrewInfoInvalidPlugin(t *testing.T) {
t.Errorf("Expected `krew info %s` to fail", plugin)
}
}

func TestKrewInfoCustomIndex(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()

test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
defaultIndexRoot := test.TempDir().Path("index/" + constants.DefaultIndexName)

test.Krew("index", "add", "foo", defaultIndexRoot).RunOrFail()
ahmetb marked this conversation as resolved.
Show resolved Hide resolved
test.Krew("install", "foo/"+validPlugin).RunOrFail()

out := string(test.Krew("info", "foo/"+validPlugin).RunOrFailOutput())
expected := `INDEX: foo`
if !strings.Contains(out, expected) {
t.Fatalf("info output doesn't have %q. output=%q", expected, out)
}
}
2 changes: 1 addition & 1 deletion internal/index/indexscanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func LoadPluginByName(pluginsDir, pluginName string) (index.Plugin, error) {
return index.Plugin{}, errors.Errorf("plugin name %q not allowed", pluginName)
}

klog.V(4).Infof("Reading plugin %q", pluginName)
klog.V(4).Infof("Reading plugin %q from %s", pluginName, pluginsDir)
return ReadPluginFromFile(filepath.Join(pluginsDir, pluginName+constants.ManifestExtension))
}

Expand Down
45 changes: 0 additions & 45 deletions internal/info/info.go

This file was deleted.

116 changes: 0 additions & 116 deletions internal/info/info_test.go

This file was deleted.