Skip to content

Commit

Permalink
Fix: Win10 symlink require privilege (#175)
Browse files Browse the repository at this point in the history
* Fix: Win10 symlink require privilege
* Refactor: Move func to specific OS
Fix: Wrong Unix
Fix: Wrong Unix
  • Loading branch information
vunhatchuong committed Feb 9, 2024
1 parent 7c27c22 commit afee1c5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
3 changes: 1 addition & 2 deletions gobrew.go
Expand Up @@ -266,11 +266,10 @@ func (gb *GoBrew) ListRemoteVersions(print bool) map[string][]string {

// CurrentVersion get current version from symb link
func (gb *GoBrew) CurrentVersion() string {
fp, err := filepath.EvalSymlinks(gb.currentBinDir)
fp, err := evalSymlinks(gb.currentBinDir)
if err != nil {
return "None"
}

version := strings.TrimSuffix(fp, filepath.Join("go", "bin"))
version = filepath.Base(version)
if version == "." {
Expand Down
13 changes: 13 additions & 0 deletions gobrew_unix.go
Expand Up @@ -4,6 +4,7 @@ package gobrew

import (
"os"
"path/filepath"

"github.com/kevincobain2000/gobrew/utils"
)
Expand All @@ -16,3 +17,15 @@ const (
func removeFile(goBrewFile string) {
utils.CheckError(os.Remove(goBrewFile), "==> [Error] Cannot remove binary file")
}

func symlink(oldname string, newname string) {
utils.CheckError(os.Symlink(oldname, newname), "==> [Error]: symbolic link failed")
}

func evalSymlinks(path string) (string, error) {
fp, err := filepath.EvalSymlinks(path)
if err != nil {
return "", err
}
return fp, nil
}
23 changes: 23 additions & 0 deletions gobrew_windows.go
@@ -1,7 +1,10 @@
package gobrew

import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/kevincobain2000/gobrew/utils"
)
Expand All @@ -15,3 +18,23 @@ func removeFile(goBrewFile string) {
goBrewOldFile := goBrewFile + ".old"
utils.CheckError(os.Rename(goBrewFile, goBrewOldFile), "==> [Error] Cannot rename binary file")
}

func symlink(oldname string, newname string) {
utils.CheckError(
exec.Command("cmd", "/c", "mklink", "/J", newname, oldname).Run(),
"==> [Error]: symbolic link failed",
)
}

// https://github.com/golang/go/issues/63703
func evalSymlinks(path string) (string, error) {
cmd := fmt.Sprintf(
"Get-Item -Path %s | Select-Object -ExpandProperty Target",
path,
)
output, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), err
}
4 changes: 2 additions & 2 deletions helpers.go
Expand Up @@ -349,13 +349,13 @@ func (gb *GoBrew) downloadAndExtract(version string) {
func (gb *GoBrew) changeSymblinkGoBin(version string) {
goBinDst := filepath.Join(gb.versionsDir, version, "/go/bin")
_ = os.RemoveAll(gb.currentBinDir)
utils.CheckError(os.Symlink(goBinDst, gb.currentBinDir), "==> [Error]: symbolic link failed")
symlink(goBinDst, gb.currentBinDir)
}

func (gb *GoBrew) changeSymblinkGo(version string) {
_ = os.RemoveAll(gb.currentGoDir)
versionGoDir := filepath.Join(gb.versionsDir, version, "go")
utils.CheckError(os.Symlink(versionGoDir, gb.currentGoDir), "==> [Error]: symbolic link failed")
symlink(versionGoDir, gb.currentGoDir)
}

func (gb *GoBrew) getGobrewVersion() string {
Expand Down

0 comments on commit afee1c5

Please sign in to comment.