Skip to content

Commit

Permalink
feat: add funcs: date, mdlink, number, base64 (#9)
Browse files Browse the repository at this point in the history
* feat: add funcs: date, mdlink, number, base64

* debug(gha): update action image

* fix: parsing date argument, support timezones

* chore: prepare for merge

* chore: update README.md
  • Loading branch information
chuhlomin authored May 11, 2023
1 parent 4bc8bb0 commit b5c1c08
Show file tree
Hide file tree
Showing 61 changed files with 17,745 additions and 3 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,26 @@ GitHub Action to render file based on template and passed variables.
| vars | Variables to use in template (in YAML format) | false |
| vars_path | Path to YAML file with variables | false |
| result_path | Desired path to result file | false |
| timezone | Timezone to use in `date` template function | false |

You must set at least `vars` or `vars_path`.
You may set both of them (`vars` values will precede over `vars_path`).

There are few template functions available:

- `date` – formats timestamp using Go's [time layout](https://golang.org/pkg/time/#pkg-constants).
Example: `{{ "2023-05-11T01:42:04Z" | date "2006-01-02" }}` will be rendered as `2023-05-11`.
You may use `timezone` input to set timezone for `date` function (e.g. `timezone: "America/New_York"`).

- `mdlink` – creates markdown link.
Example: `{{ "https://github.com" | mdlink "GitHub" }}` will be rendered as `[GitHub](https://github.com)`.

- `number` – formats number in English locale.
Example: `{{ 1234567890 | number }}` will be rendered as `1,234,567,890`.

- `base64` – encodes string to base64.
Example: `{{ "hello" | base64 }}` will be rendered as `aGVsbG8=`.

## Outputs

| Name | Description |
Expand Down Expand Up @@ -74,7 +90,7 @@ jobs:
- name: Render template
id: render_template
uses: chuhlomin/render-template@v1.6
uses: chuhlomin/render-template@v1.7
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 @@ -24,10 +24,14 @@ inputs:
description: Desired path to result file (optional)
required: false

timezone:
description: Timezone to use in `date` template function
required: false

outputs:
result:
description: Rendered file content

runs:
using: docker
image: 'docker://chuhlomin/render-template:v1.6'
image: 'docker://chuhlomin/render-template:v1.7'
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.17

require (
github.com/caarlos0/env/v6 v6.9.2
golang.org/x/text v0.9.0
gopkg.in/yaml.v3 v3.0.0
)
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
github.com/caarlos0/env/v6 v6.9.2 h1:vYTmP7KPtHf3LqaQH5Z2AkUY8GmanDrTelXnFzxSK44=
github.com/caarlos0/env/v6 v6.9.2/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
Expand Down
60 changes: 59 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ package main

import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"log"
"os"
"reflect"
"strconv"
"text/template"
"time"
_ "time/tzdata"

"github.com/caarlos0/env/v6"
"golang.org/x/text/language"
"golang.org/x/text/message"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -91,6 +98,53 @@ func mergeVars(a, b vars) vars {
return a
}

var funcMap = template.FuncMap{
"date": func(format string, in interface{}) string {
var t time.Time
switch v := in.(type) {
case string:
var err error
t, err = time.Parse(time.RFC3339, v)
if err != nil {
log.Printf("failed to parse date %q: %v", v, err)
return v
}
case time.Time:
t = v
default:
log.Printf("unsupported type %T for date", in)
return fmt.Sprintf("%v", in)
}

timezone := os.Getenv("INPUT_TIMEZONE")
if timezone != "" {
loc, err := time.LoadLocation(timezone)
if err != nil {
log.Printf("failed to load timezone %q: %v", timezone, err)
return in.(string)
}
t = t.In(loc)
}

return t.Format(format)
},
"mdlink": func(text, url string) string {
return fmt.Sprintf("[%s](%s)", text, url)
},
"number": func(in string) string {
p := message.NewPrinter(language.English)
d, err := strconv.ParseInt(in, 10, 64)
if err != nil {
log.Printf("failed to parse number %q: %v", in, err)
return in
}
return p.Sprintf("%d", d)
},
"base64": func(in string) string {
return base64.StdEncoding.EncodeToString([]byte(in))
},
}

func renderTemplate(templateFilePath string, vars vars) (string, error) {
b, err := os.ReadFile(templateFilePath)
if err != nil {
Expand All @@ -103,7 +157,11 @@ func renderTemplate(templateFilePath string, vars vars) (string, error) {
return "", fmt.Errorf("failed to read template %q: %v", templateFilePath, err)
}

tmpl, err := template.New(templateFilePath).Option("missingkey=error").Parse(string(b))
tmpl, err := template.
New(templateFilePath).
Option("missingkey=error").
Funcs(funcMap).
Parse(string(b))
if err != nil {
return "", err
}
Expand Down
12 changes: 12 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"encoding/json"
"errors"
"os"
"reflect"
"testing"
"time"
)

func TestFormatOutput(t *testing.T) {
Expand Down Expand Up @@ -145,8 +147,18 @@ func TestRenderTemplate(t *testing.T) {
nil,
"Hello text+text\n",
},
{
"./testdata/funcs.txt",
map[string]interface{}{
"time": time.Date(2023, time.August, 6, 15, 8, 28, 0, time.UTC),
},
nil,
"06 Aug 2023 11:08:28\n06 Aug 2023 11:08:28\n[download](https://github.com)\n1,000\nQUJD\n",
},
}

os.Setenv("INPUT_TIMEZONE", "America/New_York")

for _, tt := range tests {
output, err := renderTemplate(tt.templateFilePath, tt.vars)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions testdata/funcs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{ .time | date "02 Jan 2006 15:04:05" }}
{{ "2023-08-06T15:08:28Z" | date "02 Jan 2006 15:04:05" }}
{{ "https://github.com" | mdlink "download" }}
{{ "1000" | number }}
{{ "ABC" | base64 }}
27 changes: 27 additions & 0 deletions vendor/golang.org/x/text/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/golang.org/x/text/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions vendor/golang.org/x/text/feature/plural/common.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b5c1c08

Please sign in to comment.