refactor: isolate + inject DefinitionFetcher
This commit is contained in:
parent
8bc33870e6
commit
d6a5587a66
5 changed files with 43 additions and 21 deletions
|
@ -18,7 +18,7 @@ func getBuildCommand() *cobra.Command {
|
|||
if err != nil {
|
||||
return fmt.Errorf("%+v", err)
|
||||
}
|
||||
def, err := service_definition.GetServiceDefinition(pathProvided)
|
||||
def, err := service_definition.NewDefinitionFetcher().GetDefinition(pathProvided)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
||||
|
|
|
@ -49,7 +49,7 @@ func getStartCommand() *cobra.Command {
|
|||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
flags := ctx.Value("flags").(ParsedFlags)
|
||||
def, err := service_definition.GetServiceDefinition(flags.definitionPath)
|
||||
def, err := service_definition.NewDefinitionFetcher().GetDefinition(flags.definitionPath)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read service definition: %+v", err)
|
||||
|
|
|
@ -7,26 +7,60 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// TODO: Extract into own module.
|
||||
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
|
||||
}
|
||||
|
||||
type DefinitionFetcher struct {
|
||||
Git GitClient
|
||||
}
|
||||
|
||||
func NewDefinitionFetcher() DefinitionFetcher {
|
||||
return DefinitionFetcher{
|
||||
Git: Git{},
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add coverage.
|
||||
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 getDefinitionFromGit(path string) (ServiceDefinition, error) {
|
||||
func (f DefinitionFetcher) 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 {
|
||||
if _, err := f.Git.Clone(strings.TrimPrefix(path, "git+"), dir); err != nil {
|
||||
return ServiceDefinition{}, err
|
||||
}
|
||||
|
||||
return getDefinitionFromFile(dir + "/service.yml")
|
||||
return f.getDefinitionFromFile(dir + "/service.yml")
|
||||
}
|
||||
|
||||
// Extracts a service definition from the given filepath.
|
||||
func getDefinitionFromFile(path string) (ServiceDefinition, error) {
|
||||
func (f DefinitionFetcher) getDefinitionFromFile(path string) (ServiceDefinition, error) {
|
||||
var definition ServiceDefinition
|
||||
|
||||
defData, err := os.ReadFile(path)
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
package service_definition
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type BuildImage struct {
|
||||
Path string `yaml:"path"`
|
||||
TagPrefix string `yaml:"tag"`
|
||||
|
@ -47,11 +43,3 @@ type ServiceDefinition struct {
|
|||
Containers []ContainerDefinition `yaml:"containers"`
|
||||
Ports []PortMapping `yaml:"ports"`
|
||||
}
|
||||
|
||||
func GetServiceDefinition(path string) (ServiceDefinition, error) {
|
||||
if strings.HasPrefix(path, "git+") {
|
||||
return getDefinitionFromGit(path)
|
||||
}
|
||||
|
||||
return getDefinitionFromFile(path)
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
)
|
||||
|
||||
func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) {
|
||||
_, err := GetServiceDefinition(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