From b3df8462123339dab6a546f3bd654fb01743d72a Mon Sep 17 00:00:00 2001 From: root Date: Sat, 15 Mar 2025 18:15:59 -0400 Subject: [PATCH] Pull some config-related functions out to their own file. --- config.go | 42 ++++++++++++++++++++++++++++++++++++++++++ main.go | 11 +++++++---- 2 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 config.go diff --git a/config.go b/config.go new file mode 100644 index 0000000..8c2a9c9 --- /dev/null +++ b/config.go @@ -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 +} diff --git a/main.go b/main.go index ea45958..1b0c2f2 100644 --- a/main.go +++ b/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 }