refactor: generalize service definition fetch
This commit is contained in:
parent
24e795e1aa
commit
c580c0c433
3 changed files with 26 additions and 22 deletions
|
@ -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 {
|
||||
|
|
22
service_definition/fetcher.go
Normal file
22
service_definition/fetcher.go
Normal 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
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue