From 43fb56641c1529dc2efa3b94731e4cbef97348c2 Mon Sep 17 00:00:00 2001 From: Kazuki Suda Date: Tue, 7 Aug 2018 02:08:24 +0900 Subject: [PATCH] krew info: Improve error message when plugin doesn't exist (#14) * krew info: Improve error message when plugin doesn't exist * fixup! krew info: Improve error message when plugin doesn't exist --- cmd/krew/cmd/info.go | 4 +++- pkg/index/indexscanner/scanner.go | 14 ++++++++++---- pkg/index/indexscanner/scanner_test.go | 21 +++++++++++++++------ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/cmd/krew/cmd/info.go b/cmd/krew/cmd/info.go index 9b89d12ff9..4f3279e8d7 100644 --- a/cmd/krew/cmd/info.go +++ b/cmd/krew/cmd/info.go @@ -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) diff --git a/pkg/index/indexscanner/scanner.go b/pkg/index/indexscanner/scanner.go index 6d32a15b03..3f44cfcb27 100644 --- a/pkg/index/indexscanner/scanner.go +++ b/pkg/index/indexscanner/scanner.go @@ -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) @@ -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) diff --git a/pkg/index/indexscanner/scanner_test.go b/pkg/index/indexscanner/scanner_test.go index 95234aa01e..a8996af84b 100644 --- a/pkg/index/indexscanner/scanner_test.go +++ b/pkg/index/indexscanner/scanner_test.go @@ -15,6 +15,7 @@ package indexscanner import ( + "os" "path/filepath" "testing" @@ -125,9 +126,10 @@ 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", @@ -135,7 +137,8 @@ func TestLoadIndexFileFromFS(t *testing.T) { indexDir: filepath.Join(testdataPath(t), "testindex"), pluginName: "foo", }, - wantErr: false, + wantErr: false, + wantIsNotExistErr: false, }, { name: "plugin file not found", @@ -143,7 +146,8 @@ func TestLoadIndexFileFromFS(t *testing.T) { indexDir: filepath.FromSlash("./testdata"), pluginName: "not", }, - wantErr: true, + wantErr: true, + wantIsNotExistErr: true, }, { name: "plugin file bad name", @@ -151,7 +155,8 @@ func TestLoadIndexFileFromFS(t *testing.T) { indexDir: filepath.FromSlash("./testdata"), pluginName: "wrongname", }, - wantErr: true, + wantErr: true, + wantIsNotExistErr: true, }, } for _, tt := range tests { @@ -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 + } }) } }