mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2025-12-09 13:46:07 +08:00
Clean up cfsingleredirect
This commit is contained in:
parent
779d2f3bef
commit
a5a624ff96
5 changed files with 37 additions and 37 deletions
|
|
@ -59,7 +59,9 @@ func (rc *RecordConfig) PopulateFromStringFunc(rtype, contents, origin string, t
|
|||
return fmt.Errorf("assertion failed: rtype already set (%s) (%s)", rtype, rc.Type)
|
||||
}
|
||||
|
||||
switch rc.Type = rtype; rtype { // #rtype_variations
|
||||
rc.Type = rtype
|
||||
|
||||
switch rtype { // #rtype_variations
|
||||
case "A":
|
||||
ip := net.ParseIP(contents)
|
||||
if ip == nil || ip.To4() == nil {
|
||||
|
|
|
|||
|
|
@ -60,19 +60,19 @@ func NewRecordConfigFromRaw(t string, args []any, dc *models.DomainConfig) (*mod
|
|||
return rec, nil
|
||||
}
|
||||
|
||||
func stringifyMetas(metas []map[string]any) map[string]string {
|
||||
result := make(map[string]string)
|
||||
for _, m := range metas {
|
||||
for mk, mv := range m {
|
||||
if v, ok := mv.(string); ok {
|
||||
result[mk] = v // Already a string. No new malloc.
|
||||
} else {
|
||||
result[mk] = fmt.Sprintf("%v", mv)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
// func stringifyMetas(metas []map[string]any) map[string]string {
|
||||
// result := make(map[string]string)
|
||||
// for _, m := range metas {
|
||||
// for mk, mv := range m {
|
||||
// if v, ok := mv.(string); ok {
|
||||
// result[mk] = v // Already a string. No new malloc.
|
||||
// } else {
|
||||
// result[mk] = fmt.Sprintf("%v", mv)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return result
|
||||
// }
|
||||
|
||||
func setRecordNames(rec *models.RecordConfig, dc *models.DomainConfig, n string) {
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import (
|
|||
)
|
||||
|
||||
// backwards compatibility:
|
||||
var validTypes = map[string]struct{}{}
|
||||
//var validTypes = map[string]struct{}{}
|
||||
|
||||
type RType interface {
|
||||
// Returns the name of the rtype ("A", "MX", etc.)
|
||||
|
|
@ -16,9 +16,6 @@ type RType interface {
|
|||
|
||||
// RecordConfig factory. Updates a RecordConfig's fields based on args.
|
||||
FromArgs(*models.DomainConfig, *models.RecordConfig, []any) error
|
||||
|
||||
// Returns a string representation of the record in RFC1038 format.
|
||||
// AsRFC1038String([]string) (string, error)
|
||||
}
|
||||
|
||||
// Map of registered rtypes.
|
||||
|
|
|
|||
|
|
@ -78,6 +78,6 @@ func FromArgs_helper(dc *models.DomainConfig, rec *models.RecordConfig, args []a
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rec.SetTarget(target) // Overwrite target to old-style for compatibility
|
||||
_ = rec.SetTarget(target) // Overwrite target to old-style for compatibility
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,34 +32,39 @@ func (handle *SingleRedirectConfig) Name() string {
|
|||
|
||||
func (handle *SingleRedirectConfig) FromArgs(dc *models.DomainConfig, rec *models.RecordConfig, args []any) error {
|
||||
//fmt.Printf("DEBUG: CLOUDFLAREAPI_SINGLE_REDIRECT FromArgs called with args=%+v\n", args)
|
||||
|
||||
// Pave the args to be the expected types.
|
||||
if err := rtypecontrol.PaveArgs(args, "siss"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Unpack the args:
|
||||
var name, when, then string
|
||||
var code uint16
|
||||
var name = args[0].(string)
|
||||
var code = args[1].(uint16)
|
||||
var when = args[2].(string)
|
||||
var then = args[3].(string)
|
||||
|
||||
name = args[0].(string)
|
||||
code = args[1].(uint16)
|
||||
// Validate
|
||||
if code != 301 && code != 302 && code != 303 && code != 307 && code != 308 {
|
||||
return fmt.Errorf("%s: code (%03d) is not 301,302,303,307,308", rec.FilePos, code)
|
||||
}
|
||||
when = args[2].(string)
|
||||
then = args[3].(string)
|
||||
display := targetFromRaw(name, code, when, then)
|
||||
|
||||
// Calclate the Comparable and ZonefilePartial values:
|
||||
display := targetFromRaw(name, code, when, then)
|
||||
rec.Comparable = display
|
||||
rec.ZonefilePartial = display
|
||||
|
||||
// Set the fields
|
||||
rec.F = &SingleRedirectConfig{
|
||||
Code: code,
|
||||
//
|
||||
Code: code,
|
||||
SRName: name,
|
||||
SRWhen: when,
|
||||
SRThen: then,
|
||||
SRDisplay: display,
|
||||
}
|
||||
|
||||
//rec.Name = name
|
||||
// Usually these fields do not need to be changed. The caller sets appropriate values.
|
||||
// But Cloudflare Single Redirects always use "@" as the name and TTL=1. We override here.
|
||||
rec.Name = "@"
|
||||
rec.NameRaw = "@"
|
||||
rec.NameUnicode = "@"
|
||||
|
|
@ -67,10 +72,9 @@ func (handle *SingleRedirectConfig) FromArgs(dc *models.DomainConfig, rec *model
|
|||
rec.NameFQDNRaw = dc.NameRaw
|
||||
rec.NameFQDNUnicode = dc.NameUnicode
|
||||
rec.TTL = 1
|
||||
rec.Comparable = display
|
||||
rec.ZonefilePartial = display
|
||||
|
||||
_ = rec.SetTarget(name)
|
||||
// Fill in the legacy fields:
|
||||
handle.CopyToLegacyFields(rec)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -84,12 +88,9 @@ func targetFromRaw(name string, code uint16, when, then string) string {
|
|||
)
|
||||
}
|
||||
|
||||
func (handle *SingleRedirectConfig) AsRFC1038String(*models.RecordConfig) string {
|
||||
return handle.SRDisplay
|
||||
}
|
||||
|
||||
func (handle *SingleRedirectConfig) CopyToLegacyFields(rec *models.RecordConfig) {
|
||||
rec.SetTarget(handle.SRDisplay)
|
||||
//rec.SetTarget(handle.SRDisplay)
|
||||
_ = rec.SetTarget(handle.SRName)
|
||||
}
|
||||
|
||||
//func (handle *SingleRedirectConfig) IDNFields(argsRaw) (argsIDN, argsUnicode, error) {}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue