1
0
Fork 0
mirror of https://github.com/Luzifer/named-blacklist.git synced 2024-11-08 07:20:07 +00:00

Make comments sortable for diffing generated files

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-02-15 14:22:35 +01:00
parent 09170e8f8d
commit b565234570
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
6 changed files with 14 additions and 9 deletions

View file

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"sort"
"strings" "strings"
"text/template" "text/template"
@ -86,6 +87,11 @@ func loadConfigFile(filename string) (*configfile, error) {
funcs := korvike.GetFunctionMap() funcs := korvike.GetFunctionMap()
funcs["to_punycode"] = domainToPunycode funcs["to_punycode"] = domainToPunycode
funcs["join"] = strings.Join
funcs["sort"] = func(in []string) []string {
sort.Slice(in, func(i, j int) bool { return strings.ToLower(in[i]) < strings.ToLower(in[j]) })
return in
}
if out.tpl, err = template. if out.tpl, err = template.
New("configTemplate"). New("configTemplate").

View file

@ -114,7 +114,7 @@ template: |
; Blacklist entries ; Blacklist entries
{{ range .blacklist -}} {{ range .blacklist -}}
{{ to_punycode .Domain }} CNAME . ; {{ .Comment }} {{ to_punycode .Domain }} CNAME . ; {{ .Comments }}
{{ end }} {{ end }}
... ...

View file

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"os" "os"
"sort" "sort"
"strings"
"sync" "sync"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -108,7 +107,7 @@ func main() {
func addIfNotExists(entries []entry, e entry) []entry { func addIfNotExists(entries []entry, e entry) []entry {
for i, pe := range entries { for i, pe := range entries {
if pe.Domain == e.Domain { if pe.Domain == e.Domain {
entries[i].Comment = strings.Join([]string{pe.Comment, e.Comment}, ", ") entries[i].Comments = append(pe.Comments, e.Comments...)
return entries return entries
} }
} }

View file

@ -12,8 +12,8 @@ var (
) )
type entry struct { type entry struct {
Domain string Domain string
Comment string Comments []string
} }
type provider interface { type provider interface {

View file

@ -44,8 +44,8 @@ func (p providerdomainList) GetDomainList(d providerDefinition) ([]entry, error)
} }
entries = append(entries, entry{ entries = append(entries, entry{
Domain: domain, Domain: domain,
Comment: d.Name, Comments: []string{d.Name},
}) })
} }

View file

@ -63,8 +63,8 @@ func (p providerHostFile) GetDomainList(d providerDefinition) ([]entry, error) {
} }
entries = append(entries, entry{ entries = append(entries, entry{
Domain: groups[1], Domain: groups[1],
Comment: comment, Comments: []string{comment},
}) })
} }