isValidGroovyFile checks if a file is a valid, readable Groovy script Filters out binary files, empty files, and files with invalid content
(filePath string)
| 80 | // isValidGroovyFile checks if a file is a valid, readable Groovy script |
| 81 | // Filters out binary files, empty files, and files with invalid content |
| 82 | func isValidGroovyFile(filePath string) bool { |
| 83 | info, err := os.Stat(filePath) |
| 84 | if err != nil { |
| 85 | return false |
| 86 | } |
| 87 | |
| 88 | // Skip empty or very small files (likely invalid) |
| 89 | if info.Size() < 10 { |
| 90 | return false |
| 91 | } |
| 92 | |
| 93 | // Try to read the file |
| 94 | content, err := os.ReadFile(filePath) |
| 95 | if err != nil { |
| 96 | return false |
| 97 | } |
| 98 | |
| 99 | // Check if content is valid UTF-8 and not binary |
| 100 | // Binary files or invalid groovy files will fail this check |
| 101 | for i, b := range content { |
| 102 | // Allow common text characters and control chars (newlines, tabs, etc.) |
| 103 | if b < 9 || (b > 13 && b < 32 && b != 27) || b == 127 { |
| 104 | // Check if it's part of a valid UTF-8 sequence |
| 105 | if !isPartOfUTF8Sequence(content, i) { |
| 106 | return false |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return true |
| 112 | } |
| 113 | |
| 114 | // isPartOfUTF8Sequence checks if a byte at position i is part of a valid UTF-8 sequence |
| 115 | func isPartOfUTF8Sequence(content []byte, i int) bool { |
no test coverage detected