From c580c0c433709bd02cd8e1c03d2a048bd4b6936b Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 10 Nov 2024 10:37:46 -0500 Subject: [PATCH] refactor: generalize service definition fetch --- cli/start_service.go | 4 ++-- service_definition/fetcher.go | 22 ++++++++++++++++++++++ service_definition/main.go | 22 ++-------------------- 3 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 service_definition/fetcher.go diff --git a/cli/start_service.go b/cli/start_service.go index b695f86..29736a8 100644 --- a/cli/start_service.go +++ b/cli/start_service.go @@ -49,10 +49,10 @@ func getStartCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() flags := ctx.Value("flags").(ParsedFlags) - def, err := service_definition.GetServiceDefinitionFromFile(flags.definitionPath) + def, err := service_definition.GetServiceDefinition(flags.definitionPath) 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 { diff --git a/service_definition/fetcher.go b/service_definition/fetcher.go new file mode 100644 index 0000000..4294906 --- /dev/null +++ b/service_definition/fetcher.go @@ -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 +} diff --git a/service_definition/main.go b/service_definition/main.go index 1557f16..301ceb5 100644 --- a/service_definition/main.go +++ b/service_definition/main.go @@ -1,11 +1,5 @@ package service_definition -import ( - "os" - - "github.com/goccy/go-yaml" -) - type BuildImage struct { Path string `yaml:"path"` TagPrefix string `yaml:"tag"` @@ -50,18 +44,6 @@ type ServiceDefinition struct { Ports []PortMapping `yaml:"ports"` } -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 +func GetServiceDefinition(path string) (ServiceDefinition, error) { + return GetServiceDefinitionFromFile(path) }