feat/fetch-definition-from-remote #6
7 changed files with 157 additions and 26 deletions
|
@ -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.NewDefinitionFetcher().GetDefinition(pathProvided)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
||||
|
|
|
@ -49,10 +49,10 @@ func getStartCommand() *cobra.Command {
|
|||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
flags := ctx.Value("flags").(ParsedFlags)
|
||||
def, err := service_definition.GetServiceDefinitionFromFile(flags.definitionPath)
|
||||
def, err := service_definition.NewDefinitionFetcher().GetDefinition(flags.definitionPath)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
||||
return fmt.Errorf("Failed to read service definition: %+v", err)
|
||||
}
|
||||
|
||||
if flags.daemonHost != "" && flags.daemonPort != 0 {
|
||||
|
|
26
git/main.go
Normal file
26
git/main.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
// Git wrapper
|
||||
//
|
||||
// Facilitates the usage of `git` commands when dealing with
|
||||
// data living in repositories.
|
||||
|
||||
package git
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type GitClient interface {
|
||||
Clone(path string, destination string) (string, error)
|
||||
}
|
||||
|
||||
type Git struct{}
|
||||
|
||||
func (g Git) Clone(path string, destination string) (string, error) {
|
||||
cloneCmd := exec.Command("git", "clone", path, destination)
|
||||
|
||||
if err := cloneCmd.Run(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return path, nil
|
||||
}
|
69
service_definition/fetcher.go
Normal file
69
service_definition/fetcher.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
// Definition fetcher
|
||||
//
|
||||
// Handles fetching and building ServiceDefinition structs from different
|
||||
// data sources.
|
||||
|
||||
package service_definition
|
||||
|
||||
import (
|
||||
"github.com/goccy/go-yaml"
|
||||
"os"
|
||||
git "spud/git"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DefinitionFetcher struct {
|
||||
Git git.GitClient
|
||||
}
|
||||
|
||||
func NewDefinitionFetcher() DefinitionFetcher {
|
||||
return DefinitionFetcher{
|
||||
Git: git.Git{},
|
||||
}
|
||||
}
|
||||
|
||||
// Gets a ServiceDefinition from the given location.
|
||||
//
|
||||
// Depending on location prefix, different sources are used:
|
||||
// git+: Clones the target git repository and extracts a service definition from it.
|
||||
// <no-prefix>: Uses the location as a filepath to the service definition.
|
||||
func (f DefinitionFetcher) GetDefinition(path string) (ServiceDefinition, error) {
|
||||
if strings.HasPrefix(path, "git+") {
|
||||
return f.getDefinitionFromGit(path)
|
||||
}
|
||||
|
||||
return f.getDefinitionFromFile(path)
|
||||
}
|
||||
|
||||
// Clones the target git repository and uses it as a basis to extract
|
||||
// a service definition.
|
||||
func (f DefinitionFetcher) getDefinitionFromGit(path string) (ServiceDefinition, error) {
|
||||
dir, err := os.MkdirTemp("/tmp", "spud-service-")
|
||||
|
||||
if err != nil {
|
||||
return ServiceDefinition{}, err
|
||||
}
|
||||
|
||||
if _, err := f.Git.Clone(strings.TrimPrefix(path, "git+"), dir); err != nil {
|
||||
return ServiceDefinition{}, err
|
||||
}
|
||||
|
||||
return f.getDefinitionFromFile(dir + "/service.yml")
|
||||
}
|
||||
|
||||
// Extracts a service definition from the given filepath.
|
||||
func (f DefinitionFetcher) getDefinitionFromFile(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
|
||||
}
|
58
service_definition/fetcher_test.go
Normal file
58
service_definition/fetcher_test.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package service_definition
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type MockGit struct {
|
||||
calls []string
|
||||
}
|
||||
|
||||
func (g *MockGit) Clone(path string, destination string) (string, error) {
|
||||
g.calls = append(g.calls, path+":"+destination)
|
||||
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func TestGetDefinitionDetectsGitPathPrefix(t *testing.T) {
|
||||
fetcher := NewDefinitionFetcher()
|
||||
|
||||
mockGit := MockGit{}
|
||||
fetcher.Git = &mockGit
|
||||
|
||||
fetcher.GetDefinition("git+https://git.com/owner/repo.git")
|
||||
|
||||
if len(mockGit.calls) == 0 {
|
||||
t.Errorf("Expected at least one call to git, got none.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDefinitionDefaultsToFilePathIfNoPrefix(t *testing.T) {
|
||||
defPath := t.TempDir() + "/service.yml"
|
||||
|
||||
os.WriteFile(defPath, []byte("name: test-service"), 0755)
|
||||
|
||||
fetcher := NewDefinitionFetcher()
|
||||
|
||||
def, _ := fetcher.GetDefinition(defPath)
|
||||
|
||||
if def.Name != "test-service" {
|
||||
t.Errorf("Expected mock service name to be 'test-service', got %s instead.", def.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDefinitionGetDefinitionFromGit(t *testing.T) {
|
||||
fetcher := NewDefinitionFetcher()
|
||||
|
||||
mockGit := MockGit{}
|
||||
fetcher.Git = &mockGit
|
||||
|
||||
mockUrl := "https://git.com/owner/repo.git"
|
||||
fetcher.GetDefinition("git+" + mockUrl)
|
||||
|
||||
if !strings.HasPrefix(mockGit.calls[0], mockUrl) {
|
||||
t.Errorf("Expected git cloning for %s, got %s instead.", mockUrl, mockGit.calls[0])
|
||||
}
|
||||
}
|
|
@ -1,11 +1,5 @@
|
|||
package service_definition
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/goccy/go-yaml"
|
||||
)
|
||||
|
||||
type BuildImage struct {
|
||||
Path string `yaml:"path"`
|
||||
TagPrefix string `yaml:"tag"`
|
||||
|
@ -49,19 +43,3 @@ type ServiceDefinition struct {
|
|||
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
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
)
|
||||
|
||||
func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) {
|
||||
_, err := GetServiceDefinitionFromFile(t.TempDir() + "/not-a-file.yml")
|
||||
_, err := NewDefinitionFetcher().GetDefinition(t.TempDir() + "/not-a-file.yml")
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("Expected error, got nil.")
|
||||
|
|
Loading…
Reference in a new issue