1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

dead code elimination

This commit is contained in:
Tom Limoncelli
2023-11-12 21:37:11 -05:00
parent d27d8e4f9a
commit c7791dcacd
11 changed files with 25 additions and 188 deletions

View File

@ -118,19 +118,3 @@ func (config *DNSConfig) DomainContainingFQDN(fqdn string) *DomainConfig {
}
return d
}
//// IgnoreName describes an IGNORE_NAME rule.
//type IgnoreName struct {
// Pattern string `json:"pattern"` // Glob pattern.
// Types string `json:"types"` // All caps rtype names, comma separated.
//}
//
//// IgnoreTarget describes an IGNORE_TARGET rule.
//type IgnoreTarget struct {
// Pattern string `json:"pattern"` // Glob pattern.
// Type string `json:"type"` // All caps rtype name.
//}
//
//func (i *IgnoreTarget) String() string {
// return i.Pattern
//}

View File

@ -9,7 +9,7 @@ import (
"github.com/miekg/dns"
)
//// Header Header returns the header of an resource record.
//// Header returns the header of an resource record.
//func (rc *RecordConfig) Header() *dns.RR_Header {
// log.Fatal("Header not implemented")
// return nil

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"log"
"sort"
"strings"
"github.com/StackExchange/dnscontrol/v4/pkg/txtutil"
@ -268,14 +267,6 @@ func (rc *RecordConfig) SetLabel(short, origin string) {
}
}
// UnsafeSetLabelNull sets the label to "". Normally the FQDN is denoted by .Name being
// "@" however this can be used to violate that assertion. It should only be used
// on copies of a RecordConfig that is being used for non-standard things like
// Marshalling yaml.
func (rc *RecordConfig) UnsafeSetLabelNull() {
rc.Name = ""
}
// SetLabelFromFQDN sets the .Name/.NameFQDN fields given a FQDN and origin.
// fqdn may have a trailing "." but it is not required.
// origin may not have a trailing dot.
@ -312,53 +303,17 @@ func (rc *RecordConfig) GetLabelFQDN() string {
return rc.NameFQDN
}
// ToDiffable returns a string that is comparable by a differ.
// extraMaps: a list of maps that should be included in the comparison.
// NB(tlim): This will be deprecated when pkg/diff is replaced by pkg/diff2.
// Use ToComparableNoTTL() instead.
func (rc *RecordConfig) ToDiffable(extraMaps ...map[string]string) string {
var content string
switch rc.Type {
case "SOA":
content = fmt.Sprintf("%s %v %d %d %d %d ttl=%d", rc.target, rc.SoaMbox, rc.SoaRefresh, rc.SoaRetry, rc.SoaExpire, rc.SoaMinttl, rc.TTL)
// SoaSerial is not used in comparison
case "TXT":
//fmt.Fprintf(os.Stdout, "DEBUG: XXXXXXXXXXXXXXXX\n")
t := rc.GetTargetField()
te := txtutil.EncodeQuoted(t)
content = fmt.Sprintf("%v ttl=%d", te, rc.TTL)
default:
content = fmt.Sprintf("%v ttl=%d", rc.GetTargetCombined(), rc.TTL)
}
for _, valueMap := range extraMaps {
// sort the extra values map keys to perform a deterministic
// comparison since Golang maps iteration order is not guaranteed
// FIXME(tlim) The keys of each map is sorted per-map, not across
// all maps. This may be intentional since we'd have no way to
// deal with duplicates.
keys := make([]string, 0)
for k := range valueMap {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := valueMap[k]
content += fmt.Sprintf(" %s=%s", k, v)
}
}
return content
}
// ToComparableNoTTL returns a comparison string. If you need to compare two
// RecordConfigs, you can simply compare the string returned by this function.
// The comparison includes all fields except TTL and any provider-specific
// metafields. Provider-specific metafields like CF_PROXY are not the same as
// pseudo-records like ANAME or R53_ALIAS
// This replaces ToDiff()
func (rc *RecordConfig) ToComparableNoTTL() string {
if rc.Type == "TXT" {
switch rc.Type {
case "SOA":
return fmt.Sprintf("%s %v %d %d %d %d", rc.target, rc.SoaMbox, rc.SoaRefresh, rc.SoaRetry, rc.SoaExpire, rc.SoaMinttl)
// SoaSerial is not included because it isn't used in comparisons.
case "TXT":
//fmt.Fprintf(os.Stdout, "DEBUG: ToComNoTTL raw txts=%s q=%q\n", rc.target, rc.target)
r := txtutil.EncodeQuoted(rc.target)
//fmt.Fprintf(os.Stdout, "DEBUG: ToComNoTTL cmp txts=%s q=%q\n", r, r)
@ -408,7 +363,6 @@ func (rc *RecordConfig) ToRR() dns.RR {
rr.(*dns.DS).Digest = rc.DsDigest
rr.(*dns.DS).KeyTag = rc.DsKeyTag
case dns.TypeLOC:
//this is for records from .js files and read from API
// fmt.Printf("ToRR long: %d, lat:%d, sz: %d, hz:%d, vt:%d\n", rc.LocLongitude, rc.LocLatitude, rc.LocSize, rc.LocHorizPre, rc.LocVertPre)
// fmt.Printf("ToRR rc: %+v\n", *rc)
rr.(*dns.LOC).Version = rc.LocVersion
@ -522,16 +476,6 @@ func (recs Records) HasRecordTypeName(rtype, name string) bool {
return false
}
// FQDNMap returns a map of all LabelFQDNs. Useful for making a
// truthtable of labels that exist in Records.
func (recs Records) FQDNMap() (m map[string]bool) {
m = map[string]bool{}
for _, rec := range recs {
m[rec.GetLabelFQDN()] = true
}
return m
}
// GetByType returns the records that match rtype typeName.
func (recs Records) GetByType(typeName string) Records {
results := Records{}
@ -552,19 +496,6 @@ func (recs Records) GroupedByKey() map[RecordKey]Records {
return groups
}
// GroupedByLabel returns a map of keys to records, and their original key order.
func (recs Records) GroupedByLabel() ([]string, map[string]Records) {
order := []string{}
groups := map[string]Records{}
for _, rec := range recs {
if _, found := groups[rec.Name]; !found {
order = append(order, rec.Name)
}
groups[rec.Name] = append(groups[rec.Name], rec)
}
return order, groups
}
// GroupedByFQDN returns a map of keys to records, grouped by FQDN.
func (recs Records) GroupedByFQDN() ([]string, map[string]Records) {
order := []string{}

View File

@ -1,7 +1,6 @@
package models
import (
"fmt"
"strings"
"github.com/StackExchange/dnscontrol/v4/pkg/txtutil"
@ -35,8 +34,6 @@ func (rc *RecordConfig) HasFormatIdenticalToTXT() bool {
}
// SetTargetTXT sets the TXT fields when there is 1 string.
// The string is stored in .Target, and split into 255-octet chunks
// for .TxtStrings.
func (rc *RecordConfig) SetTargetTXT(s string) error {
if rc.Type == "" {
rc.Type = "TXT"
@ -47,14 +44,12 @@ func (rc *RecordConfig) SetTargetTXT(s string) error {
return rc.SetTarget(s)
}
// SetTargetTXTs sets the TXT fields when there are many strings.
// The individual strings are stored in .TxtStrings, and joined to make .Target.
// SetTargetTXTs sets the TXT fields when there are many strings. They are stored concatenated.
func (rc *RecordConfig) SetTargetTXTs(s []string) error {
return rc.SetTargetTXT(strings.Join(s, ""))
}
// GetTargetTXTJoined returns the TXT target as one string.
// Deprecated: GetTargetTXTJoined is deprecated. Use GetTargetField()
func (rc *RecordConfig) GetTargetTXTJoined() string {
return rc.target
}
@ -63,29 +58,3 @@ func (rc *RecordConfig) GetTargetTXTJoined() string {
func (rc *RecordConfig) GetTargetTXTChunked255() []string {
return txtutil.ToChunks(rc.target)
}
// SetTargetTXTfromRFC1035Quoted parses a series of quoted strings
// and sets .TxtStrings based on the result.
// Note: Most APIs do notThis is rarely used. Try using SetTargetTXT() first.
// Ex:
//
// "foo" << 1 string
// "foo bar" << 1 string
// "foo" "bar" << 2 strings
// foo << error. No quotes! Did you intend to use SetTargetTXT?
//
// Deprecated: each providers should include its own txtParse and txtEscape functions with their own unit tests. No two providers are the same.
func (rc *RecordConfig) SetTargetTXTfromRFC1035Quoted(s string) error {
if s != "" && s[0] != '"' {
// If you get this error, it is likely that you should use
// SetTargetTXT() instead of SetTargetTXTfromRFC1035Quoted().
return fmt.Errorf("non-quoted string used with SetTargetTXTfromRFC1035Quoted: (%s)", s)
}
many, err := ParseQuotedFields(s)
if err != nil {
return err
}
return rc.SetTargetTXTs(many)
}
// There is no GetTargetTXTfromRFC1025Quoted(). Use GetTargetRFC1035Quoted()

View File

@ -74,22 +74,13 @@ func (rc *RecordConfig) zoneFileQuoted() string {
return full[len(header):]
}
// GetTargetRFC1035Quoted returns the target as it would be in an
// RFC1035-style zonefile.
// Do not use this function if RecordConfig might be a pseudo-rtype
// such as R53_ALIAS. Use GetTargetCombined() instead.
func (rc *RecordConfig) GetTargetRFC1035Quoted() string {
return rc.zoneFileQuoted()
}
// GetTargetSortable returns a string that is sortable.
func (rc *RecordConfig) GetTargetSortable() string {
return rc.GetTargetDebug()
}
// GetTargetDebug returns a string with the various fields spelled out.
func (rc *RecordConfig) GetTargetDebug() string {
content := fmt.Sprintf("%s %s %s %d", rc.Type, rc.NameFQDN, rc.target, rc.TTL)
target := rc.target
if rc.Type == "TXT" {
target = fmt.Sprintf("%q", target)
}
content := fmt.Sprintf("%s %s %s %d", rc.Type, rc.NameFQDN, target, rc.TTL)
switch rc.Type { // #rtype_variations
case "A", "AAAA", "AKAMAICDN", "CNAME", "DHCID", "NS", "PTR", "TXT":
// Nothing special.

View File

@ -1,6 +1,8 @@
package diff
import (
"fmt"
"github.com/StackExchange/dnscontrol/v4/models"
"github.com/fatih/color"
)
@ -29,44 +31,13 @@ type differ struct {
// get normalized content for record. target, ttl, mxprio, and specified metadata
func (d *differ) content(r *models.RecordConfig) string {
return r.ToDiffable()
return fmt.Sprintf("%s ttl=%d", r.ToComparableNoTTL(), r.TTL)
}
// ChangesetLess returns true if c[i] < c[j].
func ChangesetLess(c Changeset, i, j int) bool {
var a, b string
// Which fields are we comparing?
// Usually only Desired OR Existing content exists (we're either
// adding or deleting records). In those cases, just use whichever
// isn't nil.
// In the case where both Desired AND Existing exist, it doesn't
// matter which we use as long as we are consistent. I flipped a
// coin and picked to use Desired in that case.
if c[i].Desired != nil {
a = c[i].Desired.NameFQDN
} else {
a = c[i].Existing.NameFQDN
}
if c[j].Desired != nil {
b = c[j].Desired.NameFQDN
} else {
b = c[j].Existing.NameFQDN
}
return a < b
// TODO(tlim): This won't correctly sort:
// []string{"example.com", "foo.example.com", "bar.example.com"}
// A simple way to do that correctly is to split on ".", reverse the
// elements, and sort on the result.
}
// CorrectionLess returns true when comparing corrections.
func CorrectionLess(c []*models.Correction, i, j int) bool {
return c[i].Msg < c[j].Msg
}
// // CorrectionLess returns true when comparing corrections.
// func CorrectionLess(c []*models.Correction, i, j int) bool {
// return c[i].Msg < c[j].Msg
// }
func (c Correlation) String() string {
if c.Existing == nil {

View File

@ -10,15 +10,10 @@ import (
// NewCompat is a constructor that uses the new pkg/diff2 system
// instead of pkg/diff.
//
// It is for backwards compatibility only. New providers should use
// pkg/diff2. Older providers should use this to reduce their
// dependency on pkg/diff2 until they can move to the pkg/diff2/By*()
// functions.
// It is for backwards compatibility only. New providers should use pkg/diff2.
//
// To use this simply change New() to NewCompat(). If that doesn't
// work please report a bug. The only exception is if you depend on
// the extraValues feature, which will not be supported. That
// parameter must be set to nil.
// work please report a bug. The "extraValues" parameter is not supported.
func NewCompat(dc *models.DomainConfig, extraValues ...func(*models.RecordConfig) map[string]string) Differ {
if len(extraValues) != 0 {
panic("extraValues not supported")

View File

@ -569,7 +569,7 @@ func checkCNAMEs(dc *models.DomainConfig) (errs []error) {
func checkDuplicates(records []*models.RecordConfig) (errs []error) {
seen := map[string]*models.RecordConfig{}
for _, r := range records {
diffable := fmt.Sprintf("%s %s %s", r.GetLabelFQDN(), r.Type, r.ToDiffable())
diffable := fmt.Sprintf("%s %s %s", r.GetLabelFQDN(), r.Type, r.ToComparableNoTTL())
if seen[diffable] != nil {
errs = append(errs, fmt.Errorf("exact duplicate record found: %s", diffable))
}

View File

@ -314,17 +314,15 @@ func TestCheckDuplicates(t *testing.T) {
// The only difference is the rType:
makeRC("aaa", "example.com", "uniquestring.com.", models.RecordConfig{Type: "NS"}),
makeRC("aaa", "example.com", "uniquestring.com.", models.RecordConfig{Type: "PTR"}),
// The only difference is the TTL.
makeRC("zzz", "example.com", "4.4.4.4", models.RecordConfig{Type: "A", TTL: 111}),
makeRC("zzz", "example.com", "4.4.4.4", models.RecordConfig{Type: "A", TTL: 222}),
// Three records each with a different target.
makeRC("@", "example.com", "ns1.foo.com.", models.RecordConfig{Type: "NS"}),
makeRC("@", "example.com", "ns2.foo.com.", models.RecordConfig{Type: "NS"}),
makeRC("@", "example.com", "ns3.foo.com.", models.RecordConfig{Type: "NS"}),
// NOTE: The comparison ignores ttl. Therefore we don't test that.
}
errs := checkDuplicates(records)
if len(errs) != 0 {
t.Errorf("Expect duplicate NOT found but found %q", errs)
t.Errorf("Expected duplicate NOT found but found %q", errs)
}
}

View File

@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"sort"
"github.com/StackExchange/dnscontrol/v4/models"
"github.com/StackExchange/dnscontrol/v4/pkg/diff"
@ -234,7 +233,7 @@ func (c *desecProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, exist
// However the code doesn't seem to have such situation. All tests
// pass. That said, if this breaks anything, the easiest fix might
// be to just remove the sort.
sort.Slice(corrections, func(i, j int) bool { return diff.CorrectionLess(corrections, i, j) })
//sort.Slice(corrections, func(i, j int) bool { return diff.CorrectionLess(corrections, i, j) })
return corrections, nil
}

View File

@ -31,7 +31,6 @@ func nativeToRecords(n livedns.DomainRecord, origin string) (rcs []*models.Recor
rc.Type = "ALIAS"
err = rc.SetTarget(value)
case "TXT":
//err = rc.SetTargetTXTfromRFC1035Quoted(value)
t := value
//printer.Printf("DEBUG gandi txt inbounds=%s q=%q\n", t, t)
td, err := txtutil.ParseQuoted(t)