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

Commit

Permalink
Merge pull request #47 from suzuki-shunsuke/feat/import-phase
Browse files Browse the repository at this point in the history
feat: import phases and tasks
  • Loading branch information
suzuki-shunsuke authored Oct 12, 2020
2 parents 6020a88 + 4c3beb4 commit c11e3c5
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ linters:
- godot
- nlreturn
- godox
- golint
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ condtion:
# The list of phases.
# Phases are run not in parallel but sequentially.
phases:
# import a list of phases from a file.
- import: phases.yaml
# The phase name. This must be unique and static.
- name: init
# The meta attributes of the phase.
Expand All @@ -381,6 +383,8 @@ phases:
service: foo
# the list of tasks.
tasks:
# import a list of tasks from a file.
- import: tasks.yaml
# The task name. The value is parsed by text/template.
- name: foo
# a tengo script which represents task's input.
Expand Down
86 changes: 86 additions & 0 deletions pkg/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/suzuki-shunsuke/buildflow/pkg/github"
"github.com/suzuki-shunsuke/go-findconfig/findconfig"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
)

func (runner Runner) setCLIArg(c *cli.Context, cfg config.Config) config.Config {
Expand All @@ -31,6 +32,85 @@ func (runner Runner) setCLIArg(c *cli.Context, cfg config.Config) config.Config
return cfg
}

func (runner Runner) importPhaseConfig(cfgPhases []config.Phase, wd string) ([]config.Phase, error) { //nolint:dupl
phases := []config.Phase{}
for _, phase := range cfgPhases {
if phase.Import == "" {
phases = append(phases, phase)
continue
}
p := phase.Import
if !filepath.IsAbs(p) {
p = filepath.Join(wd, 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 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)
}
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
}
phase.Tasks = tasks
phases[i] = phase
}
cfg.Phases = phases
return cfg, nil
}

func (runner Runner) action(c *cli.Context) error {
wd, err := os.Getwd()
if err != nil {
Expand All @@ -44,6 +124,12 @@ func (runner Runner) action(c *cli.Context) error {
return err
}

if c, err := runner.importConfig(cfg, wd); err != nil {
return err
} else {
cfg = c
}

cfg = runner.setCLIArg(c, cfg)
cfg, err = config.Set(cfg)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Phase struct {
Tasks []Task
Condition PhaseCondition
Meta map[string]interface{}
Import string
}

type PhaseCondition struct {
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
2 changes: 1 addition & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (ctrl Controller) runPhase(ctx context.Context, params Params, idx int, wd

if p, f := ctrl.checkSkipPhase(params, phase, phaseCfg); f {
return phase, nil
} else { //nolint:golint
} else {
phase = p
}

Expand Down

0 comments on commit c11e3c5

Please sign in to comment.