clean up config validation a bit

This commit is contained in:
Liam Stanley 2017-02-22 00:24:23 -05:00
parent 05d37d0840
commit 70a6fe8739
2 changed files with 19 additions and 11 deletions

View File

@ -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")

13
conn.go
View File

@ -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