89 lines
1.6 KiB
Go
89 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/hjson/hjson-go/v4"
|
|
)
|
|
|
|
// Errors
|
|
var ErrUndefined = errors.New("value not defined")
|
|
var ErrFileNotFound = errors.New("file not found")
|
|
var ErrParse = errors.New("file could not be parsed")
|
|
|
|
// File and parsing errors
|
|
type ParseError struct{
|
|
File string
|
|
Err error
|
|
}
|
|
|
|
func (e *ParseError) Error() string {
|
|
return fmt.Sprintf("Error parsing configuration file %s: %s",
|
|
e.File, e.Err.Error())
|
|
}
|
|
|
|
func (e *ParseError) Unwrap() error { return e.Err }
|
|
|
|
func fileNotFoundError(file string) *ParseError {
|
|
return &ParseError{file, ErrFileNotFound}
|
|
}
|
|
|
|
func parsingError(file string) *ParseError {
|
|
return &ParseError{file, ErrParse}
|
|
}
|
|
|
|
|
|
// Config struct errors
|
|
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}
|
|
}
|
|
|
|
|
|
func ParseConfig(file string) (Config, error) {
|
|
var err error
|
|
var c Config
|
|
|
|
fp, err := os.ReadFile(file)
|
|
if err != nil {
|
|
return Config{}, fileNotFoundError(file)
|
|
}
|
|
|
|
err = hjson.Unmarshal(fp, &c)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return Config{}, parsingError(file)
|
|
}
|
|
|
|
return c, nil
|
|
}
|
|
|
|
// 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
|
|
}
|