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 #39 from suzuki-shunsuke/feat/support-separate-file
Browse files Browse the repository at this point in the history
feat: support to separate the configuration file
  • Loading branch information
suzuki-shunsuke authored Oct 11, 2020
2 parents 2f207ad + 18aa218 commit 7b0196f
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 33 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,12 @@ phases:
# environment variables
# In the environment variable name and value text/template can be used
env:
token: "{{ .Task.Name }}"
- key: token
value: "{{ .Task.Name }}"
- key: foo
# read the environment variable from a file
# the content is parsed with text/template
value_file: foo.txt
# The condition whether the task is run.
# The default is true.
# The value should be true or false or a tengo script.
Expand Down Expand Up @@ -452,6 +457,19 @@ phases:
# The template of the file content.
template: |
{{ .Task.Name }}
- name: yoo
command:
# read a command template from a file
# The content is parsed with text/template
command_file: yoo.txt
- name: write_file external file
write_file:
# The file path to be written
# If the path is the relative path, this is treated as the relative path from the directory where the configuration file exists.
path: foo.txt
# The template file.
# The content is parsed with text/template
template_file: zoo.txt
condition:
# When the skip is true, the phase is skipped.
# The value should be true or false or a tengo script.
Expand Down
37 changes: 13 additions & 24 deletions pkg/config/command.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package config

import "github.com/suzuki-shunsuke/buildflow/pkg/template"
import (
"github.com/suzuki-shunsuke/buildflow/pkg/template"
)

type Command struct {
Shell string
ShellOpts []string `yaml:"shell_options"`
Command Template
Env Envs
Shell string
ShellOpts []string `yaml:"shell_options"`
Command Template
CommandFile string `yaml:"command_file"`
Env Envs
}

func (cmd Command) SetDefault() Command {
Expand All @@ -25,30 +28,16 @@ type Envs struct {
}

type EnvVar struct {
Key template.Template
Value template.Template
Key template.Template
Value template.Template
ValueFile string `yaml:"value_file"`
}

func (envs *Envs) UnmarshalYAML(unmarshal func(interface{}) error) error {
m := map[string]string{}
m := []EnvVar{}
if err := unmarshal(&m); err != nil {
return err
}
arr := make([]EnvVar, 0, len(m))
for k, v := range m {
key, err := template.Compile(k)
if err != nil {
return err
}
val, err := template.Compile(v)
if err != nil {
return err
}
arr = append(arr, EnvVar{
Key: key,
Value: val,
})
}
envs.Vars = arr
envs.Vars = m
return nil
}
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ for task in Tasks {
}

for j, task := range phase.Tasks {
if task.Command.Command.Text != "" {
if task.Command.Command.Text != "" || task.Command.CommandFile != "" {
task.Command = task.Command.SetDefault()
}
task.When.SetDefaultBool(true)
Expand Down
7 changes: 4 additions & 3 deletions pkg/config/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ type Task struct {
}

type WriteFile struct {
Path Template
Template Template
Path Template
Template Template
TemplateFile string `yaml:"template_file"`
}

func (task *Task) Set() error {
Expand All @@ -50,7 +51,7 @@ func (task *Task) Set() error {
}

func (task *Task) SetType() error {
if task.Command.Command.Text != "" {
if task.Command.Command.Text != "" || task.Command.CommandFile != "" {
task.Type = constant.Command
return nil
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ func (tmpl Template) New(params interface{}) (Template, error) {
Template: tmpl.Template,
}, err
}

func (tmpl *Template) SetText(text string) error {
t, err := template.Compile(text)
if err != nil {
return err
}
tmpl.Text = text
tmpl.Template = t
return nil
}
60 changes: 60 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"path/filepath"
"sync"

"github.com/google/go-github/v32/github"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/suzuki-shunsuke/buildflow/pkg/domain"
"github.com/suzuki-shunsuke/buildflow/pkg/execute"
gh "github.com/suzuki-shunsuke/buildflow/pkg/github"
"github.com/suzuki-shunsuke/buildflow/pkg/template"
"github.com/suzuki-shunsuke/go-dataeq/dataeq"
)

Expand Down Expand Up @@ -369,6 +371,60 @@ func (ctrl Controller) runPhase(ctx context.Context, params Params, idx int, wd

var ErrBuildFail = errors.New("build failed")

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
}
}
for k, v := range task.Command.Env.Vars {
if v.ValueFile != "" {
p := v.ValueFile
if !filepath.IsAbs(p) {
p = filepath.Join(wd, p)
}
result, err := ctrl.FileReader.Read(p)
if err != nil {
return err
}
tpl, err := template.Compile(result.Text)
if err != nil {
return err
}
v.Value = tpl
}
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 {
return err
}
}
phase.Tasks[j] = task
}
ctrl.Config.Phases[i] = phase
}
return nil
}

func (ctrl Controller) Run(ctx context.Context, wd string) error {
pr, err := ctrl.getPR(ctx)
if err != nil {
Expand All @@ -382,6 +438,10 @@ func (ctrl Controller) Run(ctx context.Context, wd string) error {
}).Debug("pull request")
}

if err := ctrl.ReadExternalFiles(ctx, wd); err != nil {
return err
}

params, err := ctrl.getParams(ctx, pr)
if err != nil {
return err
Expand Down
27 changes: 23 additions & 4 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

type Template struct {
tpl *template.Template
Template *template.Template
Text string
}

func Compile(tpl string) (Template, error) {
Expand All @@ -18,17 +19,35 @@ func Compile(tpl string) (Template, error) {
return Template{}, err
}
return Template{
tpl: tmpl,
Template: tmpl,
Text: tpl,
}, nil
}

func (tpl Template) GetRaw() string {
return tpl.Text
}

func (tpl Template) Render(params interface{}) (string, error) {
if tpl.tpl == nil {
if tpl.Template == nil {
return "", nil
}
buf := &bytes.Buffer{}
if err := tpl.tpl.Execute(buf, params); err != nil {
if err := tpl.Template.Execute(buf, params); err != nil {
return "", err
}
return buf.String(), nil
}

func (tpl *Template) UnmarshalYAML(unmarshal func(interface{}) error) error {
var src string
if err := unmarshal(&src); err != nil {
return err
}
t, err := Compile(src)
if err != nil {
return err
}
*tpl = t
return nil
}

0 comments on commit 7b0196f

Please sign in to comment.