Skip to content

Commit

Permalink
Add Template Functions (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmenezes authored Sep 1, 2020
1 parent 0e08516 commit 71cef28
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cmd/codeownerslint/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"path/filepath"
"strings"
"text/template"

"github.com/fmenezes/codeowners"
Expand Down Expand Up @@ -38,7 +39,10 @@ func run(wr io.Writer, opt options) exitCode {
format = opt.format
}
format = fmt.Sprintf("%s\n", format)
tpl, err := template.New("main").Parse(format)
tpl, err := template.New("main").Funcs(template.FuncMap{
"ToLower": strings.ToLower,
"ToUpper": strings.ToUpper,
}).Parse(format)
if err != nil {
fmt.Fprintf(wr, "Unexpected error when parsing format: %v", err)
return unexpectedErrorCode
Expand Down
8 changes: 8 additions & 0 deletions cmd/codeownerslint/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func TestCustomFormat(t *testing.T) {
`)
}

func TestCustomFormatFunc(t *testing.T) {
assert(t, options{
directory: "../../test/data/noowners",
format: "{{ToLower .Position.FilePath}}",
}, errorCode, `codeowners
`)
}

func TestInvalidFormatParse(t *testing.T) {
assertCode(t, options{
directory: "../../test/data/noowners",
Expand Down

0 comments on commit 71cef28

Please sign in to comment.