spud/service_definition/main.go
Marc Cataford 08f068d255
All checks were successful
/ build (push) Successful in 59s
feat: support env-file input
2024-07-06 21:15:02 -04:00

55 lines
1.4 KiB
Go

package service_definition
import (
"os"
"github.com/goccy/go-yaml"
)
type VolumeDefinition struct {
Name string `yaml:"name"`
}
type PortMapping struct {
Host string `yaml:"host"`
Container string `yaml:"container"`
Type string `yaml:"type"`
}
type VolumeConfiguration struct {
Name string `yaml:"name"`
Container string `yaml:"container"`
Host string `yaml:"host"`
ReadOnly bool `yaml:"readonly"`
}
type ContainerDefinition struct {
Name string `yaml:"name"`
Image string `yaml:"image"`
Volumes []VolumeConfiguration `yaml:"volumes"`
EnvFile string `yaml:"env-file"`
ExtraArgs []string `yaml:"extra-args"`
}
type ServiceDefinition struct {
Name string `yaml:"name"`
Volumes []VolumeDefinition `yaml:"volumes"`
Containers []ContainerDefinition `yaml:"containers"`
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
}