From 38f4ff8c6e35b47f0c6c011ec6aa0923e48b92db Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Mon, 12 Oct 2020 08:53:16 +0900 Subject: [PATCH] feat: support command.stdin_file --- README.md | 6 +++++ pkg/config/command.go | 1 + pkg/controller/controller.go | 44 +++++++++++++++++++----------------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index f95d0f0..088093b 100644 --- a/README.md +++ b/README.md @@ -469,6 +469,12 @@ phases: stdin: | {{.Task.Name}} command: grep stdin + - name: stdin_file + command: + # read the command standard input from a file. + # The content is parsed with text/template + stdin_file: stdin.txt + command: grep stdin - name: write_file external file write_file: # The file path to be written diff --git a/pkg/config/command.go b/pkg/config/command.go index 5cb54a4..703ebee 100644 --- a/pkg/config/command.go +++ b/pkg/config/command.go @@ -10,6 +10,7 @@ type Command struct { Command Template CommandFile string `yaml:"command_file"` Stdin Template + StdinFile string `yaml:"stdin_file"` Env Envs } diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 22928d8..ea968ec 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -371,21 +371,31 @@ func (ctrl Controller) runPhase(ctx context.Context, params Params, idx int, wd var ErrBuildFail = errors.New("build failed") +func (ctrl Controller) readTemplateFile(p, wd string, tpl *config.Template) 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 err := tpl.SetText(result.Text); err != nil { + return err + } + 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 { - if task.Command.CommandFile != "" { - p := task.Command.CommandFile - if !filepath.IsAbs(p) { - p = filepath.Join(wd, p) - } - result, err := ctrl.FileReader.Read(p) - if err != nil { - return err - } - if err := task.Command.Command.SetText(result.Text); err != nil { - return err - } + if err := ctrl.readTemplateFile(task.Command.CommandFile, wd, &task.Command.Command); err != nil { + return err + } + if err := ctrl.readTemplateFile(task.Command.StdinFile, wd, &task.Command.Stdin); err != nil { + return err } for k, v := range task.Command.Env.Vars { if v.ValueFile != "" { @@ -406,15 +416,7 @@ func (ctrl Controller) ReadExternalFiles(ctx context.Context, wd string) error { task.Command.Env.Vars[k] = v } if task.WriteFile.TemplateFile != "" { - p := task.WriteFile.TemplateFile - if !filepath.IsAbs(p) { - p = filepath.Join(wd, p) - } - result, err := ctrl.FileReader.Read(p) - if err != nil { - return err - } - if err := task.WriteFile.Template.SetText(result.Text); err != nil { + if err := ctrl.readTemplateFile(task.WriteFile.TemplateFile, wd, &task.WriteFile.Template); err != nil { return err } }