Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit a12dd00

Browse files
author
fbockaj
committed
Add helper for Base validate
1 parent 9ffb6a7 commit a12dd00

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

config/config.go

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@ type Base struct {
5656
RetryDialAfter string `mapstructure:"retry_dial_after"`
5757
}
5858

59+
// validateAddress validates the configuration's addresses.
60+
func validateAddress(addr string, addrName string) error {
61+
protocol := regexp.MustCompile(`(tcp|unix)://`).FindString(addr)
62+
switch protocol {
63+
case "":
64+
return fmt.Errorf("%v is missing the protocol", addrName)
65+
66+
case "tcp://":
67+
host, _, err := net.SplitHostPort(strings.TrimPrefix(addr, protocol))
68+
if err != nil {
69+
return fmt.Errorf("%v is not in the host:port format", addrName)
70+
} else {
71+
if ip := net.ParseIP(host); ip == nil {
72+
return fmt.Errorf("%v is not a valid IPv4 address", addrName)
73+
}
74+
}
75+
76+
case "unix://":
77+
if !strings.HasSuffix(addr, ".sock") {
78+
return fmt.Errorf("%v is not a unix domain socket address", addrName)
79+
}
80+
}
81+
82+
return nil
83+
}
84+
5985
// validate validates the configuration's base section.
6086
func (b Base) validate() error {
6187
var errs string
@@ -71,34 +97,11 @@ func (b Base) validate() error {
7197
if b.StartRank < 1 {
7298
errs += "\tstart_rank must be 1 or higher\n"
7399
}
74-
protocol := regexp.MustCompile(`(tcp|unix)://`).FindString(b.ValidatorListenAddress)
75-
if protocol == "" {
76-
errs += "\tvalidator_laddr is missing the protocol\n"
77-
} else if protocol == "tcp://" {
78-
host, _, err := net.SplitHostPort(strings.TrimPrefix(b.ValidatorListenAddress, protocol))
79-
if err != nil {
80-
errs += "\tvalidator_laddr is not in the host:port format\n"
81-
} else {
82-
if ip := net.ParseIP(host); ip == nil {
83-
errs += "\tvalidator_laddr is not a valid IPv4 address\n"
84-
}
85-
}
86-
} else if protocol == "unix://" {
87-
if !strings.HasSuffix(b.ValidatorListenAddress, ".sock") {
88-
errs += "\nvalidator_laddr is not a unix domain socket address\n"
89-
}
100+
if err := validateAddress(b.ValidatorListenAddress, "validator_laddr"); err != nil {
101+
errs += fmt.Sprintf("\t%v\n", err.Error())
90102
}
91-
if !strings.HasPrefix(b.ValidatorListenAddressRPC, "tcp://") {
92-
errs += "\tvalidator_laddr_rpc is missing the protocol\n"
93-
} else {
94-
host, _, err := net.SplitHostPort(strings.TrimPrefix(b.ValidatorListenAddressRPC, "tcp://"))
95-
if err != nil {
96-
errs += "\tvalidator_laddr_rpc is not in the host:port format\n"
97-
} else {
98-
if ip := net.ParseIP(host); ip == nil {
99-
errs += "\tvalidator_laddr_rpc is not a valid IPv4 address\n"
100-
}
101-
}
103+
if err := validateAddress(b.ValidatorListenAddressRPC, "validator_laddr_rpc"); err != nil {
104+
errs += fmt.Sprintf("\t%v\n", err.Error())
102105
}
103106
if b.RetryDialAfter == "" {
104107
errs += "\tretry_dial_after must not be empty\n"

0 commit comments

Comments
 (0)