commit
This commit is contained in:
parent
2723480488
commit
f0873e78d6
1 changed files with 46 additions and 0 deletions
46
main.go
46
main.go
|
@ -49,6 +49,11 @@ func loadBlockedWords(path string) {
|
|||
redaction = strings.TrimSpace(strings.TrimPrefix(line, "redaction:"))
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(line, "import:") {
|
||||
dirPath := strings.TrimSpace(strings.TrimPrefix(line, "import:"))
|
||||
expandAndImportDir(dirPath)
|
||||
continue
|
||||
}
|
||||
blockedWords = append(blockedWords, regexp.MustCompile(regexp.QuoteMeta(line)))
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
|
@ -56,6 +61,39 @@ func loadBlockedWords(path string) {
|
|||
}
|
||||
}
|
||||
|
||||
func expandAndImportDir(dir string) {
|
||||
expandedPath := os.ExpandEnv(strings.ReplaceAll(dir, "~", getHomeDir()))
|
||||
err := filepath.Walk(expandedPath, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
importWordsFromFile(path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Warning: Failed to walk directory %s: %v", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
func importWordsFromFile(path string) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
log.Printf("Warning: Cannot open import file %s: %v", path, err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line != "" && !strings.HasPrefix(line, "#") {
|
||||
blockedWords = append(blockedWords, regexp.MustCompile(regexp.QuoteMeta(line)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func censorLine(line string) string {
|
||||
for _, re := range blockedWords {
|
||||
line = re.ReplaceAllString(line, redaction)
|
||||
|
@ -130,3 +168,11 @@ func isInputFromTerminal() bool {
|
|||
return (fileInfo.Mode() & os.ModeCharDevice) != 0
|
||||
}
|
||||
|
||||
func getHomeDir() string {
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to determine home directory: %v", err)
|
||||
}
|
||||
return usr.HomeDir
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue