From b56523457004a4681bdea3940075aad16cbc7296 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 15 Feb 2020 14:22:35 +0100 Subject: [PATCH] Make comments sortable for diffing generated files Signed-off-by: Knut Ahlers --- config.go | 6 ++++++ config.sample.yaml | 2 +- main.go | 3 +-- provider.go | 4 ++-- provider_domainList.go | 4 ++-- provider_hostFile.go | 4 ++-- 6 files changed, 14 insertions(+), 9 deletions(-) diff --git a/config.go b/config.go index f69a76d..ae5862c 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "net/http" "os" + "sort" "strings" "text/template" @@ -86,6 +87,11 @@ func loadConfigFile(filename string) (*configfile, error) { funcs := korvike.GetFunctionMap() 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. New("configTemplate"). diff --git a/config.sample.yaml b/config.sample.yaml index f62bcd9..5709e37 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -114,7 +114,7 @@ template: | ; Blacklist entries {{ range .blacklist -}} - {{ to_punycode .Domain }} CNAME . ; {{ .Comment }} + {{ to_punycode .Domain }} CNAME . ; {{ .Comments }} {{ end }} ... diff --git a/main.go b/main.go index ed766a3..8cc6d45 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "sort" - "strings" "sync" log "github.com/sirupsen/logrus" @@ -108,7 +107,7 @@ func main() { func addIfNotExists(entries []entry, e entry) []entry { for i, pe := range entries { 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 } } diff --git a/provider.go b/provider.go index 938328e..31953f9 100644 --- a/provider.go +++ b/provider.go @@ -12,8 +12,8 @@ var ( ) type entry struct { - Domain string - Comment string + Domain string + Comments []string } type provider interface { diff --git a/provider_domainList.go b/provider_domainList.go index 51f7215..8d69e12 100644 --- a/provider_domainList.go +++ b/provider_domainList.go @@ -44,8 +44,8 @@ func (p providerdomainList) GetDomainList(d providerDefinition) ([]entry, error) } entries = append(entries, entry{ - Domain: domain, - Comment: d.Name, + Domain: domain, + Comments: []string{d.Name}, }) } diff --git a/provider_hostFile.go b/provider_hostFile.go index 20761fa..c28b839 100644 --- a/provider_hostFile.go +++ b/provider_hostFile.go @@ -63,8 +63,8 @@ func (p providerHostFile) GetDomainList(d providerDefinition) ([]entry, error) { } entries = append(entries, entry{ - Domain: groups[1], - Comment: comment, + Domain: groups[1], + Comments: []string{comment}, }) }