diff --git a/dns/dns.go b/dns/dns.go index f97cb24..3fae6ba 100644 --- a/dns/dns.go +++ b/dns/dns.go @@ -2,8 +2,8 @@ package dns import ( "strconv" + "strings" - "git.xbudex.com/buddy/update-dns/utils" "github.com/dnsimple/dnsimple-go/dnsimple" "github.com/pkg/errors" ) @@ -33,7 +33,7 @@ func (d *Client) GetRecords(host, kind string) ([]Record, error) { return nil, errors.Wrap(err, "unable to get account id") } - subdomain, domain, err := utils.SplitHost(host) + subdomain, domain, err := splitHost(host) if err != nil { return nil, errors.Wrap(err, "unable to parse host") } @@ -59,7 +59,7 @@ func (d *Client) CreateRecord(host string, record Record) error { return errors.Wrap(err, "unable to get account id") } - subdomain, domain, err := utils.SplitHost(host) + subdomain, domain, err := splitHost(host) if err != nil { return errors.Wrap(err, "unable to parse host") } @@ -113,3 +113,21 @@ func (d *Client) getAccountID() (string, error) { d.accountID = strconv.Itoa(whoamiResponse.Data.Account.ID) return d.accountID, nil } + +// splitHost returns hostname into domain and subdomain strings. +// For example: +// "some.subdomain.example.com" → ("some.subdomain", "example.com", nil) +// "example.com" → ("", "example.com", nil) +func splitHost(host string) (subdomain string, domain string, err error) { + subdomains := strings.Split(host, ".") + + if len(subdomains) >= 3 { + domain = strings.Join(subdomains[len(subdomains)-2:], ".") + subdomain = strings.Join(subdomains[:len(subdomains)-2], ".") + } else if len(subdomains) == 2 { + domain = strings.Join(subdomains, ".") + } else { + err = errors.New("invalid domain") + } + return subdomain, domain, err +} diff --git a/utils/utils.go b/utils/utils.go deleted file mode 100644 index ca3903e..0000000 --- a/utils/utils.go +++ /dev/null @@ -1,25 +0,0 @@ -package utils - -import ( - "strings" - - "github.com/pkg/errors" -) - -// SplitHost returns hostname into domain and subdomain strings. -// For example: -// "some.subdomain.example.com" → ("some.subdomain", "example.com", nil) -// "example.com" → ("", "example.com", nil) -func SplitHost(host string) (subdomain string, domain string, err error) { - subdomains := strings.Split(host, ".") - - if len(subdomains) >= 3 { - domain = strings.Join(subdomains[len(subdomains)-2:], ".") - subdomain = strings.Join(subdomains[:len(subdomains)-2], ".") - } else if len(subdomains) == 2 { - domain = strings.Join(subdomains, ".") - } else { - err = errors.New("invalid domain") - } - return subdomain, domain, err -}