Move verifyUniqueListeners to command/agent/config.go

This commit is contained in:
Frank Schroeder 2017-05-03 22:46:00 +02:00 committed by Frank Schröder
parent a0b98948d8
commit 3ea54c48a8
2 changed files with 43 additions and 44 deletions

View file

@ -442,50 +442,6 @@ func (c *Command) readConfig() *Config {
return config
}
// verifyUniqueListeners checks to see if an address was used more than once in
// the config
func (c *Config) verifyUniqueListeners() error {
listeners := []struct {
host string
port int
descr string
}{
{c.Addresses.DNS, c.Ports.DNS, "DNS"},
{c.Addresses.HTTP, c.Ports.HTTP, "HTTP"},
{c.Addresses.HTTPS, c.Ports.HTTPS, "HTTPS"},
{c.AdvertiseAddr, c.Ports.Server, "Server RPC"},
{c.AdvertiseAddr, c.Ports.SerfLan, "Serf LAN"},
{c.AdvertiseAddr, c.Ports.SerfWan, "Serf WAN"},
}
type key struct {
host string
port int
}
m := make(map[key]string, len(listeners))
for _, l := range listeners {
if l.host == "" {
l.host = "0.0.0.0"
} else if strings.HasPrefix(l.host, "unix") {
// Don't compare ports on unix sockets
l.port = 0
}
if l.host == "0.0.0.0" && l.port <= 0 {
continue
}
k := key{l.host, l.port}
v, ok := m[k]
if ok {
return fmt.Errorf("%s address already configured for %s", l.descr, v)
}
m[k] = l.descr
}
return nil
}
// discoverEc2Hosts searches an AWS region, returning a list of instance ips
// where EC2TagKey = EC2TagValue
func (c *Config) discoverEc2Hosts(logger *log.Logger) ([]string, error) {
config := c.RetryJoinEC2

View file

@ -931,6 +931,49 @@ func (c *Config) GetTokenForAgent() string {
return ""
}
// verifyUniqueListeners checks to see if an address was used more than once in
// the config
func (c *Config) verifyUniqueListeners() error {
listeners := []struct {
host string
port int
descr string
}{
{c.Addresses.DNS, c.Ports.DNS, "DNS"},
{c.Addresses.HTTP, c.Ports.HTTP, "HTTP"},
{c.Addresses.HTTPS, c.Ports.HTTPS, "HTTPS"},
{c.AdvertiseAddr, c.Ports.Server, "Server RPC"},
{c.AdvertiseAddr, c.Ports.SerfLan, "Serf LAN"},
{c.AdvertiseAddr, c.Ports.SerfWan, "Serf WAN"},
}
type key struct {
host string
port int
}
m := make(map[key]string, len(listeners))
for _, l := range listeners {
if l.host == "" {
l.host = "0.0.0.0"
} else if strings.HasPrefix(l.host, "unix") {
// Don't compare ports on unix sockets
l.port = 0
}
if l.host == "0.0.0.0" && l.port <= 0 {
continue
}
k := key{l.host, l.port}
v, ok := m[k]
if ok {
return fmt.Errorf("%s address already configured for %s", l.descr, v)
}
m[k] = l.descr
}
return nil
}
// DecodeConfig reads the configuration from the given reader in JSON
// format and decodes it into a proper Config structure.
func DecodeConfig(r io.Reader) (*Config, error) {