Skip to content

Commit

Permalink
feat: os specific functions and vars
Browse files Browse the repository at this point in the history
  • Loading branch information
juev committed Dec 7, 2023
1 parent 4a397d3 commit 350ecf1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
20 changes: 2 additions & 18 deletions gobrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,6 @@ func (gb *GoBrew) Upgrade(currentVersion string) {
return
}

fileExt := ""
if runtime.GOOS == "windows" {
fileExt = ".exe"
}

mkdirTemp, _ := os.MkdirTemp("", "gobrew")
tmpFile := filepath.Join(mkdirTemp, "gobrew"+fileExt)
url := goBrewDownloadUrl + "gobrew-" + gb.getArch() + fileExt
Expand All @@ -659,12 +654,7 @@ func (gb *GoBrew) Upgrade(currentVersion string) {
}(source)

goBrewFile := filepath.Join(gb.installDir, "bin", "gobrew"+fileExt)
if runtime.GOOS == "windows" {
goBrewOldFile := goBrewFile + ".old"
utils.CheckError(os.Rename(goBrewFile, goBrewOldFile), "==> [Error] Cannot rename binary file")
} else {
utils.CheckError(os.Remove(goBrewFile), "==> [Error] Cannot remove binary file")
}
removeFile(goBrewFile)
destination, err := os.Create(goBrewFile)
utils.CheckError(err, "==> [Error] Cannot open file")
defer func(destination *os.File) {
Expand All @@ -690,13 +680,7 @@ func (gb *GoBrew) getVersionDir(version string) string {
}

func (gb *GoBrew) downloadAndExtract(version string) {
tarName := "go" + version + "." + gb.getArch()

if runtime.GOOS == "windows" {
tarName = tarName + ".zip"
} else {
tarName = tarName + ".tar.gz"
}
tarName := "go" + version + "." + gb.getArch() + tarNameExt

registryPath := defaultRegistryPath
if p := os.Getenv("GOBREW_REGISTRY"); p != "" {
Expand Down
10 changes: 2 additions & 8 deletions gobrew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ func TestUpgrade(t *testing.T) {
binaryDir := filepath.Join(gb.installDir, "bin")
_ = os.MkdirAll(binaryDir, os.ModePerm)

baseName := "gobrew"
if runtime.GOOS == "windows" {
baseName = baseName + ".exe"
}
baseName := "gobrew" + fileExt
binaryFile := filepath.Join(binaryDir, baseName)

if oldFile, err := os.Create(binaryFile); err == nil {
Expand All @@ -187,10 +184,7 @@ func TestDoNotUpgradeLatestVersion(t *testing.T) {
binaryDir := filepath.Join(gb.installDir, "bin")
_ = os.MkdirAll(binaryDir, os.ModePerm)

baseName := "gobrew"
if runtime.GOOS == "windows" {
baseName = baseName + ".exe"
}
baseName := "gobrew" + fileExt
binaryFile := filepath.Join(binaryDir, baseName)

currentVersion := gb.getGobrewVersion()
Expand Down
18 changes: 18 additions & 0 deletions gobrew_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build unix

package gobrew

import (
"os"

"github.com/kevincobain2000/gobrew/utils"
)

const (
fileExt = ""
tarNameExt = ".tar.gz"
)

func removeFile(goBrewFile string) {
utils.CheckError(os.Remove(goBrewFile), "==> [Error] Cannot remove binary file")
}
17 changes: 17 additions & 0 deletions gobrew_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gobrew

import (
"os"

"github.com/kevincobain2000/gobrew/utils"
)

const (
fileExt = ".exe"
tarNameExt = ".zip"
)

func removeFile(goBrewFile string) {
goBrewOldFile := goBrewFile + ".old"
utils.CheckError(os.Rename(goBrewFile, goBrewOldFile), "==> [Error] Cannot rename binary file")
}

0 comments on commit 350ecf1

Please sign in to comment.