Pull some config-related functions out to their own file.

This commit is contained in:
root 2025-03-15 18:15:59 -04:00
parent 7d317743bb
commit b3df846212
2 changed files with 49 additions and 4 deletions

42
config.go Normal file
View 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
View file

@ -138,13 +138,16 @@ func main() {
// If a proxy definition exists, append it // If a proxy definition exists, append it
if s.Proxy != "" { if s.Proxy != "" {
ports := strings.Split(s.Ports[0], ":") port, err := s.GetHostPort()
if len(ports) == 0 { if err != nil {
fmt.Println("Error: No port defined for proxy to", s.Proxy) fmt.Println("Error in service", name)
fmt.Println(err.Error())
return return
} }
err = AddProxy(s.Proxy, ports[0])
err = AddProxy(s.Proxy, port)
if err != nil { if err != nil {
fmt.Println("Error adding proxy for service", name)
fmt.Println(err.Error()) fmt.Println(err.Error())
return return
} }