1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

Use switch/case to make code easier to follow

This commit is contained in:
Eri Bastos 2020-04-16 19:26:58 -04:00
parent 4ca167ac43
commit 019388f7b5

View File

@ -181,15 +181,16 @@ func GetIPOnline(configuration *Settings) (string, error) {
// CheckSettings check the format of settings // CheckSettings check the format of settings
func CheckSettings(config *Settings) error { func CheckSettings(config *Settings) error {
if config.Provider == DNSPOD { switch config.Provider {
case DNSPOD:
if config.Password == "" && config.LoginToken == "" { if config.Password == "" && config.LoginToken == "" {
return errors.New("password or login token cannot be empty") return errors.New("password or login token cannot be empty")
} }
} else if config.Provider == HE { case HE:
if config.Password == "" { if config.Password == "" {
return errors.New("password cannot be empty") return errors.New("password cannot be empty")
} }
} else if config.Provider == CLOUDFLARE { case CLOUDFLARE:
if config.LoginToken == "" { if config.LoginToken == "" {
if config.Email == "" { if config.Email == "" {
return errors.New("email cannot be empty") return errors.New("email cannot be empty")
@ -198,26 +199,27 @@ func CheckSettings(config *Settings) error {
return errors.New("password cannot be empty") return errors.New("password cannot be empty")
} }
} }
} else if config.Provider == ALIDNS { case ALIDNS:
if config.Email == "" { if config.Email == "" {
return errors.New("email cannot be empty") return errors.New("email cannot be empty")
} }
if config.Password == "" { if config.Password == "" {
return errors.New("password cannot be empty") return errors.New("password cannot be empty")
} }
} else if config.Provider == DUCK { case DUCK:
if config.LoginToken == "" { if config.LoginToken == "" {
return errors.New("login token cannot be empty") return errors.New("login token cannot be empty")
} }
} else if config.Provider == GOOGLE { case GOOGLE:
if config.Email == "" { if config.Email == "" {
return errors.New("email cannot be empty") return errors.New("email cannot be empty")
} }
if config.Password == "" { if config.Password == "" {
return errors.New("password cannot be empty") return errors.New("password cannot be empty")
} }
} else { default:
return errors.New("please provide supported DNS provider: DNSPod/HE/AliDNS/Cloudflare/GoogleDomain/DuckDNS") return errors.New("please provide supported DNS provider: DNSPod/HE/AliDNS/Cloudflare/GoogleDomain/DuckDNS")
} }
return nil return nil
@ -237,7 +239,6 @@ func SendTelegramNotify(configuration *Settings, domain, currentIP string) error
return errors.New("chat id cannot be empty") return errors.New("chat id cannot be empty")
} }
client := GetHttpClient(configuration) client := GetHttpClient(configuration)
tpl := configuration.Notify.Telegram.MsgTemplate tpl := configuration.Notify.Telegram.MsgTemplate
if tpl == "" { if tpl == "" {
@ -312,11 +313,11 @@ func SendMailNotify(configuration *Settings, domain, currentIP string) error {
// SendNotify sends notify if IP is changed // SendNotify sends notify if IP is changed
func SendNotify(configuration *Settings, domain, currentIP string) error { func SendNotify(configuration *Settings, domain, currentIP string) error {
err := SendTelegramNotify(configuration, domain, currentIP) err := SendTelegramNotify(configuration, domain, currentIP)
if (err != nil) { if err != nil {
log.Println("Send telegram notification with error:", err.Error()) log.Println("Send telegram notification with error:", err.Error())
} }
err = SendMailNotify(configuration, domain, currentIP) err = SendMailNotify(configuration, domain, currentIP)
if (err != nil) { if err != nil {
log.Println("Send email notification with error:", err.Error()) log.Println("Send email notification with error:", err.Error())
} }
return nil return nil