package main import ( "errors" "fmt" _ "strconv" "strings" ) // Configuration errors var ErrUndefined = errors.New("value not defined") type ConfigError struct { Key string Value interface{} Err error } func (e *ConfigError) Error() string { return fmt.Sprintf("Error in configuration for %s: %s", e.Key, e.Err.Error()) } func (e *ConfigError) Unwrap() error { return e.Err } func undefinedError(key string) *ConfigError { return &ConfigError{key, nil, ErrUndefined} } // Get the first host port from a service func (s ServiceConfig) GetHostPort() (string, error) { if s.Ports == nil { return "", undefinedError("ports") } ports := strings.Split(s.Ports[0], ":") if len(ports) == 0 { return "", undefinedError("ports") } return ports[0], nil }