Check for `.exe` case insensitively

On Windows `foo.exe`, `foo.eXe` and `foo.EXE` are equally executable.

Signed-off-by: Ian Campbell <ijc@docker.com>
This commit is contained in:
Ian Campbell 2019-01-28 17:50:05 +00:00
parent 63f3ad181b
commit 1337895751
1 changed files with 11 additions and 4 deletions

View File

@ -1,17 +1,24 @@
package manager package manager
import ( import (
"path/filepath"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// This is made slightly more complex due to needing to be case insensitive.
func trimExeSuffix(s string) (string, error) { func trimExeSuffix(s string) (string, error) {
exe := ".exe" ext := filepath.Ext(s)
if !strings.HasSuffix(s, exe) { if ext == "" {
return "", errors.Errorf("lacks required %q suffix", exe) return "", errors.Errorf("path %q lacks required file extension", s)
} }
return strings.TrimSuffix(s, exe), nil
exe := ".exe"
if !strings.EqualFold(ext, exe) {
return "", errors.Errorf("path %q lacks required %q suffix", s, exe)
}
return strings.TrimSuffix(s, ext), nil
} }
func addExeSuffix(s string) string { func addExeSuffix(s string) string {