feat: fetch service def from git
This commit is contained in:
parent
c580c0c433
commit
8bc33870e6
4 changed files with 33 additions and 4 deletions
|
@ -18,7 +18,7 @@ func getBuildCommand() *cobra.Command {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%+v", err)
|
return fmt.Errorf("%+v", err)
|
||||||
}
|
}
|
||||||
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
|
def, err := service_definition.GetServiceDefinition(pathProvided)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
||||||
|
|
|
@ -3,9 +3,30 @@ package service_definition
|
||||||
import (
|
import (
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"os"
|
"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
|
var definition ServiceDefinition
|
||||||
|
|
||||||
defData, err := os.ReadFile(path)
|
defData, err := os.ReadFile(path)
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
package service_definition
|
package service_definition
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type BuildImage struct {
|
type BuildImage struct {
|
||||||
Path string `yaml:"path"`
|
Path string `yaml:"path"`
|
||||||
TagPrefix string `yaml:"tag"`
|
TagPrefix string `yaml:"tag"`
|
||||||
|
@ -45,5 +49,9 @@ type ServiceDefinition struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetServiceDefinition(path string) (ServiceDefinition, error) {
|
func GetServiceDefinition(path string) (ServiceDefinition, error) {
|
||||||
return GetServiceDefinitionFromFile(path)
|
if strings.HasPrefix(path, "git+") {
|
||||||
|
return getDefinitionFromGit(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
return getDefinitionFromFile(path)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) {
|
func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) {
|
||||||
_, err := GetServiceDefinitionFromFile(t.TempDir() + "/not-a-file.yml")
|
_, err := GetServiceDefinition(t.TempDir() + "/not-a-file.yml")
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected error, got nil.")
|
t.Errorf("Expected error, got nil.")
|
||||||
|
|
Loading…
Reference in a new issue