Skip to content

Commit fc08a5d

Browse files
committed
Refactor ignore logic
1 parent 9d74253 commit fc08a5d

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

pkg/providers/porkbun/api/api.go

+9-26
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ func (p Api) RetrieveRecords(domain string) ([]Record, error) {
4646
return nil, err
4747
}
4848

49-
records := ignoreRecords(response.Records)
49+
records := make([]Record, 0)
50+
for _, record := range response.Records {
51+
if record.isIgnored() {
52+
continue
53+
}
54+
records = append(records, record)
55+
}
56+
5057
return records, nil
5158
}
5259

@@ -61,7 +68,7 @@ func (p Api) CreateRecord(domain string, toCreate Record) (string, error) {
6168
Id int `json:"id"`
6269
}
6370

64-
if isIgnored(toCreate) {
71+
if toCreate.isIgnored() {
6572
return "", fmt.Errorf("cannot create an ignored record: %s", toCreate)
6673
}
6774

@@ -92,30 +99,6 @@ func (p Api) DeleteRecord(domain string, id string) error {
9299
return nil
93100
}
94101

95-
func isIgnored(record Record) bool {
96-
if record.Type == "NS" {
97-
return true
98-
}
99-
if strings.HasPrefix(record.Name, "_acme-challenge") {
100-
return true
101-
}
102-
103-
return false
104-
}
105-
106-
func ignoreRecords(input []Record) []Record {
107-
records := make([]Record, 0)
108-
for _, record := range input {
109-
if isIgnored(record) {
110-
continue
111-
}
112-
113-
records = append(records, record)
114-
}
115-
116-
return records
117-
}
118-
119102
// Trims a root domain from a longer subdomain. For example, trims
120103
// host.example.com to host. If the subdomain is example.com, then
121104
// returns an empty string

pkg/providers/porkbun/api/record.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package api
22

3-
import "bacon/pkg/dns"
3+
import (
4+
"bacon/pkg/dns"
5+
"strings"
6+
)
47

58
type Record struct {
69
Id string `json:"id"`
@@ -37,3 +40,14 @@ func (r Record) GetPriority() string {
3740
}
3841

3942
var _ dns.Record = Record{}
43+
44+
func (r Record) isIgnored() bool {
45+
if r.Type == "NS" {
46+
return true
47+
}
48+
if strings.HasPrefix(r.Name, "_acme-challenge") {
49+
return true
50+
}
51+
52+
return false
53+
}

pkg/providers/porkbun/api/record_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,29 @@ func TestRecord(t *testing.T) {
3232
t.Error("did not return the expected content")
3333
}
3434
}
35+
36+
func TestIgnoredName(t *testing.T) {
37+
ignoredRecord := Record{
38+
Name: "_acme-challenge.bacontest42.com",
39+
Type: "TXT",
40+
TTL: "600",
41+
Content: "c_V4WaKPWlisAvnvTZ62BOuLiQDpkC2cOtahW8TDthw",
42+
}
43+
44+
if !ignoredRecord.isIgnored() {
45+
t.Error("did not ignore record with _acme-challenge")
46+
}
47+
}
48+
49+
func TestIgnoredType(t *testing.T) {
50+
ignoredRecord := Record{
51+
Name: "www.bacontest42.com",
52+
Type: "NS",
53+
TTL: "600",
54+
Content: "c_V4WaKPWlisAvnvTZ62BOuLiQDpkC2cOtahW8TDsfs",
55+
}
56+
57+
if !ignoredRecord.isIgnored() {
58+
t.Error("did not ignore NS record")
59+
}
60+
}

0 commit comments

Comments
 (0)