Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.

Commit

Permalink
feat: support to task import
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Oct 12, 2020
1 parent 1144482 commit ac18181
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
69 changes: 61 additions & 8 deletions pkg/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func (runner Runner) setCLIArg(c *cli.Context, cfg config.Config) config.Config
return cfg
}

func (runner Runner) importConfig(cfg config.Config, wd string) (config.Config, error) {
func (runner Runner) importPhaseConfig(cfgPhases []config.Phase, wd string) ([]config.Phase, error) { //nolint:dupl
phases := []config.Phase{}
for _, phase := range cfg.Phases {
for _, phase := range cfgPhases {
if phase.Import == "" {
phases = append(phases, phase)
continue
Expand All @@ -43,16 +43,69 @@ func (runner Runner) importConfig(cfg config.Config, wd string) (config.Config,
if !filepath.IsAbs(p) {
p = filepath.Join(wd, p)
}
arr := []config.Phase{}
file, err := os.Open(p)
arr, err := func() ([]config.Phase, error) {
arr := []config.Phase{}
file, err := os.Open(p)
if err != nil {
return nil, err
}
defer file.Close()
if err := yaml.NewDecoder(file).Decode(&arr); err != nil {
return nil, err
}
return arr, nil
}()
if err != nil {
return cfg, err
return phases, err
}
phases = append(phases, arr...)
}
return phases, nil
}

func (runner Runner) importTaskConfig(cfgTasks []config.Task, wd string) ([]config.Task, error) { //nolint:dupl
tasks := []config.Task{}
for _, task := range cfgTasks {
if task.Import == "" {
tasks = append(tasks, task)
continue
}
p := task.Import
if !filepath.IsAbs(p) {
p = filepath.Join(wd, p)
}
defer file.Close()
if err := yaml.NewDecoder(file).Decode(&arr); err != nil {
arr, err := func() ([]config.Task, error) {
arr := []config.Task{}
file, err := os.Open(p)
if err != nil {
return nil, err
}
defer file.Close()
if err := yaml.NewDecoder(file).Decode(&arr); err != nil {
return nil, err
}
return arr, nil
}()
if err != nil {
return nil, err
}
tasks = append(tasks, arr...)
}
return tasks, nil
}

func (runner Runner) importConfig(cfg config.Config, wd string) (config.Config, error) {
phases, err := runner.importPhaseConfig(cfg.Phases, wd)
if err != nil {
return cfg, err
}
for i, phase := range phases {
tasks, err := runner.importTaskConfig(phase.Tasks, wd)
if err != nil {
return cfg, err
}
phases = append(phases, arr...)
phase.Tasks = tasks
phases[i] = phase
}
cfg.Phases = phases
return cfg, nil
Expand Down
1 change: 1 addition & 0 deletions pkg/config/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Task struct {
Meta map[string]interface{}
Output Script
Input Script
Import string
}

type WriteFile struct {
Expand Down

0 comments on commit ac18181

Please sign in to comment.