fix: escape executable path on zsh and bash

resolves #1031
This commit is contained in:
Jan De Dobbeleer 2021-10-10 09:30:56 +02:00 committed by Jan De Dobbeleer
parent f62f6164ac
commit 609a534d73

View file

@ -240,8 +240,24 @@ func main() {
}
}
func initShell(shell, configFile string) string {
func getExecutablePath(shell string) (string, error) {
executable, err := os.Executable()
if err != nil {
return "", err
}
// On Windows, it fails when the excutable is called in MSYS2 for example
// which uses unix style paths to resolve the executable's location.
// PowerShell knows how to resolve both, so we can swap this without any issue.
executable = strings.ReplaceAll(executable, "\\", "/")
switch shell {
case bash, zsh:
return strings.ReplaceAll(executable, " ", "\\ "), nil
}
return executable, nil
}
func initShell(shell, configFile string) string {
executable, err := getExecutablePath(shell)
if err != nil {
return noExe
}
@ -256,11 +272,7 @@ func initShell(shell, configFile string) string {
}
func printShellInit(shell, configFile string) string {
executable, err := os.Executable()
// On Windows, it fails when the excutable is called in MSYS2 for example
// which uses unix style paths to resolve the executable's location.
// PowerShell knows how to resolve both, so we can swap this without any issue.
executable = strings.ReplaceAll(executable, "\\", "/")
executable, err := getExecutablePath(shell)
if err != nil {
return noExe
}