Pull out another instance of file reading/parsing.
This commit is contained in:
parent
bb1d0363f3
commit
e2857f8672
18
config.go
18
config.go
|
@ -73,6 +73,24 @@ func ParseConfig(file string) (Config, error) {
|
|||
return c, nil
|
||||
}
|
||||
|
||||
func ParseServiceConfig(file string) (ServiceConfig, error) {
|
||||
var err error
|
||||
var c ServiceConfig
|
||||
|
||||
fp, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return ServiceConfig{}, fileNotFoundError(file)
|
||||
}
|
||||
|
||||
err = hjson.Unmarshal(fp, &c)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return ServiceConfig{}, parsingError(file)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Get the first host port from a service
|
||||
func (s ServiceConfig) GetHostPort() (string, error) {
|
||||
if s.Ports == nil {
|
||||
|
|
36
main.go
36
main.go
|
@ -8,7 +8,6 @@ import (
|
|||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/hjson/hjson-go/v4"
|
||||
"github.com/ghodss/yaml"
|
||||
"dario.cat/mergo"
|
||||
)
|
||||
|
@ -70,20 +69,16 @@ func main() {
|
|||
|
||||
// Parse user services
|
||||
for name, service := range config.Services {
|
||||
s := ServiceConfig{
|
||||
Config: make(map[string]interface{}),
|
||||
}
|
||||
|
||||
// If no provider specified, try system
|
||||
if service.Provider == "" { service.Provider = "system" }
|
||||
|
||||
// Read service config file
|
||||
fp := fmt.Sprintf("services/%s/%s.json",
|
||||
name, service.Provider)
|
||||
c, err := os.ReadFile(fp)
|
||||
s, err := ParseServiceConfig(fp)
|
||||
if err != nil {
|
||||
switch err := err.(type) {
|
||||
case *os.PathError:
|
||||
switch err {
|
||||
case ErrFileNotFound:
|
||||
// No system config to merge
|
||||
// Rely solely on the user config
|
||||
fmt.Println(" Warning: No system config for service: " + name)
|
||||
|
@ -94,27 +89,19 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Parse service config
|
||||
err = hjson.Unmarshal(c, &s)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing config file")
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
mergo.Merge(&s, ServiceConfig{
|
||||
Config: make(map[string]interface{}),
|
||||
})
|
||||
mergo.Merge(&s, service, mergo.WithOverride)
|
||||
|
||||
/*
|
||||
// Initialize the config map if it wasn't already
|
||||
if s.Config == map[string]interface{} {
|
||||
s.Config = make(map[string]interface{})
|
||||
}
|
||||
*/
|
||||
|
||||
config.Services[name] = s
|
||||
|
||||
|
||||
// Scripted action
|
||||
|
||||
// Add service packages to the global list
|
||||
config.System.Packages = append(config.System.Packages, s.Packages...)
|
||||
pkgs := &config.System.Packages
|
||||
*pkgs = append(*pkgs, s.Packages...)
|
||||
|
||||
// If a proxy definition exists, append it
|
||||
if s.Proxy != "" {
|
||||
|
@ -133,7 +120,6 @@ func main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle any special case (scripted) configs
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue