From a811dd8927fb013a9c8c4e26d75103ed4e3bf1be Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 2 Jun 2024 01:21:25 -0400 Subject: [PATCH] refactor(service): split service-related logic into module --- main.go | 67 --------------------------------------------------- service.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 67 deletions(-) create mode 100644 service.go diff --git a/main.go b/main.go index 5faf6e8..f374234 100644 --- a/main.go +++ b/main.go @@ -1,76 +1,9 @@ package main import ( - "github.com/goccy/go-yaml" "github.com/spf13/cobra" - "log" - "os" ) -type VolumeDefinition struct { - Name string `yaml:"name"` -} - -type VolumeConfiguration struct { - Name string `yaml:"name"` - Container string `yaml:"container"` - Host string `yaml:"host"` -} - -type ContainerDefinition struct { - Name string `yaml:"name"` - Image string `yaml:"image"` - Volumes []VolumeConfiguration `yaml:"volumes"` - ExtraArgs []string `yaml:"extra-args"` -} - -type ServiceDefinition struct { - Name string `yaml:"name"` - Volumes []VolumeDefinition `yaml:"volumes"` - Containers []ContainerDefinition `yaml:"containers"` -} - -func GetServiceDefinitionFromFile(path string) ServiceDefinition { - var definition ServiceDefinition - - defData, err := os.ReadFile(path) - - // TODO: Bubble up error? - if err != nil { - log.Fatalf("Could not parse service configuration at %s: %s", path, err) - } - - if err = yaml.Unmarshal(defData, &definition); err != nil { - log.Fatalf("Could not unpack service configuration: %s", err) - } - - return definition -} - -func CreateService(definition ServiceDefinition) { - var err error - - err = CreatePod(definition.Name) - - if err != nil { - log.Fatalf("%s", err) - } - - knownVolumes := map[string]string{} - - for _, volume := range definition.Volumes { - namespacedName := definition.Name + "_" + volume.Name - CreateVolume(namespacedName, true) - knownVolumes[volume.Name] = namespacedName - } - - for _, container := range definition.Containers { - if err = CreateContainer(container, knownVolumes, definition.Name); err != nil { - log.Fatalf("%s", err) - } - } -} - var cli = &cobra.Command{ Use: "spud", Short: "A not-entirely-terrible-way to manage self-hosted services.", diff --git a/service.go b/service.go new file mode 100644 index 0000000..495c615 --- /dev/null +++ b/service.go @@ -0,0 +1,71 @@ +package main + +import ( + "github.com/goccy/go-yaml" + "log" + "os" +) + +type VolumeDefinition struct { + Name string `yaml:"name"` +} + +type VolumeConfiguration struct { + Name string `yaml:"name"` + Container string `yaml:"container"` + Host string `yaml:"host"` +} + +type ContainerDefinition struct { + Name string `yaml:"name"` + Image string `yaml:"image"` + Volumes []VolumeConfiguration `yaml:"volumes"` + ExtraArgs []string `yaml:"extra-args"` +} + +type ServiceDefinition struct { + Name string `yaml:"name"` + Volumes []VolumeDefinition `yaml:"volumes"` + Containers []ContainerDefinition `yaml:"containers"` +} + +func GetServiceDefinitionFromFile(path string) ServiceDefinition { + var definition ServiceDefinition + + defData, err := os.ReadFile(path) + + // TODO: Bubble up error? + if err != nil { + log.Fatalf("Could not parse service configuration at %s: %s", path, err) + } + + if err = yaml.Unmarshal(defData, &definition); err != nil { + log.Fatalf("Could not unpack service configuration: %s", err) + } + + return definition +} + +func CreateService(definition ServiceDefinition) { + var err error + + err = CreatePod(definition.Name) + + if err != nil { + log.Fatalf("%s", err) + } + + knownVolumes := map[string]string{} + + for _, volume := range definition.Volumes { + namespacedName := definition.Name + "_" + volume.Name + CreateVolume(namespacedName, true) + knownVolumes[volume.Name] = namespacedName + } + + for _, container := range definition.Containers { + if err = CreateContainer(container, knownVolumes, definition.Name); err != nil { + log.Fatalf("%s", err) + } + } +}