Add template_dir function to HCL

Signed-off-by: Jeremiah Snapp <jeremiah@chef.io>
This commit is contained in:
Jeremiah Snapp 2020-06-05 13:14:36 -04:00
parent 2800043149
commit 3b9a0427cd
4 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package function
import (
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
// MakeTemplateDirFunc constructs a function that returns the directory
// in which the configuration file is located.
func MakeTemplateDirFunc(baseDir string) function.Function {
return function.New(&function.Spec{
Params: []function.Parameter{},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
return cty.StringVal(baseDir), nil
},
})
}

View File

@ -94,6 +94,7 @@ func Functions(basedir string) map[string]function.Function {
"split": stdlib.SplitFunc,
"strrev": stdlib.ReverseFunc,
"substr": stdlib.SubstrFunc,
"template_dir": pkrfunction.MakeTemplateDirFunc(basedir),
"timestamp": pkrfunction.TimestampFunc,
"timeadd": stdlib.TimeAddFunc,
"title": stdlib.TitleFunc,

View File

@ -113,6 +113,7 @@ export default [
'fileset',
'pathexpand',
'pwd',
'template_dir',
],
},
{

View File

@ -0,0 +1,21 @@
---
layout: docs
page_title: template_dir - Functions - Configuration Language
sidebar_title: template_dir
description: The template_dir function returns the directory in which the configuration file is located.
---
# `template_dir` Function
`template_dir` returns the directory in which the configuration file is located.
```hcl
template_dir()
```
## Examples
```shell-session
> template_dir()
packer
```