Skip to content

Commit

Permalink
fix: add .exe suffix on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane committed Apr 27, 2024
1 parent 9bce9e3 commit 2dab9b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
21 changes: 11 additions & 10 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (a *App) Use(ctx context.Context, version string) error {
fmt.Fprintf(a.Output, "%s is already in use\n", version)
return nil
case local.main:
if err := a.GoBin.Remove("go"); err != nil {
if err := a.GoBin.Remove("go" + exe()); err != nil {
return err
}
fmt.Fprintf(a.Output, "Switched to %s (main)\n", version)
Expand All @@ -59,7 +59,7 @@ func (a *App) Use(ctx context.Context, version string) error {
initial = true
fmt.Fprintf(a.Output, "%s is not installed. Looking for it on go.dev ...\n", version)
url := fmt.Sprintf("golang.org/dl/go%s@latest", version)
if err := a.RunCmd(ctx, "go", "install", url); err != nil {
if err := a.RunCmd(ctx, "go"+exe(), "install", url); err != nil {
return err
}
}
Expand All @@ -71,15 +71,15 @@ func (a *App) Use(ctx context.Context, version string) error {
// this message doesn't make sense during initial installation.
fmt.Fprintf(a.Output, "%s SDK is missing. Starting download ...\n", version)
}
if err := a.RunCmd(ctx, "go"+version, "download"); err != nil {
if err := a.RunCmd(ctx, "go"+version+exe(), "download"); err != nil {
return err
}
}

if err := a.GoBin.Remove("go"); err != nil && !errors.Is(err, fs.ErrNotExist) {
if err := a.GoBin.Remove("go" + exe()); err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
if err := a.GoBin.Symlink("go"+version, "go"); err != nil {
if err := a.GoBin.Symlink("go"+version+exe(), "go"+exe()); err != nil {
return err
}

Expand Down Expand Up @@ -158,13 +158,13 @@ func (a *App) Remove(ctx context.Context, version string) error {
case local.main:
return fmt.Errorf("unable to remove %s (main)", version)
case local.current:
if err := a.GoBin.Remove("go"); err != nil {
if err := a.GoBin.Remove("go" + exe()); err != nil {
return err
}
fmt.Fprintf(a.Output, "Switched to %s (main)\n", local.main)
}

if err := a.GoBin.Remove("go" + version); err != nil {
if err := a.GoBin.Remove("go" + version + exe()); err != nil {
return err
}
if err := a.SDK.RemoveAll("go" + version); err != nil {
Expand Down Expand Up @@ -200,7 +200,7 @@ func (a *App) localVersions(ctx context.Context) (*local, error) {
tempPath := cutFromPath(currPath, os.Getenv("GOBIN"))
os.Setenv("PATH", tempPath)

output, err := a.RunCmdOut(ctx, "go", "version")
output, err := a.RunCmdOut(ctx, "go"+exe(), "version")
if err != nil {
return nil, err
}
Expand All @@ -211,11 +211,11 @@ func (a *App) localVersions(ctx context.Context) (*local, error) {
}

var current string
switch link, err := a.GoBin.Readlink("go"); {
switch link, err := a.GoBin.Readlink("go" + exe()); {
case errors.Is(err, fs.ErrNotExist):
current = main
case err == nil:
current = strings.TrimPrefix(filepath.Base(link), "go")
current = strings.TrimPrefix(filepath.Base(link), "go") // TODO: windows: trim .exe?
default:
return nil, err
}
Expand All @@ -230,6 +230,7 @@ func (a *App) localVersions(ctx context.Context) (*local, error) {
if entry.IsDir() {
continue
}
// TODO: windows: trim .exe?
version := strings.TrimPrefix(entry.Name(), "go")
if isValid(version) {
list = append(list, version)
Expand Down
8 changes: 8 additions & 0 deletions app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
goversion "go/version"
"os"
"runtime"
"slices"
"sort"
"strconv"
Expand All @@ -13,6 +14,13 @@ func isValid(version string) bool {
return goversion.IsValid("go"+version) || version == "tip"
}

func exe() string {
if runtime.GOOS == "windows" {
return ".exe"
}
return ""
}

func cutFromPath(path, value string) string {
oldPath := strings.Split(path, string(os.PathListSeparator))
newPath := slices.DeleteFunc(oldPath, func(v string) bool {
Expand Down

0 comments on commit 2dab9b5

Please sign in to comment.