Skip to content

Commit

Permalink
feat: add template-related template funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
MuXiu1997 committed Dec 28, 2023
1 parent d7f81f9 commit 087f613
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ docker run --rm muxiu1997/goet -h
- [Sprig Function Documentation](http://masterminds.github.io/sprig/)
### OS/FS/PATH
### Template
- `templateFilePath` - templateFilePath returns the path of the template file. If the template is stdin, "/dev/stdin" is returned. If the template is a URL, the URL is returned.
- `templateDirPath` - templateDirPath returns the directory of the template file. If the template is stdin, "/dev" is returned. If the template is a URL, "" is returned.
- `outputFilePath` - outputFilePath returns the path of the output file. If the output is stdout, "/dev/stdout" is returned.
- `outputDirPath` - outputDirPath returns the directory of the output file. If the output is stdout, "/dev" is returned.
### OS / FS / PATH
- `lookPath` - lookPath runs exec.LookPath on name and returns the path or an empty string if the name is not found.
- `lstat` - lstat runs os.Lstat on name and returns structured data that contains the
Expand Down Expand Up @@ -86,4 +93,4 @@ docker run --rm muxiu1997/goet -h
## License
[MIT](https://github.com/MuXiu1997/goet/blob/main/LICENSE)
[MIT](https://github.com/MuXiu1997/goet/blob/main/LICENSE)
53 changes: 53 additions & 0 deletions pkg/templatefunc/template/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package template

import "path/filepath"
import ttemplate "text/template"

import tc "github.com/MuXiu1997/goet/pkg/templatecontext"

func FuncMap(templateContext *tc.TemplateContext) ttemplate.FuncMap {
return ttemplate.FuncMap{
"templateFilePath": templateFilePath(templateContext),
"templateDirPath": templateDirPath(templateContext),
"outputFilePath": outputFilePath(templateContext),
"outputDirPath": outputDirPath(templateContext),
}
}

// templateFilePath returns the path of the template file.
// If the template is stdin, "/dev/stdin" is returned.
// If the template is a URL, the URL is returned.
func templateFilePath(templateContext *tc.TemplateContext) func() string {
return func() string {
return templateContext.Template
}
}

// templateDirPath returns the directory of the template file.
// If the template is stdin, "/dev" is returned.
// If the template is a URL, "" is returned.
func templateDirPath(templateContext *tc.TemplateContext) func() string {
return func() string {
if templateContext.TemplateType == tc.TemplateTypeFile {
return filepath.Dir(templateContext.Template)
} else {
return ""
}
}
}

// outputFilePath returns the path of the output file.
// If the output is stdout, "/dev/stdout" is returned.
func outputFilePath(templateContext *tc.TemplateContext) func() string {
return func() string {
return templateContext.Output
}
}

// outputDirPath returns the directory of the output file.
// If the output is stdout, "/dev" is returned.
func outputDirPath(templateContext *tc.TemplateContext) func() string {
return func() string {
return filepath.Dir(templateContext.Output)
}
}
2 changes: 2 additions & 0 deletions pkg/templaterenderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"github.com/Masterminds/sprig/v3"
"github.com/MuXiu1997/goet/pkg/templatefunc/osfspath"
"github.com/MuXiu1997/goet/pkg/templatefunc/template"
"github.com/MuXiu1997/goet/pkg/templatefunc/toml"
"github.com/MuXiu1997/goet/pkg/templatefunc/yaml"
ttemplate "text/template"
Expand All @@ -17,6 +18,7 @@ func Render(templateContext *tc.TemplateContext) (string, error) {
Funcs(osfspath.FuncMap()).
Funcs(yaml.FuncMap()).
Funcs(toml.FuncMap()).
Funcs(template.FuncMap(templateContext)).
Parse(templateContext.TemplateContent)
if err != nil {
return "", err
Expand Down

0 comments on commit 087f613

Please sign in to comment.