Merge pull request #9367 from jeremiahsnapp/add-template_dir-and-pwd-functions

Add template_dir() and pwd() functions to HCL
This commit is contained in:
Megan Marsh 2020-06-05 12:57:04 -07:00 committed by GitHub
commit d73f83a9d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 96 additions and 12 deletions

View File

@ -0,0 +1,20 @@
package function
import (
"os"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
// MakePwdFunc constructs a function that returns the working directory as a string.
func MakePwdFunc() 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) {
dir, err := os.Getwd()
return cty.StringVal(dir), err
},
})
}

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

@ -76,6 +76,7 @@ func Functions(basedir string) map[string]function.Function {
"parseint": stdlib.ParseIntFunc, "parseint": stdlib.ParseIntFunc,
"pathexpand": filesystem.PathExpandFunc, "pathexpand": filesystem.PathExpandFunc,
"pow": stdlib.PowFunc, "pow": stdlib.PowFunc,
"pwd": pkrfunction.MakePwdFunc(),
"range": stdlib.RangeFunc, "range": stdlib.RangeFunc,
"reverse": stdlib.ReverseFunc, "reverse": stdlib.ReverseFunc,
"replace": stdlib.ReplaceFunc, "replace": stdlib.ReplaceFunc,
@ -93,6 +94,7 @@ func Functions(basedir string) map[string]function.Function {
"split": stdlib.SplitFunc, "split": stdlib.SplitFunc,
"strrev": stdlib.ReverseFunc, "strrev": stdlib.ReverseFunc,
"substr": stdlib.SubstrFunc, "substr": stdlib.SubstrFunc,
"template_dir": pkrfunction.MakeTemplateDirFunc(basedir),
"timestamp": pkrfunction.TimestampFunc, "timestamp": pkrfunction.TimestampFunc,
"timeadd": stdlib.TimeAddFunc, "timeadd": stdlib.TimeAddFunc,
"title": stdlib.TitleFunc, "title": stdlib.TitleFunc,

View File

@ -50,7 +50,7 @@ export default [
'join', 'join',
'lower', 'lower',
'replace', 'replace',
'regexreplace', 'regex_replace',
'split', 'split',
'strrev', 'strrev',
'substr', 'substr',
@ -112,6 +112,8 @@ export default [
'fileexists', 'fileexists',
'fileset', 'fileset',
'pathexpand', 'pathexpand',
'pwd',
'template_dir',
], ],
}, },
{ {

View File

@ -0,0 +1,21 @@
---
layout: docs
page_title: pwd - Functions - Configuration Language
sidebar_title: pwd
description: The pwd function returns the working directory while executing Packer.
---
# `pwd` Function
`pwd` returns the working directory while executing Packer.
```hcl
pwd()
```
## Examples
```shell-session
> pwd()
/home/user/some/packer
```

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
```

View File

@ -1,21 +1,21 @@
--- ---
layout: docs layout: docs
page_title: regexreplace - Functions - Configuration Language page_title: regex_replace - Functions - Configuration Language
sidebar_title: regexreplace sidebar_title: regex_replace
description: |- description: |-
The regexreplace function searches a given string for another given substring, The regex_replace function searches a given string for another given substring,
and replaces all occurrences with a given replacement string. The substring and replaces all occurrences with a given replacement string. The substring
argument can be a valid regular expression or a string. argument can be a valid regular expression or a string.
--- ---
# `regexreplace` Function # `regex_replace` Function
`regexreplace` searches a given string for another given substring, and `regex_replace` searches a given string for another given substring, and
replaces each occurrence with a given replacement string. The substring replaces each occurrence with a given replacement string. The substring
argument can be a valid regular expression or a string. argument can be a valid regular expression or a string.
```hcl ```hcl
regexreplace(string, substring, replacement) regex_replace(string, substring, replacement)
``` ```
`substring` should not be wrapped in forward slashes, it is always treated as a `substring` should not be wrapped in forward slashes, it is always treated as a
@ -26,17 +26,17 @@ name of a capture group.
## Examples ## Examples
```shell-session ```shell-session
> regexreplace("hello world", "world", "everybody") > regex_replace("hello world", "world", "everybody")
hello everybody hello everybody
> regexreplace("hello world", "w.*d", "everybody") > regex_replace("hello world", "w.*d", "everybody")
hello everybody hello everybody
> regexreplace("-ab-axxb-", "a(x*)b", "$1W) > regex_replace("-ab-axxb-", "a(x*)b", "$1W)
--- ---
> regexreplace("-ab-axxb-", "a(x*)b", "${1}W") > regex_replace("-ab-axxb-", "a(x*)b", "${1}W")
-W-xxW- -W-xxW-
``` ```

View File

@ -28,5 +28,5 @@ hello everybody
## Related Functions ## Related Functions
- [`regexreplace`](/docs/from-1.5/functions/string/regexreplace) searches a given string for another given substring, - [`regex_replace`](/docs/from-1.5/functions/string/regex_replace) searches a given string for another given substring,
and replaces each occurrence with a given replacement string. and replaces each occurrence with a given replacement string.