Skip to content

Commit

Permalink
Extract only the Go binary from GitHub release tarball
Browse files Browse the repository at this point in the history
  • Loading branch information
abhay-krishna committed Mar 6, 2024
1 parent 3e8e125 commit ccd7d8e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
3 changes: 1 addition & 2 deletions tools/version-tracker/pkg/ecrpublic/ecrpublic.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package ecrpublic

import (
"encoding/json"
"fmt"
"os/exec"
"strings"
//"regexp"
"encoding/json"

"github.com/aws/eks-anywhere/pkg/semver"

Expand Down
10 changes: 5 additions & 5 deletions tools/version-tracker/pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,22 @@ func GetGoVersionForLatestRevision(client *github.Client, org, repo, latestRevis
return "", fmt.Errorf("downloading release tarball from URL [%s]: %v", tarballUrl, err)
}

binaryName := projectReleaseAsset.BinaryName
if strings.Count(binaryName, "%s") > 0 {
binaryName = fmt.Sprintf(binaryName, assetVersionReplacement)
}
if projectReleaseAsset.Extract {
tarballFile, err := os.Open(tarballFilePath)
if err != nil {
return "", fmt.Errorf("opening tarball filepath: %v", err)
}

err = tar.ExtractTarGz(tarballDownloadPath, tarballFile)
err = tar.ExtractFileFromTarball(tarballDownloadPath, tarballFile, binaryName)
if err != nil {
return "", fmt.Errorf("extracting tarball file: %v", err)
}
}

binaryName := projectReleaseAsset.BinaryName
if strings.Count(binaryName, "%s") > 0 {
binaryName = fmt.Sprintf(binaryName, assetVersionReplacement)
}
binaryFilePath := filepath.Join(tarballDownloadPath, binaryName)
goVersion, err = version.GetGoVersion(binaryFilePath)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions tools/version-tracker/pkg/util/tar/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"io"
"os"
"path/filepath"
"strings"
)

// ExtractTarGz extracts the contents of the given tarball.
func ExtractTarGz(tarballDownloadPath string, gzipStream io.Reader) error {
// ExtractFileFromTarball extracts the specified file from the given tarball.
func ExtractFileFromTarball(tarballDownloadPath string, gzipStream io.Reader, targetFile string) error {
uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
return err
Expand All @@ -19,12 +20,13 @@ func ExtractTarGz(tarballDownloadPath string, gzipStream io.Reader) error {
tarReader := tar.NewReader(uncompressedStream)
var header *tar.Header
for header, err = tarReader.Next(); err == nil; header, err = tarReader.Next() {
switch header.Typeflag {
case tar.TypeDir:
if err := os.Mkdir(filepath.Join(tarballDownloadPath, header.Name), 0o755); err != nil {
return fmt.Errorf("creating directory from archive: %v", err)
if header.Name == targetFile {
if strings.Contains(header.Name, "/") {
err = os.MkdirAll(filepath.Join(tarballDownloadPath, filepath.Dir(header.Name)), 0o755)
if err != nil {
return fmt.Errorf("creating parent directory for archive contents: %v", err)
}
}
case tar.TypeReg:
outFile, err := os.Create(filepath.Join(tarballDownloadPath, header.Name))
if err != nil {
return fmt.Errorf("creating file from archive: %v", err)
Expand All @@ -37,8 +39,6 @@ func ExtractTarGz(tarballDownloadPath string, gzipStream io.Reader) error {
if err := outFile.Close(); err != nil {
return fmt.Errorf("closing output destination file descriptor: %v", err)
}
default:
return fmt.Errorf("unknown type in tar header: %b in %s", header.Typeflag, header.Name)
}
}
if err != io.EOF {
Expand Down

0 comments on commit ccd7d8e

Please sign in to comment.