refactor: generalize service definition fetch

This commit is contained in:
Marc 2024-11-10 10:37:46 -05:00
parent 24e795e1aa
commit c580c0c433
Signed by: marc
GPG key ID: 048E042F22B5DC79
3 changed files with 26 additions and 22 deletions

View file

@ -49,10 +49,10 @@ func getStartCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context() ctx := cmd.Context()
flags := ctx.Value("flags").(ParsedFlags) flags := ctx.Value("flags").(ParsedFlags)
def, err := service_definition.GetServiceDefinitionFromFile(flags.definitionPath) def, err := service_definition.GetServiceDefinition(flags.definitionPath)
if err != nil { if err != nil {
return fmt.Errorf("Failed to read service definition from file: %+v", err) return fmt.Errorf("Failed to read service definition: %+v", err)
} }
if flags.daemonHost != "" && flags.daemonPort != 0 { if flags.daemonHost != "" && flags.daemonPort != 0 {

View file

@ -0,0 +1,22 @@
package service_definition
import (
"github.com/goccy/go-yaml"
"os"
)
func GetServiceDefinitionFromFile(path string) (ServiceDefinition, error) {
var definition ServiceDefinition
defData, err := os.ReadFile(path)
if err != nil {
return ServiceDefinition{}, &FileDoesNotExistError{Message: "Could not find service configuration file", ExpectedPath: path}
}
if err = yaml.Unmarshal(defData, &definition); err != nil {
return ServiceDefinition{}, &InvalidServiceDefinitionError{Path: path}
}
return definition, nil
}

View file

@ -1,11 +1,5 @@
package service_definition package service_definition
import (
"os"
"github.com/goccy/go-yaml"
)
type BuildImage struct { type BuildImage struct {
Path string `yaml:"path"` Path string `yaml:"path"`
TagPrefix string `yaml:"tag"` TagPrefix string `yaml:"tag"`
@ -50,18 +44,6 @@ type ServiceDefinition struct {
Ports []PortMapping `yaml:"ports"` Ports []PortMapping `yaml:"ports"`
} }
func GetServiceDefinitionFromFile(path string) (ServiceDefinition, error) { func GetServiceDefinition(path string) (ServiceDefinition, error) {
var definition ServiceDefinition return GetServiceDefinitionFromFile(path)
defData, err := os.ReadFile(path)
if err != nil {
return ServiceDefinition{}, &FileDoesNotExistError{Message: "Could not find service configuration file", ExpectedPath: path}
}
if err = yaml.Unmarshal(defData, &definition); err != nil {
return ServiceDefinition{}, &InvalidServiceDefinitionError{Path: path}
}
return definition, nil
} }