-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
38 lines (31 loc) · 897 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"bufio"
"os"
"strings"
)
func main() {
// Read masks from config file
masks := loadConfig(defaultConfigPath())
// Read from stdin, mask words and write to stdout and stderr
stdin := bufio.NewScanner(os.Stdin)
stdout := bufio.NewWriter(os.Stdout)
for stdin.Scan() {
if _, err := stdout.WriteString(maskLine(stdin.Text(), masks) + "\n"); err != nil {
bail(err.Error())
}
}
if err := stdout.Flush(); err != nil {
bail(err.Error())
}
}
// maskLine replaces substrings in the line with their masks from the config
func maskLine(line string, masks map[string]string) string {
// Ensure everything is lowercased
line = strings.ToLower(line)
// Iterate through the masks and replace all instances of each key with its value
for word, replacement := range masks {
line = strings.ReplaceAll(line, word, strings.ToLower(replacement))
}
return line
}