github.com/StackExchange/DNSControl@v0.2.8/models/t_mx.go (about)

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