github.com/teknogeek/dnscontrol/v2@v2.10.1-0.20200227202244-ae299b55ba42/models/t_mx.go (about)

     1  package models
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // SetTargetMX sets the MX fields.
    10  func (rc *RecordConfig) SetTargetMX(pref uint16, target string) error {
    11  	rc.MxPreference = pref
    12  	rc.SetTarget(target)
    13  	if rc.Type == "" {
    14  		rc.Type = "MX"
    15  	}
    16  	if rc.Type != "MX" {
    17  		panic("assertion failed: SetTargetMX called when .Type is not MX")
    18  	}
    19  	return nil
    20  }
    21  
    22  // SetTargetMXStrings is like SetTargetMX but accepts strings.
    23  func (rc *RecordConfig) SetTargetMXStrings(pref, target string) error {
    24  	u64pref, err := strconv.ParseUint(pref, 10, 16)
    25  	if err != nil {
    26  		return fmt.Errorf("can't parse MX data: %w", err)
    27  	}
    28  	return rc.SetTargetMX(uint16(u64pref), target)
    29  }
    30  
    31  // SetTargetMXString is like SetTargetMX but accepts one big string.
    32  func (rc *RecordConfig) SetTargetMXString(s string) error {
    33  	part := strings.Fields(s)
    34  	if len(part) != 2 {
    35  		return fmt.Errorf("MX value does not contain 2 fields: (%#v)", s)
    36  	}
    37  	return rc.SetTargetMXStrings(part[0], part[1])
    38  }