Pull out another instance of file reading/parsing.

This commit is contained in:
root 2025-03-15 20:50:51 -04:00
parent bb1d0363f3
commit e2857f8672
2 changed files with 46 additions and 42 deletions

View file

@ -73,6 +73,24 @@ func ParseConfig(file string) (Config, error) {
return c, nil 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 // Get the first host port from a service
func (s ServiceConfig) GetHostPort() (string, error) { func (s ServiceConfig) GetHostPort() (string, error) {
if s.Ports == nil { if s.Ports == nil {

36
main.go
View file

@ -8,7 +8,6 @@ import (
"strings" "strings"
"text/template" "text/template"
"github.com/hjson/hjson-go/v4"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"dario.cat/mergo" "dario.cat/mergo"
) )
@ -70,20 +69,16 @@ func main() {
// Parse user services // Parse user services
for name, service := range config.Services { for name, service := range config.Services {
s := ServiceConfig{
Config: make(map[string]interface{}),
}
// If no provider specified, try system // If no provider specified, try system
if service.Provider == "" { service.Provider = "system" } if service.Provider == "" { service.Provider = "system" }
// Read service config file // Read service config file
fp := fmt.Sprintf("services/%s/%s.json", fp := fmt.Sprintf("services/%s/%s.json",
name, service.Provider) name, service.Provider)
c, err := os.ReadFile(fp) s, err := ParseServiceConfig(fp)
if err != nil { if err != nil {
switch err := err.(type) { switch err {
case *os.PathError: case ErrFileNotFound:
// No system config to merge // No system config to merge
// Rely solely on the user config // Rely solely on the user config
fmt.Println(" Warning: No system config for service: " + name) fmt.Println(" Warning: No system config for service: " + name)
@ -94,27 +89,19 @@ func main() {
} }
} }
{
// Parse service config // Parse service config
err = hjson.Unmarshal(c, &s) mergo.Merge(&s, ServiceConfig{
if err != nil { Config: make(map[string]interface{}),
fmt.Println("Error parsing config file") })
fmt.Println(err)
return
}
mergo.Merge(&s, service, mergo.WithOverride) 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 config.Services[name] = s
// Scripted action
// Add service packages to the global list // 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 a proxy definition exists, append it
if s.Proxy != "" { if s.Proxy != "" {
@ -133,7 +120,6 @@ func main() {
} }
} }
} }
}
// Handle any special case (scripted) configs // Handle any special case (scripted) configs
{ {