From 28000431492f835912a785bd8a93f86ae590a041 Mon Sep 17 00:00:00 2001 From: Jeremiah Snapp Date: Fri, 5 Jun 2020 13:14:03 -0400 Subject: [PATCH 1/3] 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 +``` From 3b9a0427cdf992ff402aa4eaef1815e315a9ad83 Mon Sep 17 00:00:00 2001 From: Jeremiah Snapp Date: Fri, 5 Jun 2020 13:14:36 -0400 Subject: [PATCH 2/3] Add template_dir function to HCL Signed-off-by: Jeremiah Snapp --- hcl2template/function/templatedir.go | 18 ++++++++++++++++ hcl2template/functions.go | 1 + website/data/docs-navigation.js | 1 + .../from-1.5/functions/file/template_dir.mdx | 21 +++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 hcl2template/function/templatedir.go create mode 100644 website/pages/docs/from-1.5/functions/file/template_dir.mdx diff --git a/hcl2template/function/templatedir.go b/hcl2template/function/templatedir.go new file mode 100644 index 000000000..351cda13c --- /dev/null +++ b/hcl2template/function/templatedir.go @@ -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 + }, + }) +} diff --git a/hcl2template/functions.go b/hcl2template/functions.go index 4ae0fa862..6af63c559 100644 --- a/hcl2template/functions.go +++ b/hcl2template/functions.go @@ -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, diff --git a/website/data/docs-navigation.js b/website/data/docs-navigation.js index 4840b93fd..a4966c7f9 100644 --- a/website/data/docs-navigation.js +++ b/website/data/docs-navigation.js @@ -113,6 +113,7 @@ export default [ 'fileset', 'pathexpand', 'pwd', + 'template_dir', ], }, { diff --git a/website/pages/docs/from-1.5/functions/file/template_dir.mdx b/website/pages/docs/from-1.5/functions/file/template_dir.mdx new file mode 100644 index 000000000..ecad93176 --- /dev/null +++ b/website/pages/docs/from-1.5/functions/file/template_dir.mdx @@ -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 +``` From 3f2b8587b105830353c152baed964c29b6e0a073 Mon Sep 17 00:00:00 2001 From: Jeremiah Snapp Date: Fri, 5 Jun 2020 13:17:18 -0400 Subject: [PATCH 3/3] Replace regexreplace docs typo with regex_replace Signed-off-by: Jeremiah Snapp --- website/data/docs-navigation.js | 2 +- .../{regexreplace.mdx => regex_replace.mdx} | 20 +++++++++---------- .../from-1.5/functions/string/replace.mdx | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) rename website/pages/docs/from-1.5/functions/string/{regexreplace.mdx => regex_replace.mdx} (62%) diff --git a/website/data/docs-navigation.js b/website/data/docs-navigation.js index a4966c7f9..5afb3d00a 100644 --- a/website/data/docs-navigation.js +++ b/website/data/docs-navigation.js @@ -50,7 +50,7 @@ export default [ 'join', 'lower', 'replace', - 'regexreplace', + 'regex_replace', 'split', 'strrev', 'substr', diff --git a/website/pages/docs/from-1.5/functions/string/regexreplace.mdx b/website/pages/docs/from-1.5/functions/string/regex_replace.mdx similarity index 62% rename from website/pages/docs/from-1.5/functions/string/regexreplace.mdx rename to website/pages/docs/from-1.5/functions/string/regex_replace.mdx index 8957a4913..7d209a287 100644 --- a/website/pages/docs/from-1.5/functions/string/regexreplace.mdx +++ b/website/pages/docs/from-1.5/functions/string/regex_replace.mdx @@ -1,21 +1,21 @@ --- layout: docs -page_title: regexreplace - Functions - Configuration Language -sidebar_title: regexreplace +page_title: regex_replace - Functions - Configuration Language +sidebar_title: regex_replace 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 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 argument can be a valid regular expression or a string. ```hcl -regexreplace(string, substring, replacement) +regex_replace(string, substring, replacement) ``` `substring` should not be wrapped in forward slashes, it is always treated as a @@ -26,17 +26,17 @@ name of a capture group. ## Examples ```shell-session -> regexreplace("hello world", "world", "everybody") +> regex_replace("hello world", "world", "everybody") hello everybody -> regexreplace("hello world", "w.*d", "everybody") +> regex_replace("hello world", "w.*d", "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- ``` diff --git a/website/pages/docs/from-1.5/functions/string/replace.mdx b/website/pages/docs/from-1.5/functions/string/replace.mdx index 54a9ed212..472f55da4 100644 --- a/website/pages/docs/from-1.5/functions/string/replace.mdx +++ b/website/pages/docs/from-1.5/functions/string/replace.mdx @@ -28,5 +28,5 @@ hello everybody ## 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.