From e51648801f2028d2af38687d3ca6bc5c438131e6 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 2 Jun 2024 02:03:47 -0400 Subject: [PATCH] refactor: split into modules --- main.go | 13 ++++++- podman.go => podman/main.go | 6 ++- service/service.go | 32 +++++++++++++++ service_definition/errors.go | 24 ++++++++++++ service.go => service_definition/main.go | 39 ++++--------------- service_definition/service_definition_test.go | 13 +++++++ 6 files changed, 91 insertions(+), 36 deletions(-) rename podman.go => podman/main.go (91%) create mode 100644 service/service.go create mode 100644 service_definition/errors.go rename service.go => service_definition/main.go (50%) create mode 100644 service_definition/service_definition_test.go diff --git a/main.go b/main.go index f374234..3b9e98d 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,10 @@ package main import ( "github.com/spf13/cobra" + "log" + + service "spud/service" + service_definition "spud/service_definition" ) var cli = &cobra.Command{ @@ -14,8 +18,13 @@ var start = &cobra.Command{ Short: "Creates or updates a service based on the provided definition.", Run: func(cmd *cobra.Command, args []string) { pathProvided := args[0] - def := GetServiceDefinitionFromFile(pathProvided) - CreateService(def) + def, err := service_definition.GetServiceDefinitionFromFile(pathProvided) + + if err != nil { + log.Fatal(err) + } + + service.CreateService(def) }, } diff --git a/podman.go b/podman/main.go similarity index 91% rename from podman.go rename to podman/main.go index 3db9602..8bd07a7 100644 --- a/podman.go +++ b/podman/main.go @@ -1,8 +1,10 @@ -package main +package podman import ( "log" "os/exec" + + "spud/service_definition" ) /* @@ -53,7 +55,7 @@ func CreatePod(name string) error { /* * Creates individual containers. */ -func CreateContainer(definition ContainerDefinition, knownVolumes map[string]string, service string) error { +func CreateContainer(definition service_definition.ContainerDefinition, knownVolumes map[string]string, service string) error { namespacedContainerName := service + "_" + definition.Name args := []string{ diff --git a/service/service.go b/service/service.go new file mode 100644 index 0000000..2a3100f --- /dev/null +++ b/service/service.go @@ -0,0 +1,32 @@ +package service + +import ( + "log" + + podman "spud/podman" + service_definition "spud/service_definition" +) + +func CreateService(definition service_definition.ServiceDefinition) { + var err error + + err = podman.CreatePod(definition.Name) + + if err != nil { + log.Fatalf("%s", err) + } + + knownVolumes := map[string]string{} + + for _, volume := range definition.Volumes { + namespacedName := definition.Name + "_" + volume.Name + podman.CreateVolume(namespacedName, true) + knownVolumes[volume.Name] = namespacedName + } + + for _, container := range definition.Containers { + if err = podman.CreateContainer(container, knownVolumes, definition.Name); err != nil { + log.Fatalf("%s", err) + } + } +} diff --git a/service_definition/errors.go b/service_definition/errors.go new file mode 100644 index 0000000..45fa4a1 --- /dev/null +++ b/service_definition/errors.go @@ -0,0 +1,24 @@ +package service_definition + +type FileDoesNotExistError struct { + Message string + ExpectedPath string +} + +func (r *FileDoesNotExistError) Error() string { + prefix := "File not found" + + if r.Message != "" { + prefix = r.Message + } + + return prefix + ": " + r.ExpectedPath +} + +type InvalidServiceDefinitionError struct { + Path string +} + +func (r *InvalidServiceDefinitionError) Error() string { + return "Service definition does not satisfy expected schema: " + r.Path +} diff --git a/service.go b/service_definition/main.go similarity index 50% rename from service.go rename to service_definition/main.go index 495c615..a3afa1f 100644 --- a/service.go +++ b/service_definition/main.go @@ -1,9 +1,9 @@ -package main +package service_definition import ( - "github.com/goccy/go-yaml" - "log" "os" + + "github.com/goccy/go-yaml" ) type VolumeDefinition struct { @@ -29,43 +29,18 @@ type ServiceDefinition struct { Containers []ContainerDefinition `yaml:"containers"` } -func GetServiceDefinitionFromFile(path string) ServiceDefinition { +func GetServiceDefinitionFromFile(path string) (ServiceDefinition, error) { 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) + return ServiceDefinition{}, &FileDoesNotExistError{Message: "Could not find service configuration file", ExpectedPath: path} } if err = yaml.Unmarshal(defData, &definition); err != nil { - log.Fatalf("Could not unpack service configuration: %s", err) + return ServiceDefinition{}, &InvalidServiceDefinitionError{Path: path} } - 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) - } - } + return definition, nil } diff --git a/service_definition/service_definition_test.go b/service_definition/service_definition_test.go new file mode 100644 index 0000000..5363be3 --- /dev/null +++ b/service_definition/service_definition_test.go @@ -0,0 +1,13 @@ +package service_definition + +import ( + "testing" +) + +func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) { + _, err := GetServiceDefinitionFromFile(t.TempDir() + "/not-a-file.yml") + + if err == nil { + t.Errorf("Expected error, got nil.") + } +}