2022-08-15 06:16:22 +00:00
|
|
|
package redirector
|
2022-01-10 05:11:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"io"
|
|
|
|
"os"
|
2022-04-02 05:21:42 +00:00
|
|
|
"strings"
|
2022-01-10 05:11:15 +00:00
|
|
|
)
|
|
|
|
|
2022-08-14 07:42:49 +00:00
|
|
|
// loadMapFile loads a file as a map
|
|
|
|
func loadMapFile(file string) (map[string]string, error) {
|
2022-01-10 05:11:15 +00:00
|
|
|
f, err := os.Open(file)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
2022-08-14 07:42:49 +00:00
|
|
|
return loadMap(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadMap loads a pipe separated file of mappings
|
|
|
|
func loadMap(f io.Reader) (map[string]string, error) {
|
2022-01-10 05:11:15 +00:00
|
|
|
m := make(map[string]string)
|
|
|
|
|
|
|
|
r := csv.NewReader(f)
|
|
|
|
|
2022-01-11 06:05:20 +00:00
|
|
|
r.Comma = '|'
|
|
|
|
|
2022-01-10 05:11:15 +00:00
|
|
|
for {
|
|
|
|
row, err := r.Read()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-02 05:21:42 +00:00
|
|
|
m[strings.TrimLeft(row[0], "/")] = strings.TrimLeft(row[1], "/")
|
2022-01-10 05:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|