Skip to content

Commit

Permalink
krew info: Improve error message when plugin doesn't exist (kubernete…
Browse files Browse the repository at this point in the history
…s-sigs#14)

* krew info: Improve error message when plugin doesn't exist

* fixup! krew info: Improve error message when plugin doesn't exist
  • Loading branch information
superbrothers authored and ahmetb committed Aug 6, 2018
1 parent 87dbda0 commit 43fb566
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cmd/krew/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Use this command to find out about plugin requirements and caveats.`,
Run: func(cmd *cobra.Command, args []string) {
for _, arg := range args {
plugin, err := indexscanner.LoadPluginFileFromFS(paths.Index, arg)
if err != nil {
if os.IsNotExist(err) {
glog.Fatalf("plugin %q not found", arg)
} else if err != nil {
glog.Fatal(err)
}
printPluginInfo(os.Stdout, plugin)
Expand Down
14 changes: 10 additions & 4 deletions pkg/index/indexscanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func LoadPluginListFromFS(indexDir string) (index.PluginList, error) {
return indexList, nil
}

// LoadPluginFileFromFS loads a plugins index file by its name.
// LoadPluginFileFromFS loads a plugins index file by its name. When plugin
// file not found, it returns an error that can be checked with os.IsNotExist.
func LoadPluginFileFromFS(indexDir, pluginName string) (index.Plugin, error) {
if !index.IsSafePluginName(pluginName) {
return index.Plugin{}, fmt.Errorf("plugin name %q not allowed", pluginName)
Expand All @@ -76,17 +77,22 @@ func LoadPluginFileFromFS(indexDir, pluginName string) (index.Plugin, error) {
return index.Plugin{}, err
}
p, err := ReadPluginFile(filepath.Join(indexDir, pluginName+".yaml"))
if err != nil {
if os.IsNotExist(err) {
return index.Plugin{}, err
} else if err != nil {
return index.Plugin{}, fmt.Errorf("failed to read the plugin file, err: %v", err)
}
return p, p.Validate(pluginName)
}

// ReadPluginFile loads a file from the FS
// ReadPluginFile loads a file from the FS. When plugin file not found, it
// returns an error that can be checked with os.IsNotExist.
// TODO(lbb): Add object verification
func ReadPluginFile(indexFilePath string) (index.Plugin, error) {
f, err := os.Open(indexFilePath)
if err != nil {
if os.IsNotExist(err) {
return index.Plugin{}, err
} else if err != nil {
return index.Plugin{}, fmt.Errorf("failed to open index file, err: %v", err)
}
return DecodePluginFile(f)
Expand Down
21 changes: 15 additions & 6 deletions pkg/index/indexscanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package indexscanner

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -125,33 +126,37 @@ func TestLoadIndexFileFromFS(t *testing.T) {
pluginName string
}
tests := []struct {
name string
args args
wantErr bool
name string
args args
wantErr bool
wantIsNotExistErr bool
}{
{
name: "load single index file",
args: args{
indexDir: filepath.Join(testdataPath(t), "testindex"),
pluginName: "foo",
},
wantErr: false,
wantErr: false,
wantIsNotExistErr: false,
},
{
name: "plugin file not found",
args: args{
indexDir: filepath.FromSlash("./testdata"),
pluginName: "not",
},
wantErr: true,
wantErr: true,
wantIsNotExistErr: true,
},
{
name: "plugin file bad name",
args: args{
indexDir: filepath.FromSlash("./testdata"),
pluginName: "wrongname",
},
wantErr: true,
wantErr: true,
wantIsNotExistErr: true,
},
}
for _, tt := range tests {
Expand All @@ -161,6 +166,10 @@ func TestLoadIndexFileFromFS(t *testing.T) {
t.Errorf("LoadIndexFileFromFS() got = %##v,error = %v, wantErr %v", got, err, tt.wantErr)
return
}
if os.IsNotExist(err) != tt.wantIsNotExistErr {
t.Errorf("LoadIndexFileFromFS() got = %##v,error = %v, wantIsNotExistErr %v", got, err, tt.wantErr)
return
}
})
}
}
Expand Down

0 comments on commit 43fb566

Please sign in to comment.