-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to ignore files in the directory listing, ref #149
- Loading branch information
Showing
2 changed files
with
77 additions
and
8 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
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,43 @@ | ||
package engine | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// ReadIgnoreFile reads the ignore patterns from the ignore.txt file. | ||
func ReadIgnoreFile(ignoreFilePath string) ([]string, error) { | ||
var patterns []string | ||
file, err := os.Open(ignoreFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := strings.TrimSpace(scanner.Text()) | ||
if line != "" && !strings.HasPrefix(line, "#") { | ||
patterns = append(patterns, line) | ||
} | ||
} | ||
return patterns, scanner.Err() | ||
} | ||
|
||
// ShouldIgnore checks if a given filename should be ignored based on the provided patterns. | ||
// A warning will be printed if there are errors with an ignore pattern. | ||
func ShouldIgnore(filename string, patterns []string) bool { | ||
for _, pattern := range patterns { | ||
matched, err := filepath.Match(pattern, filename) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "warning: %s contains an unmatchable pattern %s: %v\n", filename, pattern, err) | ||
continue | ||
} | ||
if matched { | ||
return true | ||
} | ||
} | ||
return false | ||
} |