Rely exclusively on error to signal failure

Pointed out by: slackpad
This commit is contained in:
Sean Chittenden 2016-04-01 10:34:45 -07:00
parent 7a814fce63
commit 45bd128f12
2 changed files with 23 additions and 23 deletions

View File

@ -193,7 +193,7 @@ func (c *Command) readConfig() *Config {
} }
// Ensure all endpoints are unique // Ensure all endpoints are unique
if ok, err := config.verifyUniqueListeners(); !ok { if err := config.verifyUniqueListeners(); err != nil {
c.Ui.Error(fmt.Sprintf("All listening endpoints must be unique: %s", err)) c.Ui.Error(fmt.Sprintf("All listening endpoints must be unique: %s", err))
return nil return nil
} }
@ -309,7 +309,7 @@ func (c *Command) readConfig() *Config {
// verifyUniqueListeners checks to see if an address was used more than once in // verifyUniqueListeners checks to see if an address was used more than once in
// the config // the config
func (config *Config) verifyUniqueListeners() (bool, error) { func (config *Config) verifyUniqueListeners() error {
type key struct { type key struct {
host string host string
port int port int
@ -317,7 +317,7 @@ func (config *Config) verifyUniqueListeners() (bool, error) {
const numUniqueAddrs = 7 const numUniqueAddrs = 7
m := make(map[key]string, numUniqueAddrs) m := make(map[key]string, numUniqueAddrs)
testFunc := func(k key, descr string) (bool, error) { testFunc := func(k key, descr string) error {
if k.host == "" { if k.host == "" {
k.host = "0.0.0.0" k.host = "0.0.0.0"
} else if strings.HasPrefix(k.host, "unix") { } else if strings.HasPrefix(k.host, "unix") {
@ -325,47 +325,47 @@ func (config *Config) verifyUniqueListeners() (bool, error) {
k.port = 0 k.port = 0
} }
if k.host == "0.0.0.0" && k.port <= 0 { if k.host == "0.0.0.0" && k.port <= 0 {
return true, nil return nil
} }
v, ok := m[k] v, ok := m[k]
if ok { if ok {
return false, fmt.Errorf("%s address already configured for %s", descr, v) return fmt.Errorf("%s address already configured for %s", descr, v)
} }
m[k] = descr m[k] = descr
return true, nil return nil
} }
if ok, err := testFunc(key{config.Addresses.RPC, config.Ports.RPC}, "RPC"); !ok { if err := testFunc(key{config.Addresses.RPC, config.Ports.RPC}, "RPC"); err != nil {
return false, err return err
} }
if ok, err := testFunc(key{config.Addresses.DNS, config.Ports.DNS}, "DNS"); !ok { if err := testFunc(key{config.Addresses.DNS, config.Ports.DNS}, "DNS"); err != nil {
return false, err return err
} }
if ok, err := testFunc(key{config.Addresses.HTTP, config.Ports.HTTP}, "HTTP"); !ok { if err := testFunc(key{config.Addresses.HTTP, config.Ports.HTTP}, "HTTP"); err != nil {
return false, err return err
} }
if ok, err := testFunc(key{config.Addresses.HTTPS, config.Ports.HTTPS}, "HTTPS"); !ok { if err := testFunc(key{config.Addresses.HTTPS, config.Ports.HTTPS}, "HTTPS"); err != nil {
return false, err return err
} }
if ok, err := testFunc(key{config.AdvertiseAddr, config.Ports.Server}, "Server RPC"); !ok { if err := testFunc(key{config.AdvertiseAddr, config.Ports.Server}, "Server RPC"); err != nil {
return false, err return err
} }
if ok, err := testFunc(key{config.AdvertiseAddr, config.Ports.SerfLan}, "Serf LAN"); !ok { if err := testFunc(key{config.AdvertiseAddr, config.Ports.SerfLan}, "Serf LAN"); err != nil {
return false, err return err
} }
if ok, err := testFunc(key{config.AdvertiseAddr, config.Ports.SerfWan}, "Serf WAN"); !ok { if err := testFunc(key{config.AdvertiseAddr, config.Ports.SerfWan}, "Serf WAN"); err != nil {
return false, err return err
} }
return true, nil return nil
} }
// setupLoggers is used to setup the logGate, logWriter, and our logOutput // setupLoggers is used to setup the logGate, logWriter, and our logOutput

View File

@ -1018,8 +1018,8 @@ func TestDecodeConfig_verifyUniqueListeners(t *testing.T) {
t.Fatalf("err: %s %s", test.name, err) t.Fatalf("err: %s %s", test.name, err)
} }
ok, err := config.verifyUniqueListeners() err = config.verifyUniqueListeners()
if ok != test.pass { if (err != nil && test.pass) || (err == nil && !test.pass) {
t.Errorf("err: %s should have %v: %v: %v", test.name, test.pass, test.cfg, err) t.Errorf("err: %s should have %v: %v: %v", test.name, test.pass, test.cfg, err)
} }
} }