From 8bc33870e6df278a8102133e655da0e822e73600 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 10 Nov 2024 10:52:06 -0500 Subject: [PATCH] feat: fetch service def from git --- cli/build.go | 2 +- service_definition/fetcher.go | 23 ++++++++++++++++++- service_definition/main.go | 10 +++++++- service_definition/service_definition_test.go | 2 +- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/cli/build.go b/cli/build.go index e28f992..8f1ed6b 100644 --- a/cli/build.go +++ b/cli/build.go @@ -18,7 +18,7 @@ func getBuildCommand() *cobra.Command { if err != nil { return fmt.Errorf("%+v", err) } - def, err := service_definition.GetServiceDefinitionFromFile(pathProvided) + def, err := service_definition.GetServiceDefinition(pathProvided) if err != nil { return fmt.Errorf("Failed to read service definition from file: %+v", err) diff --git a/service_definition/fetcher.go b/service_definition/fetcher.go index 4294906..d7de849 100644 --- a/service_definition/fetcher.go +++ b/service_definition/fetcher.go @@ -3,9 +3,30 @@ package service_definition import ( "github.com/goccy/go-yaml" "os" + "os/exec" + "strings" ) -func GetServiceDefinitionFromFile(path string) (ServiceDefinition, error) { +// Clones the target git repository and uses it as a basis to extract +// a service definition. +func getDefinitionFromGit(path string) (ServiceDefinition, error) { + dir, err := os.MkdirTemp("/tmp", "spud-service-") + + if err != nil { + return ServiceDefinition{}, err + } + + cloneCmd := exec.Command("git", "clone", strings.TrimPrefix(path, "git+"), dir) + + if err := cloneCmd.Run(); err != nil { + return ServiceDefinition{}, err + } + + return getDefinitionFromFile(dir + "/service.yml") +} + +// Extracts a service definition from the given filepath. +func getDefinitionFromFile(path string) (ServiceDefinition, error) { var definition ServiceDefinition defData, err := os.ReadFile(path) diff --git a/service_definition/main.go b/service_definition/main.go index 301ceb5..7b57423 100644 --- a/service_definition/main.go +++ b/service_definition/main.go @@ -1,5 +1,9 @@ package service_definition +import ( + "strings" +) + type BuildImage struct { Path string `yaml:"path"` TagPrefix string `yaml:"tag"` @@ -45,5 +49,9 @@ type ServiceDefinition struct { } func GetServiceDefinition(path string) (ServiceDefinition, error) { - return GetServiceDefinitionFromFile(path) + if strings.HasPrefix(path, "git+") { + return getDefinitionFromGit(path) + } + + return getDefinitionFromFile(path) } diff --git a/service_definition/service_definition_test.go b/service_definition/service_definition_test.go index 5363be3..a63b02a 100644 --- a/service_definition/service_definition_test.go +++ b/service_definition/service_definition_test.go @@ -5,7 +5,7 @@ import ( ) func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) { - _, err := GetServiceDefinitionFromFile(t.TempDir() + "/not-a-file.yml") + _, err := GetServiceDefinition(t.TempDir() + "/not-a-file.yml") if err == nil { t.Errorf("Expected error, got nil.")