courgette/internal/workflow/create.go

52 lines
1.1 KiB
Go

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
}
// If not "name" prop is provided, it defaults to the
// key that is used in the jobs object.
for jobLabel, job := range workflow.Jobs {
if job.Name == "" {
job.Name = jobLabel
}
workflow.Jobs[jobLabel] = job
}
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
}