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

Commit

Permalink
feat: support command.stdin_file
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Oct 11, 2020
1 parent 43aa387 commit 38f4ff8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Command struct {
Command Template
CommandFile string `yaml:"command_file"`
Stdin Template
StdinFile string `yaml:"stdin_file"`
Env Envs
}

Expand Down
44 changes: 23 additions & 21 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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
}
}
Expand Down

0 comments on commit 38f4ff8

Please sign in to comment.