diff --git a/.gitignore b/.gitignore index 43c233ec..fb7d19d9 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,9 @@ bin/terraform-* # Idea .idea -.DS_Store \ No newline at end of file +.DS_Store + +tenv +terraform +terragrunt +tofu \ No newline at end of file diff --git a/Makefile b/Makefile index 627f41ff..0158aca4 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/pkg/check/binary/check.go b/pkg/check/binary/check.go new file mode 100644 index 00000000..a5acf3ec --- /dev/null +++ b/pkg/check/binary/check.go @@ -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 +} diff --git a/pkg/check/binary/check_test.go b/pkg/check/binary/check_test.go new file mode 100644 index 00000000..78be7b4f --- /dev/null +++ b/pkg/check/binary/check_test.go @@ -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") + } +} diff --git a/pkg/check/binary/testdata/test.bin b/pkg/check/binary/testdata/test.bin new file mode 100644 index 00000000..7a42ff11 Binary files /dev/null and b/pkg/check/binary/testdata/test.bin differ diff --git a/pkg/check/binary/testdata/test.txt b/pkg/check/binary/testdata/test.txt new file mode 100644 index 00000000..2c1f75cf --- /dev/null +++ b/pkg/check/binary/testdata/test.txt @@ -0,0 +1 @@ +This is text file for testing diff --git a/pkg/zip/zip.go b/pkg/zip/zip.go index 6bbd7052..40b75c72 100644 --- a/pkg/zip/zip.go +++ b/pkg/zip/zip.go @@ -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. @@ -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