From 70a6fe87396df55f18e0db5c58bcb8439f4cdbf5 Mon Sep 17 00:00:00 2001 From: Liam Stanley Date: Wed, 22 Feb 2017 00:24:23 -0500 Subject: [PATCH] clean up config validation a bit --- client.go | 17 +++++++++++++++++ conn.go | 13 ++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index 849e2b2..d302489 100644 --- a/client.go +++ b/client.go @@ -168,6 +168,23 @@ type Config struct { HandleNickCollide func(oldNick string) (newNick string) } +// isValid checks some basic settings to ensure the config is valid. +func (conf Config) isValid() error { + if conf.Server == "" { + return errors.New("invalid server specified") + } + + if conf.Port < 21 || conf.Port > 65535 { + return errors.New("invalid port (21-65535)") + } + + if !IsValidNick(conf.Nick) || !IsValidUser(conf.User) { + return errors.New("invalid nickname or user") + } + + return nil +} + // ErrNotConnected is returned if a method is used when the client isn't // connected. var ErrNotConnected = errors.New("client is not connected to server") diff --git a/conn.go b/conn.go index 8bce183..0b4b062 100644 --- a/conn.go +++ b/conn.go @@ -51,17 +51,8 @@ type ircConn struct { // newConn sets up and returns a new connection to the server. This includes // setting up things like proxies, ssl/tls, and other misc. things. func newConn(conf Config, addr string) (*ircConn, error) { - // Sanity check a few options. - if conf.Server == "" { - return nil, errors.New("invalid server specified") - } - - if conf.Port < 21 || conf.Port > 65535 { - return nil, errors.New("invalid port (21-65535)") - } - - if !IsValidNick(conf.Nick) || !IsValidUser(conf.User) { - return nil, errors.New("invalid nickname or user") + if err := conf.isValid(); err != nil { + return nil, fmt.Errorf("invalid configuration: %s", err) } var conn net.Conn