Skip to content

Commit

Permalink
feat: skip non-binary files extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
kvendingoldo committed Apr 28, 2024
1 parent 7c5d1e5 commit 215f29f
Show file tree
Hide file tree
Showing 4 changed files with 81 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
48 changes: 48 additions & 0 deletions pkg/check/binary/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
*
* 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(filePath string) (bool, error) {
file, err := os.Open(filePath)
if err != nil {
return false, err
}
defer file.Close()

buf := make([]byte, 512)
_, err = file.Read(buf)
if err != nil {
return false, err
}

for _, b := range buf {
if b == 0 {
return true, nil
}
if b < 7 || (b > 14 && b < 32) {
return true, nil
}
}

return false, nil
}
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 215f29f

Please sign in to comment.