Skip to content

Commit

Permalink
added split domain
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Aug 6, 2019
1 parent 5d4c51c commit dcd2a52
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
41 changes: 37 additions & 4 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ func TrimZone(zone, name string) string {
return newName
}

// NormalizeDomain will normalize the provided domain name.
func NormalizeDomain(name string, lower, fqdn bool) string {
// NormalizeDomain will normalize the provided domain name by removing space
// around the name and lowercase it if request.
func NormalizeDomain(name string, lower, makeFQDN, removeFQDN bool) string {
// remove spaces
name = strings.TrimSpace(name)

Expand All @@ -58,14 +59,46 @@ func NormalizeDomain(name string, lower, fqdn bool) string {
name = strings.ToLower(name)
}

// ensure FQDN if requested
if fqdn {
// make FQDN if requested
if makeFQDN {
name = dns.Fqdn(name)
}

// remove FQDN if requested
if removeFQDN && dns.IsFqdn(name) {
name = name[:len(name)-1]
}

return name
}

// SplitDomain will split the provided domain either in separate labels or
// hierarchical labels. The late allows walking a domain up to the root.
func SplitDomain(name string, hierarchical bool) []string {
// normalize name
name = NormalizeDomain(name, false, false, true)

// return nil if empty
if name == "" {
return nil
}

// split in labels
if !hierarchical {
return dns.SplitDomainName(name)
}

// prepare list
var list []string

// walk domain
for off, end := 0, false; !end; off, end = dns.NextLabel(name, off) {
list = append(list, name[off:])
}

return list
}

// TransferCase will transfer the case from the source name to the destination.
// For for the source "foo.AAA.com." and destination "aaa.com" the function
// will return "AAA.com". The source must be either a child or the same as the
Expand Down
25 changes: 19 additions & 6 deletions tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,25 @@ func TestTrimZone(t *testing.T) {
}

func TestNormalizeDomain(t *testing.T) {
assert.Equal(t, "", NormalizeDomain("", false, false))
assert.Equal(t, ".", NormalizeDomain("", false, true))
assert.Equal(t, "foo", NormalizeDomain(" foo", false, false))
assert.Equal(t, "foo", NormalizeDomain("foo ", false, false))
assert.Equal(t, "foo", NormalizeDomain(" fOO ", true, false))
assert.Equal(t, "foo.", NormalizeDomain(" fOO ", true, true))
assert.Equal(t, "", NormalizeDomain("", false, false, false))
assert.Equal(t, ".", NormalizeDomain("", false, true, false))
assert.Equal(t, "foo", NormalizeDomain(" foo", false, false, false))
assert.Equal(t, "foo", NormalizeDomain("foo ", false, false, false))
assert.Equal(t, "foo", NormalizeDomain(" fOO ", true, false, false))
assert.Equal(t, "foo.", NormalizeDomain(" fOO ", true, true, false))
assert.Equal(t, "foo", NormalizeDomain(" fOO. ", true, false, true))
}

func TestSplitDomain(t *testing.T) {
assert.Equal(t, []string(nil), SplitDomain("", false))
assert.Equal(t, []string(nil), SplitDomain(".", false))
assert.Equal(t, []string{"foo"}, SplitDomain("foo", false))
assert.Equal(t, []string{"foo", "bar"}, SplitDomain("foo.bar", false))

assert.Equal(t, []string(nil), SplitDomain("", true))
assert.Equal(t, []string(nil), SplitDomain(".", true))
assert.Equal(t, []string{"foo"}, SplitDomain("foo", true))
assert.Equal(t, []string{"foo.bar", "bar"}, SplitDomain("foo.bar", true))
}

func TestTransferCase(t *testing.T) {
Expand Down

0 comments on commit dcd2a52

Please sign in to comment.