Refactor some code

This commit is contained in:
TwinProduction 2021-09-30 20:56:09 -04:00
parent bc25fea1c0
commit ac43ef4ab7
2 changed files with 6 additions and 11 deletions

View File

@ -38,11 +38,11 @@ func CanPerformStartTLS(address string, config *Config) (connected bool, certifi
if len(hostAndPort) != 2 { if len(hostAndPort) != 2 {
return false, nil, errors.New("invalid address for starttls, format must be host:port") return false, nil, errors.New("invalid address for starttls, format must be host:port")
} }
conn, err := net.DialTimeout("tcp", address, config.Timeout) connection, err := net.DialTimeout("tcp", address, config.Timeout)
if err != nil { if err != nil {
return return
} }
smtpClient, err := smtp.NewClient(conn, hostAndPort[0]) smtpClient, err := smtp.NewClient(connection, hostAndPort[0])
if err != nil { if err != nil {
return return
} }

View File

@ -44,6 +44,7 @@ var (
) )
// Service is the configuration of a monitored endpoint // Service is the configuration of a monitored endpoint
// XXX: Rename this to Endpoint in v4.0.0?
type Service struct { type Service struct {
// Enabled defines whether to enable the service // Enabled defines whether to enable the service
Enabled *bool `yaml:"enabled,omitempty"` Enabled *bool `yaml:"enabled,omitempty"`
@ -227,17 +228,11 @@ func (service *Service) call(result *Result) {
service.DNS.query(service.URL, result) service.DNS.query(service.URL, result)
result.Duration = time.Since(startTime) result.Duration = time.Since(startTime)
} else if isServiceStartTLS || isServiceTLS { } else if isServiceStartTLS || isServiceTLS {
var clientFunction func(address string, config *client.Config) (connected bool, certificate *x509.Certificate, err error)
var addressPrefix string
if isServiceStartTLS { if isServiceStartTLS {
clientFunction = client.CanPerformStartTLS result.Connected, certificate, err = client.CanPerformStartTLS(strings.TrimPrefix(service.URL, "starttls://"), service.ClientConfig)
addressPrefix = "starttls://" } else {
} else if isServiceTLS { result.Connected, certificate, err = client.CanPerformStartTLS(strings.TrimPrefix(service.URL, "tls://"), service.ClientConfig)
clientFunction = client.CanPerformTLS
addressPrefix = "tls://"
} }
result.Connected, certificate, err = clientFunction(strings.TrimPrefix(service.URL, addressPrefix), service.ClientConfig)
if err != nil { if err != nil {
result.AddError(err.Error()) result.AddError(err.Error())
return return