Pull some config-related functions out to their own file.
This commit is contained in:
parent
7d317743bb
commit
b3df846212
42
config.go
Normal file
42
config.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
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
|
||||
}
|
11
main.go
11
main.go
|
@ -138,13 +138,16 @@ func main() {
|
|||
|
||||
// If a proxy definition exists, append it
|
||||
if s.Proxy != "" {
|
||||
ports := strings.Split(s.Ports[0], ":")
|
||||
if len(ports) == 0 {
|
||||
fmt.Println("Error: No port defined for proxy to", s.Proxy)
|
||||
port, err := s.GetHostPort()
|
||||
if err != nil {
|
||||
fmt.Println("Error in service", name)
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
err = AddProxy(s.Proxy, ports[0])
|
||||
|
||||
err = AddProxy(s.Proxy, port)
|
||||
if err != nil {
|
||||
fmt.Println("Error adding proxy for service", name)
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue