diff --git a/pkg/config/item.go b/pkg/config/item.go index a7f59eb..5712cf8 100644 --- a/pkg/config/item.go +++ b/pkg/config/item.go @@ -1,6 +1,9 @@ package config -import "github.com/suzuki-shunsuke/buildflow/pkg/expr" +import ( + "github.com/suzuki-shunsuke/buildflow/pkg/expr" + "github.com/suzuki-shunsuke/go-convmap/convmap" +) type Items struct { Items interface{} @@ -19,3 +22,29 @@ func (items Items) Run(params map[string]interface{}) (interface{}, error) { } return a, nil } + +func (items *Items) UnmarshalYAML(unmarshal func(interface{}) error) error { + var src interface{} + if err := unmarshal(&src); err != nil { + return err + } + switch t := src.(type) { + case string: + prog, err := expr.New(t) + if err != nil { + return err + } + items.Program = prog + return nil + default: + if t == nil { + return nil + } + a, err := convmap.Convert(t) + if err != nil { + return err + } + items.Items = a + return nil + } +} diff --git a/pkg/config/task.go b/pkg/config/task.go index 4f548f6..5d59635 100644 --- a/pkg/config/task.go +++ b/pkg/config/task.go @@ -5,30 +5,27 @@ import ( "github.com/suzuki-shunsuke/buildflow/pkg/constant" "github.com/suzuki-shunsuke/buildflow/pkg/execute" - "github.com/suzuki-shunsuke/buildflow/pkg/expr" - "github.com/suzuki-shunsuke/go-convmap/convmap" ) type Task struct { - Name Template - Type string `yaml:"-"` - When Bool - WhenFile string `yaml:"when_file"` - Dependency Dependency - Command Command - ReadFile ReadFile `yaml:"read_file"` - WriteFile WriteFile `yaml:"write_file"` - HTTP HTTP - Timeout execute.Timeout - Items interface{} - Item Item `yaml:"-"` - CompiledItems Items - Meta map[string]interface{} - Output Script - Input Script - InputFile string `yaml:"input_file"` - OutputFile string `yaml:"output_file"` - Import string + Name Template + Type string `yaml:"-"` + When Bool + WhenFile string `yaml:"when_file"` + Dependency Dependency + Command Command + ReadFile ReadFile `yaml:"read_file"` + WriteFile WriteFile `yaml:"write_file"` + HTTP HTTP + Timeout execute.Timeout + Items Items + Item Item `yaml:"-"` + Meta map[string]interface{} + Output Script + Input Script + InputFile string `yaml:"input_file"` + OutputFile string `yaml:"output_file"` + Import string } type WriteFile struct { @@ -42,22 +39,6 @@ func (task *Task) Set() error { return err } - if s, ok := task.Items.(string); ok { - prog, err := expr.New(s) - if err != nil { - return err - } - task.CompiledItems = Items{ - Program: prog, - } - } else if task.Items != nil { - a, err := convmap.Convert(task.Items) - if err != nil { - return err - } - task.Items = a - } - if err := convertMeta(task.Meta); err != nil { return err } diff --git a/pkg/controller/expand.go b/pkg/controller/expand.go index ebe318d..a0b94dc 100644 --- a/pkg/controller/expand.go +++ b/pkg/controller/expand.go @@ -73,7 +73,14 @@ func expandItems(task config.Task, items interface{}, templateParams Params) ([] } func Expand(task config.Task, params Params) ([]config.Task, error) { - if task.Items == nil { + if task.Items.Items != nil { + return expandItems(task, task.Items.Items, params) + } + items, err := task.Items.Run(params.ToExpr()) + if err != nil { + return nil, err + } + if items == nil { name, err := task.Name.New(params.ToTemplate()) if err != nil { return nil, err @@ -82,12 +89,5 @@ func Expand(task config.Task, params Params) ([]config.Task, error) { t.Name = name return []config.Task{t}, nil } - if _, ok := task.Items.(string); ok { - items, err := task.CompiledItems.Run(params.ToExpr()) - if err != nil { - return nil, err - } - return expandItems(task, items, params) - } - return expandItems(task, task.Items, params) + return expandItems(task, items, params) }