From 28000431492f835912a785bd8a93f86ae590a041 Mon Sep 17 00:00:00 2001 From: Jeremiah Snapp Date: Fri, 5 Jun 2020 13:14:03 -0400 Subject: [PATCH] Add pwd function to HCL Signed-off-by: Jeremiah Snapp --- hcl2template/function/pwd.go | 20 ++++++++++++++++++ hcl2template/functions.go | 1 + website/data/docs-navigation.js | 1 + .../docs/from-1.5/functions/file/pwd.mdx | 21 +++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 hcl2template/function/pwd.go create mode 100644 website/pages/docs/from-1.5/functions/file/pwd.mdx diff --git a/hcl2template/function/pwd.go b/hcl2template/function/pwd.go new file mode 100644 index 000000000..64948d838 --- /dev/null +++ b/hcl2template/function/pwd.go @@ -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 + }, + }) +} diff --git a/hcl2template/functions.go b/hcl2template/functions.go index 821c525ba..4ae0fa862 100644 --- a/hcl2template/functions.go +++ b/hcl2template/functions.go @@ -76,6 +76,7 @@ func Functions(basedir string) map[string]function.Function { "parseint": stdlib.ParseIntFunc, "pathexpand": filesystem.PathExpandFunc, "pow": stdlib.PowFunc, + "pwd": pkrfunction.MakePwdFunc(), "range": stdlib.RangeFunc, "reverse": stdlib.ReverseFunc, "replace": stdlib.ReplaceFunc, diff --git a/website/data/docs-navigation.js b/website/data/docs-navigation.js index 3b29ceaa9..4840b93fd 100644 --- a/website/data/docs-navigation.js +++ b/website/data/docs-navigation.js @@ -112,6 +112,7 @@ export default [ 'fileexists', 'fileset', 'pathexpand', + 'pwd', ], }, { diff --git a/website/pages/docs/from-1.5/functions/file/pwd.mdx b/website/pages/docs/from-1.5/functions/file/pwd.mdx new file mode 100644 index 000000000..ef2bafe28 --- /dev/null +++ b/website/pages/docs/from-1.5/functions/file/pwd.mdx @@ -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 +```