spud/service_definition/file_definition_fetcher.go

35 lines
833 B
Go
Raw Normal View History

// File Definition fetcher
//
// Handles extracting service definitions from local files.
package service_definition
import (
"fmt"
"github.com/goccy/go-yaml"
"os"
)
type FileDefinitionFetcher struct{}
func NewFileDefinitionFetcher() *FileDefinitionFetcher {
return &FileDefinitionFetcher{}
}
// Retrieves a service definition from the given filepath.
func (f FileDefinitionFetcher) GetDefinition(path string) (ServiceDefinition, error) {
var definition ServiceDefinition
defData, err := os.ReadFile(path)
if err != nil {
return ServiceDefinition{}, fmt.Errorf("Could not find service configuration file: %s", path)
}
if err = yaml.Unmarshal(defData, &definition); err != nil {
return ServiceDefinition{}, fmt.Errorf("Service definition does not satisfy expected schema: %s", path)
}
return definition, nil
}