From c2bce670432c03f3ee5e806199cf9c42661ddbe0 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Mon, 12 Oct 2020 13:58:12 +0900 Subject: [PATCH] feat: support to read output from a file --- pkg/config/task.go | 1 + pkg/controller/controller.go | 38 +++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/pkg/config/task.go b/pkg/config/task.go index 1054234..071fe2e 100644 --- a/pkg/config/task.go +++ b/pkg/config/task.go @@ -25,6 +25,7 @@ type Task struct { Output Script Input Script InputFile string `yaml:"input_file"` + OutputFile string `yaml:"output_file"` Import string } diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 3f626d9..646d85c 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -389,6 +389,25 @@ func (ctrl Controller) readTemplateFile(p, wd string, tpl *config.Template) erro return nil } +func (ctrl Controller) readScript(p, wd string, scr *config.Script) error { + if p == "" { + return nil + } + if !filepath.IsAbs(p) { + p = filepath.Join(wd, p) + } + result, err := ctrl.FileReader.Read(p) + if err != nil { + return err + } + if prog, err := expr.New(result.Text); err != nil { + return err + } else { + scr.Prog = prog + } + return nil +} + func (ctrl Controller) ReadExternalFiles(ctx context.Context, wd string) error { //nolint:gocognit for i, phase := range ctrl.Config.Phases { for j, task := range phase.Tasks { @@ -421,20 +440,11 @@ func (ctrl Controller) ReadExternalFiles(ctx context.Context, wd string) error { return err } } - if task.InputFile != "" { - p := task.InputFile - if !filepath.IsAbs(p) { - p = filepath.Join(wd, p) - } - result, err := ctrl.FileReader.Read(p) - if err != nil { - return err - } - if prog, err := expr.New(result.Text); err != nil { - return err - } else { - task.Input.Prog = prog - } + if err := ctrl.readScript(task.InputFile, wd, &task.Input); err != nil { + return err + } + if err := ctrl.readScript(task.OutputFile, wd, &task.Output); err != nil { + return err } phase.Tasks[j] = task }