courgette/internal/workflow/create.go

43 lines
923 B
Go
Raw Normal View History

package workflow
import (
"gopkg.in/yaml.v3"
"io/ioutil"
)
// Builds a Workflow from serialized yaml provided as bytes.
//
// If the bytes cannot be unmarshalled into the Workflow, an error
// is returned along with a nil Workflow pointer.
func FromYamlBytes(raw []byte) (*Workflow, error) {
var workflow Workflow
if yamlError := yaml.Unmarshal(raw, &workflow); yamlError != nil {
return nil, yamlError
}
return &workflow, nil
}
// Builds a Workflow from the contents of the given file.
//
// If the file cannot be read, or its content cannot be parsed, an
// error is returned along with a nil Workflow pointer.
func FromYamlFile(workflowPath string) (*Workflow, error) {
workflowRaw, err := ioutil.ReadFile(workflowPath)
if err != nil {
return nil, err
}
workflow, err := FromYamlBytes(workflowRaw)
if err != nil {
return nil, err
}
workflow.SourcePath = workflowPath
return workflow, err
}