Skip to content

Commit

Permalink
Do not expect specific folder layout in LoadPluginFileFromFS
Browse files Browse the repository at this point in the history
  • Loading branch information
corneliusweig committed Jun 21, 2019
1 parent 52bc435 commit eb428ac
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/krew/cmd/install.go
Expand Up @@ -84,7 +84,7 @@ Remarks:

var install []index.Plugin
for _, name := range pluginNames {
plugin, err := indexscanner.LoadPluginFileFromFS(paths.IndexPath(), name)
plugin, err := indexscanner.LoadPluginFileFromFS(paths.IndexPluginsPath(), name)
if err != nil {
return errors.Wrapf(err, "failed to load plugin %q from the index", name)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/krew/cmd/search.go
Expand Up @@ -41,7 +41,7 @@ Examples:
To fuzzy search plugins with a keyword:
kubectl krew search KEYWORD`,
RunE: func(cmd *cobra.Command, args []string) error {
plugins, err := indexscanner.LoadPluginListFromFS(paths.IndexPath())
plugins, err := indexscanner.LoadPluginListFromFS(paths.IndexPluginsPath())
if err != nil {
return errors.Wrap(err, "failed to load the index")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/krew/cmd/upgrade.go
Expand Up @@ -53,7 +53,7 @@ kubectl krew upgrade foo bar"`,
}

for _, name := range pluginNames {
plugin, err := indexscanner.LoadPluginFileFromFS(paths.IndexPath(), name)
plugin, err := indexscanner.LoadPluginFileFromFS(paths.IndexPluginsPath(), name)
if err != nil {
return errors.Wrapf(err, "failed to load the index file for plugin %s", plugin.Name)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/environment/environment.go
Expand Up @@ -59,6 +59,11 @@ func (p Paths) BasePath() string { return p.base }
// e.g. {IndexPath}/plugins/{plugin}.yaml
func (p Paths) IndexPath() string { return filepath.Join(p.base, "index") }

// IndexPluginsPath returns the plugins directory of the index repository.
//
// e.g. {IndexPath}/plugins/
func (p Paths) IndexPluginsPath() string { return filepath.Join(p.base, "index", "plugins") }

// ReceiptsPath returns the base directory where plugin receipts are stored.
//
// e.g. {ReceiptsPath}/krew-index/{plugin}.yaml
Expand Down
3 changes: 3 additions & 0 deletions pkg/environment/environment_test.go
Expand Up @@ -56,6 +56,9 @@ func TestPaths(t *testing.T) {
if got, expected := p.IndexPath(), filepath.FromSlash("/foo/index"); got != expected {
t.Fatalf("IndexPath()=%s; expected=%s", got, expected)
}
if got, expected := p.IndexPluginsPath(), filepath.FromSlash("/foo/index/plugins"); got != expected {
t.Fatalf("IndexPluginsPath()=%s; expected=%s", got, expected)
}
if got, expected := p.InstallPath(), filepath.FromSlash("/foo/store"); got != expected {
t.Fatalf("InstallPath()=%s; expected=%s", got, expected)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/index/indexscanner/scanner.go
Expand Up @@ -38,7 +38,7 @@ func LoadPluginListFromFS(indexDir string) (index.PluginList, error) {
return indexList, err
}

files, err := ioutil.ReadDir(filepath.Join(indexDir, "plugins"))
files, err := ioutil.ReadDir(indexDir)
if err != nil {
return indexList, errors.Wrap(err, "failed to open index dir")
}
Expand Down Expand Up @@ -66,17 +66,17 @@ func LoadPluginListFromFS(indexDir string) (index.PluginList, error) {

// 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) {
func LoadPluginFileFromFS(pluginsDir, pluginName string) (index.Plugin, error) {
if !index.IsSafePluginName(pluginName) {
return index.Plugin{}, errors.Errorf("plugin name %q not allowed", pluginName)
}

glog.V(4).Infof("Reading plugin %q", pluginName)
indexDir, err := filepath.EvalSymlinks(filepath.Join(indexDir, "plugins"))
pluginsDir, err := filepath.EvalSymlinks(pluginsDir)
if err != nil {
return index.Plugin{}, err
}
p, err := ReadPluginFile(filepath.Join(indexDir, pluginName+constants.ManifestExtension))
p, err := ReadPluginFile(filepath.Join(pluginsDir, pluginName+constants.ManifestExtension))
if os.IsNotExist(err) {
return index.Plugin{}, err
} else if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/index/indexscanner/scanner_test.go
Expand Up @@ -101,7 +101,7 @@ func TestLoadIndexListFromFS(t *testing.T) {
{
name: "load index dir",
args: args{
indexDir: filepath.Join(testdataPath(t), "testindex"),
indexDir: filepath.Join(testdataPath(t), "testindex", "plugins"),
},
},
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestLoadIndexFileFromFS(t *testing.T) {
{
name: "load single index file",
args: args{
indexDir: filepath.Join(testdataPath(t), "testindex"),
indexDir: filepath.Join(testdataPath(t), "testindex", "plugins"),
pluginName: "foo",
},
wantErr: false,
Expand All @@ -143,7 +143,7 @@ func TestLoadIndexFileFromFS(t *testing.T) {
{
name: "plugin file not found",
args: args{
indexDir: filepath.FromSlash("./testdata"),
indexDir: filepath.FromSlash("./testdata/plugins"),
pluginName: "not",
},
wantErr: true,
Expand All @@ -152,7 +152,7 @@ func TestLoadIndexFileFromFS(t *testing.T) {
{
name: "plugin file bad name",
args: args{
indexDir: filepath.FromSlash("./testdata"),
indexDir: filepath.FromSlash("./testdata/plugins"),
pluginName: "wrongname",
},
wantErr: true,
Expand Down
2 changes: 1 addition & 1 deletion pkg/info/info.go
Expand Up @@ -39,5 +39,5 @@ func LoadManifestFromReceiptOrIndex(p environment.Paths, name string) (index.Plu
}

glog.V(3).Infof("Plugin manifest for %q not found in the receipts dir", name)
return indexscanner.LoadPluginFileFromFS(p.IndexPath(), name)
return indexscanner.LoadPluginFileFromFS(p.IndexPluginsPath(), name)
}

0 comments on commit eb428ac

Please sign in to comment.