forked from drew/smtprelay
feat: add alias file support
This commit is contained in:
committed by
Bernhard Fröhlich
parent
6e9c00e171
commit
7b6be95125
52
aliases.go
Normal file
52
aliases.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AliasMap map[string]string
|
||||
|
||||
func AliasLoadFile(file string) (AliasMap, error) {
|
||||
aliasMap := make(AliasMap)
|
||||
count := 0
|
||||
log.Info().
|
||||
Str("file", file).
|
||||
Msg("Loading aliases file")
|
||||
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
log.Fatal().
|
||||
Str("file", file).
|
||||
Err(err).
|
||||
Msg("cannot load aliases file")
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
parts := strings.Fields(line)
|
||||
if len(parts) >= 2 {
|
||||
aliasMap[parts[0]] = parts[1]
|
||||
count++
|
||||
}
|
||||
}
|
||||
log.Info().
|
||||
Str("file", file).
|
||||
Msg(fmt.Sprintf("Loaded %d aliases from file", count))
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Fatal().
|
||||
Str("file", file).
|
||||
Err(err).
|
||||
Msg("cannot load aliases file")
|
||||
}
|
||||
return aliasMap, nil
|
||||
}
|
||||
Reference in New Issue
Block a user