Skip to content

Commit

Permalink
Merge pull request #111 from tofuutils/feat/skip-non-bin-files-extrac…
Browse files Browse the repository at this point in the history
…tion

feat: skip non-binary files extraction
  • Loading branch information
kvendingoldo authored Apr 28, 2024
2 parents 7c5d1e5 + 0d7a082 commit 6ee0826
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 3 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ bin/terraform-*

# Idea
.idea
.DS_Store
.DS_Store

tenv
terraform
terragrunt
tofu
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ help: ## Display this help.
fmt: ## Run go fmt against code.
go fmt ./...

get: ## Install dependencies.
go get ./...

vet: ## Run go vet against code.
go vet ./...

##@ Build
build: fmt vet ## Build service binary.
build: get fmt ## Build service binary.
go build -o tenv ./cmd/tenv
go build -o tofu ./cmd/tofu
go build -o terraform ./cmd/terraform
go build -o terragrunt ./cmd/terragrunt

run: vet ## Run service from your laptop.
run: ## Run service from your laptop.
go run ./main.go

##@ Test
Expand Down
53 changes: 53 additions & 0 deletions pkg/check/binary/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
*
* Copyright 2024 tofuutils authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package bincheck

import (
"os"
)

func Check(filename string) (bool, error) {
file, err := os.Open(filename)
if err != nil {
return false, err
}
defer file.Close()

const maxBytes = 8000 // Read up to 8000 bytes for analysis
bytes := make([]byte, maxBytes)
numberOfBytes, err := file.Read(bytes)
if err != nil {
return false, err
}

// Check for the presence of a null byte within the read bytes
for i := 0; i < numberOfBytes; i++ {
if bytes[i] == 0 {
return true, nil // Null byte found, file is binary
}
}

// If no null byte found, check for a UTF-8 encoding signature (BOM)
if numberOfBytes >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF {
return false, nil // UTF-8 encoded text file
}

// If no null byte or UTF-8 BOM found, assume it's a text file
return false, nil
}
49 changes: 49 additions & 0 deletions pkg/check/binary/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* Copyright 2024 tofuutils authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package bincheck_test

import (
"testing"

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

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")
}
}
Binary file added pkg/check/binary/testdata/test.bin
Binary file not shown.
1 change: 1 addition & 0 deletions pkg/check/binary/testdata/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is text file for testing
22 changes: 22 additions & 0 deletions pkg/zip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"os"
"path/filepath"
"strings"

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

// ensure the directory exists with a MkdirAll call.
Expand All @@ -45,6 +47,26 @@ func UnzipToDir(dataZip []byte, dirPath string) error {
if err = copyZipFileToDir(file, dirPath); 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
Expand Down

0 comments on commit 6ee0826

Please sign in to comment.