-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearchdomain.go
51 lines (44 loc) · 1.33 KB
/
searchdomain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package resolvconf
import (
"fmt"
"unicode/utf8"
)
// SearchDomain is one of the items in the search list
type SearchDomain struct {
Name string
}
// NewSearchDomain creates a new search domain that will be added
// to the 'search' list in the generated file
func NewSearchDomain(dom string) *SearchDomain {
return &SearchDomain{dom}
}
func (sd SearchDomain) applyLimits(conf *Conf) (bool, error) {
// Search if conf search domain is already added
if conf.Find(sd) != nil {
return false, fmt.Errorf("Search domain %s already exists in conf", sd.Name)
}
// Check max limit
doms := conf.GetSearchDomains()
if len(doms) == searchDomainMaxCount {
return false, fmt.Errorf("Too many search domains, %d is maximum", searchDomainMaxCount)
}
// Check max char count limit
var charcount int
for _, str := range doms {
charcount += utf8.RuneCountInString(str.Name)
}
if charcount+utf8.RuneCountInString(sd.Name) > searchDomainMaxCharCount {
return false, fmt.Errorf("Too many charactes is search domain list, %d is maximum", searchDomainMaxCharCount)
}
return true, nil
}
func (sd SearchDomain) String() string {
return sd.Name
}
// Equal compares two search domains with each other, returns true if equal
func (sd SearchDomain) Equal(b ConfItem) bool {
if item, ok := b.(*SearchDomain); ok {
return sd.Name == item.Name
}
return false
}