43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package service_definition
|
|
|
|
import (
|
|
"github.com/goccy/go-yaml"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// 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)
|
|
|
|
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
|
|
}
|