From 1144482f05963e508412a8dfc14d8e8b6f400b56 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Mon, 12 Oct 2020 11:05:19 +0900 Subject: [PATCH 1/4] feat: support to import phases --- pkg/cli/run.go | 33 +++++++++++++++++++++++++++++++++ pkg/config/config.go | 1 + pkg/controller/controller.go | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pkg/cli/run.go b/pkg/cli/run.go index b55115c..439ff6d 100644 --- a/pkg/cli/run.go +++ b/pkg/cli/run.go @@ -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 { @@ -31,6 +32,32 @@ 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) { + phases := []config.Phase{} + for _, phase := range cfg.Phases { + if phase.Import == "" { + phases = append(phases, phase) + continue + } + p := phase.Import + if !filepath.IsAbs(p) { + p = filepath.Join(wd, p) + } + arr := []config.Phase{} + file, err := os.Open(p) + if err != nil { + return cfg, err + } + defer file.Close() + if err := yaml.NewDecoder(file).Decode(&arr); err != nil { + return cfg, err + } + phases = append(phases, arr...) + } + cfg.Phases = phases + return cfg, nil +} + func (runner Runner) action(c *cli.Context) error { wd, err := os.Getwd() if err != nil { @@ -44,6 +71,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 { diff --git a/pkg/config/config.go b/pkg/config/config.go index 681602a..51ac720 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -12,6 +12,7 @@ type Phase struct { Tasks []Task Condition PhaseCondition Meta map[string]interface{} + Import string } type PhaseCondition struct { diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index ea968ec..b1678a9 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -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 } From ac181815d2768d087bc3be9e409e1eb8ce349ef4 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Mon, 12 Oct 2020 11:18:14 +0900 Subject: [PATCH 2/4] feat: support to task import --- pkg/cli/run.go | 69 ++++++++++++++++++++++++++++++++++++++++------ pkg/config/task.go | 1 + 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/pkg/cli/run.go b/pkg/cli/run.go index 439ff6d..f73cf45 100644 --- a/pkg/cli/run.go +++ b/pkg/cli/run.go @@ -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 @@ -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 diff --git a/pkg/config/task.go b/pkg/config/task.go index fb8bcf3..113b8dc 100644 --- a/pkg/config/task.go +++ b/pkg/config/task.go @@ -24,6 +24,7 @@ type Task struct { Meta map[string]interface{} Output Script Input Script + Import string } type WriteFile struct { From 119e95bbf8d794bb80903c427c764443d1ba44db Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Mon, 12 Oct 2020 11:18:31 +0900 Subject: [PATCH 3/4] ci: disable golint --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 4883ba8..50bd72a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,3 +8,4 @@ linters: - godot - nlreturn - godox + - golint From 4c3beb4952df56bc6c8d14aaff157d47ef98d72e Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Mon, 12 Oct 2020 11:21:12 +0900 Subject: [PATCH 4/4] docs: update README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 088093b..4743923 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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.