Skip to content

Commit

Permalink
add a filter to pkg/zip to extract only necessary binary (#182)
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Vaumoron <dvaumoron@gmail.com>
  • Loading branch information
dvaumoron authored Jun 22, 2024
1 parent e57de0e commit 843e6bc
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 105 deletions.
51 changes: 0 additions & 51 deletions pkg/check/binary/check.go

This file was deleted.

Binary file removed pkg/check/binary/testdata/test.bin
Binary file not shown.
1 change: 0 additions & 1 deletion pkg/check/binary/testdata/test.txt

This file was deleted.

36 changes: 10 additions & 26 deletions pkg/check/binary/check_test.go → pkg/pathfilter/pathfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,18 @@
*
*/

package bincheck_test
package pathfilter

import (
"testing"
import "strings"

bincheck "github.com/tofuutils/tenv/v2/pkg/check/binary"
)
// Handle '/' and '\' separated path.
func NameEqual(targetName string) func(string) bool {
return func(path string) bool {
separatorIndex := strings.LastIndexByte(path, '/')
if separatorIndex == -1 {
separatorIndex = strings.LastIndexByte(path, '\\')
}

func TestTextFileCheck(t *testing.T) {
t.Parallel()

isBinary, err := bincheck.Check("testdata/test.txt")
if err != nil {
t.Fatalf("Error checking non-binary file: %v", err)
}
if isBinary {
t.Errorf("Expected non-binary file, got binary")
}
}

func TestBinaryFileCheck(t *testing.T) {
t.Parallel()

isBinary, err := bincheck.Check("testdata/test.bin")
if err != nil {
t.Fatalf("Error checking non-binary file: %v", err)
}
if !isBinary {
t.Errorf("Expected binary file, got non-binary")
return path[separatorIndex+1:] == targetName
}
}
32 changes: 7 additions & 25 deletions pkg/zip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ import (
"os"
"path/filepath"
"strings"

bincheck "github.com/tofuutils/tenv/v2/pkg/check/binary"
)

// ensure the directory exists with a MkdirAll call.
func UnzipToDir(dataZip []byte, dirPath string) error {
func UnzipToDir(dataZip []byte, dirPath string, filter func(string) bool) error {
err := os.MkdirAll(dirPath, 0o755)
if err != nil {
return err
Expand All @@ -44,36 +42,16 @@ func UnzipToDir(dataZip []byte, dirPath string) error {
}

for _, file := range zipReader.File {
if err = copyZipFileToDir(file, dirPath); err != nil {
if err = copyZipFileToDir(file, dirPath, filter); err != nil {
return err
}

files, err := os.ReadDir(dirPath)
if err != nil {
return err
}

for _, file := range files {
filePath := filepath.Join(dirPath, file.Name())

isBinary, err := bincheck.Check(filePath)
if err != nil {
return err
}

if !isBinary {
if err = os.Remove(filePath); err != nil {
return err
}
}
}
}

return nil
}

// a separate function allows deferred Close to execute earlier.
func copyZipFileToDir(zipFile *zip.File, dirPath string) error {
func copyZipFileToDir(zipFile *zip.File, dirPath string, filter func(string) bool) error {
destPath, err := sanitizeArchivePath(dirPath, zipFile.Name)
if err != nil {
return err
Expand All @@ -95,6 +73,10 @@ func copyZipFileToDir(zipFile *zip.File, dirPath string) error {
return err
}

if !filter(destPath) {
return nil
}

return os.WriteFile(destPath, data, zipFile.Mode())
}

Expand Down
4 changes: 3 additions & 1 deletion versionmanager/retriever/terraform/terraformretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
pgpcheck "github.com/tofuutils/tenv/v2/pkg/check/pgp"
sha256check "github.com/tofuutils/tenv/v2/pkg/check/sha256"
"github.com/tofuutils/tenv/v2/pkg/download"
"github.com/tofuutils/tenv/v2/pkg/pathfilter"
"github.com/tofuutils/tenv/v2/pkg/winbin"
"github.com/tofuutils/tenv/v2/pkg/zip"
htmlretriever "github.com/tofuutils/tenv/v2/versionmanager/retriever/html"
)
Expand Down Expand Up @@ -130,7 +132,7 @@ func (r TerraformRetriever) InstallRelease(version string, targetPath string) er
return err
}

return zip.UnzipToDir(data, targetPath)
return zip.UnzipToDir(data, targetPath, pathfilter.NameEqual(winbin.GetBinaryName(config.TerraformName)))
}

func (r TerraformRetriever) ListReleases() ([]string, error) {
Expand Down
4 changes: 3 additions & 1 deletion versionmanager/retriever/tofu/tofuretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
sha256check "github.com/tofuutils/tenv/v2/pkg/check/sha256"
"github.com/tofuutils/tenv/v2/pkg/download"
"github.com/tofuutils/tenv/v2/pkg/github"
"github.com/tofuutils/tenv/v2/pkg/pathfilter"
"github.com/tofuutils/tenv/v2/pkg/winbin"
"github.com/tofuutils/tenv/v2/pkg/zip"
htmlretriever "github.com/tofuutils/tenv/v2/versionmanager/retriever/html"
)
Expand Down Expand Up @@ -114,7 +116,7 @@ func (r TofuRetriever) InstallRelease(versionStr string, targetPath string) erro
return err
}

return zip.UnzipToDir(data, targetPath)
return zip.UnzipToDir(data, targetPath, pathfilter.NameEqual(winbin.GetBinaryName(config.TofuName)))
}

func (r TofuRetriever) ListReleases() ([]string, error) {
Expand Down

0 comments on commit 843e6bc

Please sign in to comment.