Add pwd function to HCL

Signed-off-by: Jeremiah Snapp <jeremiah@chef.io>
This commit is contained in:
Jeremiah Snapp 2020-06-05 13:14:03 -04:00
parent 26e099023b
commit 2800043149
4 changed files with 43 additions and 0 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

@ -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,

View File

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

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