Skip to content

Commit

Permalink
Add vars_path input
Browse files Browse the repository at this point in the history
  • Loading branch information
chuhlomin committed May 27, 2022
1 parent 13a933e commit 6946209
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ GitHub Action to render file based on template and passed variables.

* `template` – path to Go template file
* `vars` – template variables in YAML format
* `vars_path` – Path to YAML file with variables
* `result_path` – (optional) desired path to result file

You must set either `vars` or `vars_path`, or you may set both of them
(`vars` values will take precedence over `vars_path`).

## Output

`result` – rendered template
Expand Down Expand Up @@ -60,7 +64,7 @@ jobs:
...
- name: Render template
id: render_template
uses: chuhlomin/render-template@v1.4
uses: chuhlomin/render-template@v1.5
with:
template: kube.template.yml
vars: |
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ inputs:
description: Variables to use in template
required: false

vars_path:
description: Path to YAML file with variables
required: false

result_path:
description: Desired path to result file (optional)
required: false
Expand All @@ -26,4 +30,4 @@ outputs:

runs:
using: docker
image: 'docker://chuhlomin/render-template:v1.4'
image: 'docker://chuhlomin/render-template:v1.5'
27 changes: 27 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type vars map[string]interface{}
type config struct {
Template string `env:"INPUT_TEMPLATE" envDefault:".kube.yml"`
Vars vars `env:"INPUT_VARS" envDefault:""`
VarsPath string `env:"INPUT_VARS_PATH" envDefault:""`
ResultPath string `env:"INPUT_RESULT_PATH" envDefault:""`
}

Expand All @@ -38,6 +39,18 @@ func run() error {
return err
}

if c.VarsPath != "" {
varsFile, err := ioutil.ReadFile(c.VarsPath)
if err != nil {
return fmt.Errorf("failed to read vars file %q: %v", c.VarsPath, err)
}
var varsFromFile vars
if err = yaml.Unmarshal(varsFile, &varsFromFile); err != nil {
return fmt.Errorf("failed to parse vars file %q: %v", c.VarsPath, err)
}
c.Vars = mergeVars(c.Vars, varsFromFile)
}

output, err := renderTemplate(c.Template, c.Vars)
if err != nil {
return fmt.Errorf("failed to render template: %v", err)
Expand All @@ -64,6 +77,20 @@ func varsParser(v string) (interface{}, error) {
return m, nil
}

func mergeVars(a, b vars) vars {
if a == nil {
return b
}

for k, v := range b {
if _, ok := a[k]; ok {
continue
}
a[k] = v
}
return a
}

func renderTemplate(templateFilePath string, vars vars) (string, error) {
b, err := ioutil.ReadFile(templateFilePath)
if err != nil {
Expand Down
60 changes: 60 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"errors"
"reflect"
"testing"
)

Expand Down Expand Up @@ -172,3 +173,62 @@ func TestRenderTemplate(t *testing.T) {
}
}
}

func TestMergeVars(t *testing.T) {
tests := []struct {
vars, varsFromFile, expectedResult vars
}{
{
map[string]interface{}{
"key_1": "value_1",
},
map[string]interface{}{
"key_2": "value_2",
},
map[string]interface{}{
"key_1": "value_1",
"key_2": "value_2",
},
},
{
map[string]interface{}{
"key": "value_1",
},
map[string]interface{}{
"key": "value_2",
},
map[string]interface{}{
"key": "value_1",
},
},
{
nil,
map[string]interface{}{
"key": "value",
},
map[string]interface{}{
"key": "value",
},
},
{
nil,
nil,
nil,
},
{
map[string]interface{}{
"key": "value",
},
nil,
map[string]interface{}{
"key": "value",
},
},
}
for _, tt := range tests {
result := mergeVars(tt.vars, tt.varsFromFile)
if !reflect.DeepEqual(result, tt.expectedResult) {
t.Errorf("mergeVars(%v, %v) expected: %v, got: %v", tt.vars, tt.varsFromFile, tt.expectedResult, result)
}
}
}

0 comments on commit 6946209

Please sign in to comment.