Skip to content

Commit

Permalink
handle 1.21 dropping the .0
Browse files Browse the repository at this point in the history
  • Loading branch information
drewgonzales360 committed Aug 9, 2023
1 parent c53423c commit 2b4d348
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ require (
github.com/google/go-github/v48 v48.2.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -21,6 +23,7 @@ require (
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ func defaultDownloadURL(v *semver.Version) string {
func toLooseGoVersion(v *semver.Version) string {
urlVersion := v.String()
// If we have 1.18, we'd parse the version to 1.18.0, but the URL doesn't
// actually inclued the last .0
if v.Patch() == 0 {
// actually inclued the last .0. Starting in Go 1.21, we leave the .0 at the
// end.
if v.Patch() == 0 && v.LessThan(semver.MustParse("1.21.0")){
urlVersion = strings.TrimSuffix(urlVersion, ".0")
}

Expand Down
33 changes: 31 additions & 2 deletions internal/pkg/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"math/rand"
"net/http"
"os"
"path"
"strings"
"time"

Expand Down Expand Up @@ -53,7 +54,7 @@ func randomString() string {
func DownloadFile(v *semver.Version) (filepath string, err error) {
s := spinner.New(spinner.CharSets[38], 200*time.Millisecond)
s.Suffix = fmt.Sprintf(" Downloading Go %s\n", v) // Build our new spinner
s.Start() // Start the spinner
s.Start() // Start the spinner
defer s.Stop()

url, checksum := getDownloadInfo(v)
Expand Down Expand Up @@ -129,6 +130,10 @@ func ExtractTarGz(tarballPath, destinationPath string) error {
s.Start() // Start the spinner
defer s.Stop()

if err := os.MkdirAll(destinationPath, 0755); err != nil {
return fmt.Errorf("could not create %s: %w", destinationPath, err)
}

r, err := os.Open(tarballPath)
if err != nil {
return fmt.Errorf("could not open tarball: %w", err)
Expand Down Expand Up @@ -158,7 +163,7 @@ func ExtractTarGz(tarballPath, destinationPath string) error {
return fmt.Errorf("could not create directory: %w", err)
}
case tar.TypeReg:
outFile, err := os.Create(extractionDestination)
outFile, err := createFile(extractionDestination)
if err != nil {
return fmt.Errorf("could not create file: %w", err)
}
Expand All @@ -184,3 +189,27 @@ func ExtractTarGz(tarballPath, destinationPath string) error {
s.FinalMSG = "✅ Extracted package\n"
return nil
}

// createFile will create all the parent directories for a file path, assuming that the last
// element in the filepath is a file. In Go 1.21, the tarballs no longer have directory elements, so
// extracting the files in 1.21 would fail because intermediary directories were not being created.
func createFile(filepath string) (*os.File, error) {
dir := path.Dir(filepath)
_, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, fmt.Errorf("could not create directory: %w", err)
}
} else {
return nil, fmt.Errorf("could not find %s: %w", dir, err)
}
}

outFile, err := os.Create(filepath)
if err != nil {
return nil, err
}

return outFile, err
}
26 changes: 19 additions & 7 deletions internal/pkg/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,34 @@
package pkg_test

import (
"path"
"testing"

"github.com/Masterminds/semver"
"github.com/stretchr/testify/assert"

"github.com/drewgonzales360/goenv/internal/pkg"
)

const testDir = "/tmp/goenv/"

func TestDownloadAndUntar(t *testing.T) {
version := "1.18"
goVersion := semver.MustParse(version)
tarballPath, err := pkg.DownloadFile(goVersion)
if err != nil {
t.Fatal(err)

testCases := []string{
"1.18",
"1.20.5",
"1.21",
}

if err = pkg.ExtractTarGz(tarballPath, "/tmp/goenv/"+version); err != nil {
t.Fatal(err)
for _, version := range testCases {
goVersion := semver.MustParse(version)
tarballPath, err := pkg.DownloadFile(goVersion)
assert.NoError(t, err)

installDir := path.Join(testDir, goVersion.String())
err = pkg.ExtractTarGz(tarballPath, installDir)
assert.NoError(t, err)

assert.DirExists(t, installDir)
}
}

0 comments on commit 2b4d348

Please sign in to comment.