From f204247c2d58f5c019a0b0f2a1cb39279e29de7b Mon Sep 17 00:00:00 2001 From: Tom Limoncelli Date: Sat, 18 Nov 2023 09:28:07 -0500 Subject: [PATCH] fixup! --- commands/gz_test.go | 1 + models/t_parse.go | 104 ++++++++++++++++++++++---------------------- 2 files changed, 53 insertions(+), 52 deletions(-) diff --git a/commands/gz_test.go b/commands/gz_test.go index 4aa50e560..594debb83 100644 --- a/commands/gz_test.go +++ b/commands/gz_test.go @@ -67,6 +67,7 @@ func testFormat(t *testing.T, domain, format string) { } if w, g := string(want), string(got); w != g { + // If the test fails, output a file showing "got" err = os.WriteFile(expectedFilename+".ACTUAL", got, 0644) if err != nil { log.Fatal(err) diff --git a/models/t_parse.go b/models/t_parse.go index d38b3c5c4..c9ff682bd 100644 --- a/models/t_parse.go +++ b/models/t_parse.go @@ -5,6 +5,58 @@ import ( "net" ) +func (rc *RecordConfig) PopulateFromStringFunc(rtype, contents, origin string, txtFn func(s string) string) error { + if rc.Type != "" && rc.Type != rtype { + return fmt.Errorf("assertion failed: rtype already set (%s) (%s)", rtype, rc.Type) + } + + switch rc.Type = rtype; rtype { // #rtype_variations + case "A": + ip := net.ParseIP(contents) + if ip == nil || ip.To4() == nil { + return fmt.Errorf("invalid IP in A record: %s", contents) + } + return rc.SetTargetIP(ip) // Reformat to canonical form. + case "AAAA": + ip := net.ParseIP(contents) + if ip == nil || ip.To16() == nil { + return fmt.Errorf("invalid IP in AAAA record: %s", contents) + } + return rc.SetTargetIP(ip) // Reformat to canonical form. + case "AKAMAICDN", "ALIAS", "ANAME", "CNAME", "NS", "PTR": + return rc.SetTarget(contents) + case "CAA": + return rc.SetTargetCAAString(contents) + case "DS": + return rc.SetTargetDSString(contents) + case "DHCID": + return rc.SetTarget(contents) + case "LOC": + return rc.SetTargetLOCString(origin, contents) + case "MX": + return rc.SetTargetMXString(contents) + case "NAPTR": + return rc.SetTargetNAPTRString(contents) + case "SOA": + return rc.SetTargetSOAString(contents) + case "SPF", "TXT": + if txtFn == nil { + return rc.SetTargetTXT(contents) + } else { + return rc.SetTargetTXT(txtFn(contents)) + } + case "SRV": + return rc.SetTargetSRVString(contents) + case "SSHFP": + return rc.SetTargetSSHFPString(contents) + case "TLSA": + return rc.SetTargetTLSAString(contents) + default: + return fmt.Errorf("unknown rtype (%s) when parsing (%s) domain=(%s)", + rtype, contents, origin) + } +} + // PopulateFromString populates a RecordConfig given a type and string. Many // providers give all the parameters of a resource record in one big string. // This helper function lets you not re-invent the wheel. @@ -81,55 +133,3 @@ func (rc *RecordConfig) PopulateFromString(rtype, contents, origin string) error rtype, contents, origin) } } - -func (rc *RecordConfig) PopulateFromStringFunc(rtype, contents, origin string, txtFn func(s string) string) error { - if rc.Type != "" && rc.Type != rtype { - return fmt.Errorf("assertion failed: rtype already set (%s) (%s)", rtype, rc.Type) - } - - switch rc.Type = rtype; rtype { // #rtype_variations - case "A": - ip := net.ParseIP(contents) - if ip == nil || ip.To4() == nil { - return fmt.Errorf("invalid IP in A record: %s", contents) - } - return rc.SetTargetIP(ip) // Reformat to canonical form. - case "AAAA": - ip := net.ParseIP(contents) - if ip == nil || ip.To16() == nil { - return fmt.Errorf("invalid IP in AAAA record: %s", contents) - } - return rc.SetTargetIP(ip) // Reformat to canonical form. - case "AKAMAICDN", "ALIAS", "ANAME", "CNAME", "NS", "PTR": - return rc.SetTarget(contents) - case "CAA": - return rc.SetTargetCAAString(contents) - case "DS": - return rc.SetTargetDSString(contents) - case "DHCID": - return rc.SetTarget(contents) - case "LOC": - return rc.SetTargetLOCString(origin, contents) - case "MX": - return rc.SetTargetMXString(contents) - case "NAPTR": - return rc.SetTargetNAPTRString(contents) - case "SOA": - return rc.SetTargetSOAString(contents) - case "SPF", "TXT": - if txtFn == nil { - return rc.SetTargetTXT(contents) - } else { - return rc.SetTargetTXT(txtFn(contents)) - } - case "SRV": - return rc.SetTargetSRVString(contents) - case "SSHFP": - return rc.SetTargetSSHFPString(contents) - case "TLSA": - return rc.SetTargetTLSAString(contents) - default: - return fmt.Errorf("unknown rtype (%s) when parsing (%s) domain=(%s)", - rtype, contents, origin) - } -}