Refactor code which deals with Windows' `.exe` suffix

Signed-off-by: Ian Campbell <ijc@docker.com>
This commit is contained in:
Ian Campbell 2019-01-14 17:53:19 +00:00
parent 609dcb9152
commit 63f3ad181b
4 changed files with 37 additions and 19 deletions

View File

@ -5,7 +5,6 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/docker/cli/cli/command"
@ -61,12 +60,9 @@ func addPluginCandidatesFromDir(res map[string][]string, d string) error {
continue
}
name = strings.TrimPrefix(name, NamePrefix)
if runtime.GOOS == "windows" {
exe := ".exe"
if !strings.HasSuffix(name, exe) {
continue
}
name = strings.TrimSuffix(name, exe)
var err error
if name, err = trimExeSuffix(name); err != nil {
continue
}
res[name] = append(res[name], filepath.Join(d, dentry.Name()))
}
@ -131,10 +127,7 @@ func PluginRunCommand(dockerCli command.Cli, name string, rootcmd *cobra.Command
// fallback to their "invalid" command path.
return nil, errPluginNotFound(name)
}
exename := NamePrefix + name
if runtime.GOOS == "windows" {
exename = exename + ".exe"
}
exename := addExeSuffix(NamePrefix + name)
for _, d := range getPluginDirs(dockerCli) {
path := filepath.Join(d, exename)

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/pkg/errors"
@ -46,15 +45,12 @@ func newPlugin(c Candidate, rootcmd *cobra.Command) (Plugin, error) {
if fullname == "." {
return Plugin{}, errors.Errorf("unable to determine basename of plugin candidate %q", path)
}
if runtime.GOOS == "windows" {
exe := ".exe"
if !strings.HasSuffix(fullname, exe) {
return Plugin{}, errors.Errorf("plugin candidate %q lacks required %q suffix", path, exe)
}
fullname = strings.TrimSuffix(fullname, exe)
var err error
if fullname, err = trimExeSuffix(fullname); err != nil {
return Plugin{}, errors.Wrapf(err, "plugin candidate %q", path)
}
if !strings.HasPrefix(fullname, NamePrefix) {
return Plugin{}, errors.Errorf("plugin candidate %q does not have %q prefix", path, NamePrefix)
return Plugin{}, errors.Errorf("plugin candidate %q: does not have %q prefix", path, NamePrefix)
}
p := Plugin{

View File

@ -0,0 +1,10 @@
// +build !windows
package manager
func trimExeSuffix(s string) (string, error) {
return s, nil
}
func addExeSuffix(s string) string {
return s
}

View File

@ -0,0 +1,19 @@
package manager
import (
"strings"
"github.com/pkg/errors"
)
func trimExeSuffix(s string) (string, error) {
exe := ".exe"
if !strings.HasSuffix(s, exe) {
return "", errors.Errorf("lacks required %q suffix", exe)
}
return strings.TrimSuffix(s, exe), nil
}
func addExeSuffix(s string) string {
return s + ".exe"
}