spud/service_definition/main.go

56 lines
1.4 KiB
Go
Raw Normal View History

2024-06-02 06:03:47 +00:00
package service_definition
import (
"os"
2024-06-02 06:03:47 +00:00
"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"`
2024-07-07 01:15:02 +00:00
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"`
}
2024-06-02 06:03:47 +00:00
func GetServiceDefinitionFromFile(path string) (ServiceDefinition, error) {
var definition ServiceDefinition
defData, err := os.ReadFile(path)
if err != nil {
2024-06-02 06:03:47 +00:00
return ServiceDefinition{}, &FileDoesNotExistError{Message: "Could not find service configuration file", ExpectedPath: path}
}
if err = yaml.Unmarshal(defData, &definition); err != nil {
2024-06-02 06:03:47 +00:00
return ServiceDefinition{}, &InvalidServiceDefinitionError{Path: path}
}
2024-06-02 06:03:47 +00:00
return definition, nil
}