-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add template-related template funcs
- Loading branch information
Showing
3 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters