From ccd7d8e231bc0f99d4f8d191abb75948d96e9fc6 Mon Sep 17 00:00:00 2001 From: Abhay Krishna Arunachalam Date: Tue, 5 Mar 2024 21:35:47 -0800 Subject: [PATCH] Extract only the Go binary from GitHub release tarball --- .../version-tracker/pkg/ecrpublic/ecrpublic.go | 3 +-- tools/version-tracker/pkg/github/github.go | 10 +++++----- tools/version-tracker/pkg/util/tar/tar.go | 18 +++++++++--------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/tools/version-tracker/pkg/ecrpublic/ecrpublic.go b/tools/version-tracker/pkg/ecrpublic/ecrpublic.go index 1f6e3b4e01..8ebb54e444 100644 --- a/tools/version-tracker/pkg/ecrpublic/ecrpublic.go +++ b/tools/version-tracker/pkg/ecrpublic/ecrpublic.go @@ -1,11 +1,10 @@ package ecrpublic import ( + "encoding/json" "fmt" "os/exec" "strings" - //"regexp" - "encoding/json" "github.com/aws/eks-anywhere/pkg/semver" diff --git a/tools/version-tracker/pkg/github/github.go b/tools/version-tracker/pkg/github/github.go index 369c037dc1..32e8ce775c 100644 --- a/tools/version-tracker/pkg/github/github.go +++ b/tools/version-tracker/pkg/github/github.go @@ -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 { diff --git a/tools/version-tracker/pkg/util/tar/tar.go b/tools/version-tracker/pkg/util/tar/tar.go index b007f99790..c37ced3865 100644 --- a/tools/version-tracker/pkg/util/tar/tar.go +++ b/tools/version-tracker/pkg/util/tar/tar.go @@ -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 @@ -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) @@ -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 {