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 #87 from suzuki-shunsuke/fix/fix-infinite-phase-de…
Browse files Browse the repository at this point in the history
…tection

feat: support to read JSON and YAML
  • Loading branch information
suzuki-shunsuke authored Oct 16, 2020
2 parents 9d30cb0 + a675b65 commit 72d1ec6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
# read from read_file.yaml
message: hello
3 changes: 3 additions & 0 deletions examples/read_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "foo"
}
28 changes: 28 additions & 0 deletions examples/read_file.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,31 @@ phases:
{{with GetTaskByName .Tasks "foo"}}{{.File.Text}}{{end}}
dependency:
- foo
- name: json
read_file:
format: json
path: read_file.json
- name: output json
command:
command: |
echo "$FOO"
env:
- key: FOO
value: |
{{with GetTaskByName .Tasks "json"}}{{.File.Data.name}}{{end}}
dependency:
- json
- name: yaml
read_file:
format: yaml
path: data.yaml
- name: output yaml
command:
command: |
echo "$FOO"
env:
- key: FOO
value: |
{{with GetTaskByName .Tasks "yaml"}}{{.File.Data.message}}{{end}}
dependency:
- yaml
35 changes: 33 additions & 2 deletions pkg/controller/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
"encoding/json"
"errors"
"io"
"time"
Expand All @@ -10,6 +11,8 @@ import (
"github.com/suzuki-shunsuke/buildflow/pkg/constant"
"github.com/suzuki-shunsuke/buildflow/pkg/domain"
"github.com/suzuki-shunsuke/buildflow/pkg/execute"
"github.com/suzuki-shunsuke/go-convmap/convmap"
"gopkg.in/yaml.v2"
)

type Task struct {
Expand Down Expand Up @@ -50,9 +53,37 @@ func (task Task) run(ctx context.Context, wd string) (domain.Result, error) {
}, err
case constant.ReadFile:
fileResult, err := task.FileReader.Read(task.Config.ReadFile.Path.Text)
return domain.Result{
result := domain.Result{
File: fileResult,
}, err
}
if err != nil {
return result, err
}
switch task.Config.ReadFile.Format {
case "":
return result, err
case "json":
var d interface{}
if err := json.Unmarshal([]byte(fileResult.Text), &d); err != nil {
return result, err
}
result.File.Data = d
return result, nil
case "yaml":
var d interface{}
if err := yaml.Unmarshal([]byte(fileResult.Text), &d); err != nil {
return result, err
}
a, err := convmap.Convert(d)
if err != nil {
return result, err
}
result.File.Data = a
return result, nil
// case "toml":
default:
return result, errors.New("invalid file.format: " + task.Config.ReadFile.Format)
}
case constant.WriteFile:
// TODO append a new line
fileResult, err := task.FileWriter.Write(
Expand Down
2 changes: 2 additions & 0 deletions pkg/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type FileResult struct {
Size int64
Mode os.FileMode
IsDir bool
Data interface{}
}

func (fileResult FileResult) ToTemplate() map[string]interface{} {
Expand All @@ -71,6 +72,7 @@ func (fileResult FileResult) ToTemplate() map[string]interface{} {
"ModTime": fileResult.ModTime,
"Size": fileResult.Size,
"IsDir": fileResult.IsDir,
"Data": fileResult.Data,
}
}

Expand Down

0 comments on commit 72d1ec6

Please sign in to comment.