mirror of
https://github.com/therootcompany/golib.git
synced 2026-11-04 06:55:00 +00:00
feat(iplist): accept "source" as header name in addition to "network"
Some deployments used "source,comment" as the CSV header for IP list files, but the parser only recognized "network" as a header row. Accept both "network" and "source" (case-insensitive) to avoid treating the header as a (non-existent) domain entry.
This commit is contained in:
parent
ca6e271f66
commit
c6da1e7e4a
@ -228,8 +228,9 @@ func readSource(ctx context.Context, source, cacheDir string, seen map[string]st
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse reads TSV or CSV data from r and returns the first column of each
|
// Parse reads TSV or CSV data from r and returns the first column of each
|
||||||
// row. Blank lines, comments (lines starting with #), and an optional
|
// row. Blank lines, comments (lines starting with #), and an optional header
|
||||||
// "network" header on the first line are ignored. Other columns are
|
// on the first line are ignored. The header is recognized when the first
|
||||||
|
// column is "network" or "source" (case-insensitive). Other columns are
|
||||||
// treated as labels and discarded.
|
// treated as labels and discarded.
|
||||||
//
|
//
|
||||||
// The format is auto-detected: if the first non-empty, non-comment line
|
// The format is auto-detected: if the first non-empty, non-comment line
|
||||||
@ -258,13 +259,17 @@ func isTSV(data []byte) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isHeader(value string) bool {
|
||||||
|
return strings.EqualFold(value, "network") || strings.EqualFold(value, "source")
|
||||||
|
}
|
||||||
|
|
||||||
func parseTSV(r io.Reader) ([]string, error) {
|
func parseTSV(r io.Reader) ([]string, error) {
|
||||||
scanner := bufio.NewScanner(r)
|
scanner := bufio.NewScanner(r)
|
||||||
entries := make([]string, 0)
|
entries := make([]string, 0)
|
||||||
for line := 1; scanner.Scan(); line++ {
|
for line := 1; scanner.Scan(); line++ {
|
||||||
col, _, _ := strings.Cut(scanner.Text(), "\t")
|
col, _, _ := strings.Cut(scanner.Text(), "\t")
|
||||||
value := strings.TrimSpace(col)
|
value := strings.TrimSpace(col)
|
||||||
if value == "" || strings.HasPrefix(value, "#") || (line == 1 && strings.EqualFold(value, "network")) {
|
if value == "" || strings.HasPrefix(value, "#") || (line == 1 && isHeader(value)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
entries = append(entries, value)
|
entries = append(entries, value)
|
||||||
@ -289,7 +294,7 @@ func parseCSV(r io.Reader) ([]string, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
value := strings.TrimSpace(record[0])
|
value := strings.TrimSpace(record[0])
|
||||||
if value == "" || (i == 0 && strings.EqualFold(value, "network")) {
|
if value == "" || (i == 0 && isHeader(value)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
entries = append(entries, value)
|
entries = append(entries, value)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user