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

View File

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