-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from tofuutils/feat/skip-non-bin-files-extrac…
…tion feat: skip non-binary files extraction
- Loading branch information
Showing
7 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,9 @@ bin/terraform-* | |
|
||
# Idea | ||
.idea | ||
.DS_Store | ||
.DS_Store | ||
|
||
tenv | ||
terraform | ||
terragrunt | ||
tofu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is text file for testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters