terraform-provider-gitea/vendor/github.com/hashicorp/hc-install/internal/build/install_go_version.go
dependabot[bot] 84c9110a24
Bump github.com/hashicorp/terraform-plugin-sdk/v2 from 2.24.1 to 2.26.0
Bumps [github.com/hashicorp/terraform-plugin-sdk/v2](https://github.com/hashicorp/terraform-plugin-sdk) from 2.24.1 to 2.26.0.
- [Release notes](https://github.com/hashicorp/terraform-plugin-sdk/releases)
- [Changelog](https://github.com/hashicorp/terraform-plugin-sdk/blob/main/CHANGELOG.md)
- [Commits](https://github.com/hashicorp/terraform-plugin-sdk/compare/v2.24.1...v2.26.0)

---
updated-dependencies:
- dependency-name: github.com/hashicorp/terraform-plugin-sdk/v2
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-03-20 20:25:53 +00:00

66 lines
1.7 KiB
Go

package build
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"github.com/hashicorp/go-version"
)
// installGoVersion installs given version of Go using Go
// according to https://golang.org/doc/manage-install
func (gb *GoBuild) installGoVersion(ctx context.Context, v *version.Version) (Go, error) {
versionString := v.Core().String()
// trim 0 patch versions as that's how Go does it :shrug:
shortVersion := strings.TrimSuffix(versionString, ".0")
pkgURL := fmt.Sprintf("golang.org/dl/go%s", shortVersion)
gb.log().Printf("go getting %q", pkgURL)
cmd := exec.CommandContext(ctx, "go", "get", pkgURL)
out, err := cmd.CombinedOutput()
if err != nil {
return Go{}, fmt.Errorf("unable to get Go %s: %w\n%s", v, err, out)
}
gb.log().Printf("go installing %q", pkgURL)
cmd = exec.CommandContext(ctx, "go", "install", pkgURL)
out, err = cmd.CombinedOutput()
if err != nil {
return Go{}, fmt.Errorf("unable to install Go %s: %w\n%s", v, err, out)
}
cmdName := fmt.Sprintf("go%s", shortVersion)
gb.log().Printf("downloading go %q", v)
cmd = exec.CommandContext(ctx, cmdName, "download")
out, err = cmd.CombinedOutput()
if err != nil {
return Go{}, fmt.Errorf("unable to download Go %s: %w\n%s", v, err, out)
}
gb.log().Printf("download of go %q finished", v)
cleanupFunc := func(ctx context.Context) {
cmd = exec.CommandContext(ctx, cmdName, "env", "GOROOT")
out, err = cmd.CombinedOutput()
if err != nil {
return
}
rootPath := strings.TrimSpace(string(out))
// run some extra checks before deleting, just to be sure
if rootPath != "" && strings.HasSuffix(rootPath, v.String()) {
os.RemoveAll(rootPath)
}
}
return Go{
Cmd: cmdName,
CleanupFunc: cleanupFunc,
Version: v,
}, nil
}