cli-plugins: Simplify addPluginCandidatesFromDir
The returned error is always nil now, so just remove it. Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
parent
6de3d71ab6
commit
fcd94feefb
@ -75,12 +75,12 @@ func getPluginDirs(cfg *configfile.ConfigFile) ([]string, error) {
|
|||||||
return pluginDirs, nil
|
return pluginDirs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func addPluginCandidatesFromDir(res map[string][]string, d string) error {
|
func addPluginCandidatesFromDir(res map[string][]string, d string) {
|
||||||
dentries, err := os.ReadDir(d)
|
dentries, err := os.ReadDir(d)
|
||||||
// Silently ignore any directories which we cannot list (e.g. due to
|
// Silently ignore any directories which we cannot list (e.g. due to
|
||||||
// permissions or anything else) or which is not a directory
|
// permissions or anything else) or which is not a directory
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
for _, dentry := range dentries {
|
for _, dentry := range dentries {
|
||||||
switch dentry.Type() & os.ModeType {
|
switch dentry.Type() & os.ModeType {
|
||||||
@ -101,22 +101,15 @@ func addPluginCandidatesFromDir(res map[string][]string, d string) error {
|
|||||||
}
|
}
|
||||||
res[name] = append(res[name], filepath.Join(d, dentry.Name()))
|
res[name] = append(res[name], filepath.Join(d, dentry.Name()))
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// listPluginCandidates returns a map from plugin name to the list of (unvalidated) Candidates. The list is in descending order of priority.
|
// listPluginCandidates returns a map from plugin name to the list of (unvalidated) Candidates. The list is in descending order of priority.
|
||||||
func listPluginCandidates(dirs []string) (map[string][]string, error) {
|
func listPluginCandidates(dirs []string) map[string][]string {
|
||||||
result := make(map[string][]string)
|
result := make(map[string][]string)
|
||||||
for _, d := range dirs {
|
for _, d := range dirs {
|
||||||
if err := addPluginCandidatesFromDir(result, d); err != nil {
|
addPluginCandidatesFromDir(result, d)
|
||||||
// Silently ignore paths which don't exist.
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return nil, err // Or return partial result?
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result, nil
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPlugin returns a plugin on the system by its name
|
// GetPlugin returns a plugin on the system by its name
|
||||||
@ -126,11 +119,7 @@ func GetPlugin(name string, dockerCli command.Cli, rootcmd *cobra.Command) (*Plu
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
candidates, err := listPluginCandidates(pluginDirs)
|
candidates := listPluginCandidates(pluginDirs)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if paths, ok := candidates[name]; ok {
|
if paths, ok := candidates[name]; ok {
|
||||||
if len(paths) == 0 {
|
if len(paths) == 0 {
|
||||||
return nil, errPluginNotFound(name)
|
return nil, errPluginNotFound(name)
|
||||||
@ -156,10 +145,7 @@ func ListPlugins(dockerCli command.Cli, rootcmd *cobra.Command) ([]Plugin, error
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
candidates, err := listPluginCandidates(pluginDirs)
|
candidates := listPluginCandidates(pluginDirs)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var plugins []Plugin
|
var plugins []Plugin
|
||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
|
@ -51,8 +51,7 @@ func TestListPluginCandidates(t *testing.T) {
|
|||||||
dirs = append(dirs, dir.Join(d))
|
dirs = append(dirs, dir.Join(d))
|
||||||
}
|
}
|
||||||
|
|
||||||
candidates, err := listPluginCandidates(dirs)
|
candidates := listPluginCandidates(dirs)
|
||||||
assert.NilError(t, err)
|
|
||||||
exp := map[string][]string{
|
exp := map[string][]string{
|
||||||
"plugin1": {
|
"plugin1": {
|
||||||
dir.Join("plugins1", "docker-plugin1"),
|
dir.Join("plugins1", "docker-plugin1"),
|
||||||
@ -94,11 +93,10 @@ func TestListPluginCandidatesInaccesibleDir(t *testing.T) {
|
|||||||
)
|
)
|
||||||
defer dir.Remove()
|
defer dir.Remove()
|
||||||
|
|
||||||
candidates, err := listPluginCandidates([]string{
|
candidates := listPluginCandidates([]string{
|
||||||
dir.Join("no-perm"),
|
dir.Join("no-perm"),
|
||||||
dir.Join("plugins"),
|
dir.Join("plugins"),
|
||||||
})
|
})
|
||||||
assert.NilError(t, err)
|
|
||||||
assert.DeepEqual(t, candidates, map[string][]string{
|
assert.DeepEqual(t, candidates, map[string][]string{
|
||||||
"buildx": {
|
"buildx": {
|
||||||
dir.Join("plugins", "docker-buildx"),
|
dir.Join("plugins", "docker-buildx"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user