support list/map/scalar for on and needs

Signed-off-by: Casey Lee <cplee@nektos.com>
This commit is contained in:
Casey Lee
2020-02-10 16:35:00 -08:00
parent 0582306861
commit ac8258db4b
35 changed files with 2454 additions and 1696 deletions

View File

@@ -6,21 +6,45 @@ import (
"regexp"
"strings"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)
// Workflow is the structure of the files in .github/workflows
type Workflow struct {
Name string `yaml:"name"`
On string `yaml:"on"`
Env map[string]string `yaml:"env"`
Jobs map[string]*Job `yaml:"jobs"`
Name string `yaml:"name"`
RawOn yaml.Node `yaml:"on"`
Env map[string]string `yaml:"env"`
Jobs map[string]*Job `yaml:"jobs"`
}
// On events for the workflow
func (w *Workflow) On() []string {
switch w.RawOn.Kind {
case yaml.ScalarNode:
var val string
w.RawOn.Decode(&val)
return []string{val}
case yaml.SequenceNode:
var val []string
w.RawOn.Decode(&val)
return val
case yaml.MappingNode:
var val map[string]interface{}
w.RawOn.Decode(&val)
var keys []string
for k := range val {
keys = append(keys, k)
}
return keys
}
return nil
}
// Job is the structure of one job in a workflow
type Job struct {
Name string `yaml:"name"`
Needs []string `yaml:"needs"`
RawNeeds yaml.Node `yaml:"needs"`
RunsOn string `yaml:"runs-on"`
Env map[string]string `yaml:"env"`
If string `yaml:"if"`
@@ -30,6 +54,22 @@ type Job struct {
Services map[string]*ContainerSpec `yaml:"services"`
}
// Needs list for Job
func (j *Job) Needs() []string {
switch j.RawNeeds.Kind {
case yaml.ScalarNode:
var val string
j.RawNeeds.Decode(&val)
return []string{val}
case yaml.SequenceNode:
var val []string
j.RawNeeds.Decode(&val)
return val
}
return nil
}
// ContainerSpec is the specification of the container to use for the job
type ContainerSpec struct {
Image string `yaml:"image"`