@@ -56,6 +56,32 @@ type Base struct {
56
56
RetryDialAfter string `mapstructure:"retry_dial_after"`
57
57
}
58
58
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
+
59
85
// validate validates the configuration's base section.
60
86
func (b Base ) validate () error {
61
87
var errs string
@@ -71,34 +97,11 @@ func (b Base) validate() error {
71
97
if b .StartRank < 1 {
72
98
errs += "\t start_rank must be 1 or higher\n "
73
99
}
74
- protocol := regexp .MustCompile (`(tcp|unix)://` ).FindString (b .ValidatorListenAddress )
75
- if protocol == "" {
76
- errs += "\t validator_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 += "\t validator_laddr is not in the host:port format\n "
81
- } else {
82
- if ip := net .ParseIP (host ); ip == nil {
83
- errs += "\t validator_laddr is not a valid IPv4 address\n "
84
- }
85
- }
86
- } else if protocol == "unix://" {
87
- if ! strings .HasSuffix (b .ValidatorListenAddress , ".sock" ) {
88
- errs += "\n validator_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 ())
90
102
}
91
- if ! strings .HasPrefix (b .ValidatorListenAddressRPC , "tcp://" ) {
92
- errs += "\t validator_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 += "\t validator_laddr_rpc is not in the host:port format\n "
97
- } else {
98
- if ip := net .ParseIP (host ); ip == nil {
99
- errs += "\t validator_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 ())
102
105
}
103
106
if b .RetryDialAfter == "" {
104
107
errs += "\t retry_dial_after must not be empty\n "
0 commit comments