lang/funcs: add "abspath" function (#21409)
This commit is contained in:
parent
7570711a67
commit
042aead714
|
@ -237,6 +237,21 @@ var DirnameFunc = function.New(&function.Spec{
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// AbsPathFunc constructs a function that converts a filesystem path to an absolute path
|
||||||
|
var AbsPathFunc = function.New(&function.Spec{
|
||||||
|
Params: []function.Parameter{
|
||||||
|
{
|
||||||
|
Name: "path",
|
||||||
|
Type: cty.String,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Type: function.StaticReturnType(cty.String),
|
||||||
|
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||||
|
absPath, err := filepath.Abs(args[0].AsString())
|
||||||
|
return cty.StringVal(filepath.ToSlash(absPath)), err
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
// PathExpandFunc constructs a function that expands a leading ~ character to the current user's home directory.
|
// PathExpandFunc constructs a function that expands a leading ~ character to the current user's home directory.
|
||||||
var PathExpandFunc = function.New(&function.Spec{
|
var PathExpandFunc = function.New(&function.Spec{
|
||||||
Params: []function.Parameter{
|
Params: []function.Parameter{
|
||||||
|
|
|
@ -31,6 +31,7 @@ func (s *Scope) Functions() map[string]function.Function {
|
||||||
|
|
||||||
s.funcs = map[string]function.Function{
|
s.funcs = map[string]function.Function{
|
||||||
"abs": stdlib.AbsoluteFunc,
|
"abs": stdlib.AbsoluteFunc,
|
||||||
|
"abspath": funcs.AbsPathFunc,
|
||||||
"basename": funcs.BasenameFunc,
|
"basename": funcs.BasenameFunc,
|
||||||
"base64decode": funcs.Base64DecodeFunc,
|
"base64decode": funcs.Base64DecodeFunc,
|
||||||
"base64encode": funcs.Base64EncodeFunc,
|
"base64encode": funcs.Base64EncodeFunc,
|
||||||
|
|
|
@ -2,6 +2,7 @@ package lang
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -53,6 +54,19 @@ func TestFunctions(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"abspath": {
|
||||||
|
{
|
||||||
|
`abspath(".")`,
|
||||||
|
cty.StringVal((func() string {
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return cwd
|
||||||
|
})()),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
"base64decode": {
|
"base64decode": {
|
||||||
{
|
{
|
||||||
`base64decode("YWJjMTIzIT8kKiYoKSctPUB+")`,
|
`base64decode("YWJjMTIzIT8kKiYoKSctPUB+")`,
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
---
|
||||||
|
layout: "functions"
|
||||||
|
page_title: "abspath - Functions - Configuration Language"
|
||||||
|
sidebar_current: "docs-funcs-file-abspath"
|
||||||
|
description: |-
|
||||||
|
The abspath function converts the argument to an absolute filesystem path.
|
||||||
|
---
|
||||||
|
|
||||||
|
# `abspath` Function
|
||||||
|
|
||||||
|
-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
|
||||||
|
earlier, see
|
||||||
|
[0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html).
|
||||||
|
|
||||||
|
`abspath` takes a string containing a filesystem path and converts it
|
||||||
|
to an absolute path. That is, if the path is not absolute, it will be joined
|
||||||
|
with the current working directory.
|
||||||
|
|
||||||
|
Referring directly to filesystem paths in resource arguments may cause
|
||||||
|
spurious diffs if the same configuration is applied from multiple systems or on
|
||||||
|
different host operating systems. We recommend using filesystem paths only
|
||||||
|
for transient values, such as the argument to [`file`](./file.html) (where
|
||||||
|
only the contents are then stored) or in `connection` and `provisioner` blocks.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
```
|
||||||
|
> abspath(path.root)
|
||||||
|
/home/user/some/terraform/root
|
||||||
|
```
|
|
@ -272,6 +272,10 @@
|
||||||
<a href="#docs-funcs-file">Filesystem Functions</a>
|
<a href="#docs-funcs-file">Filesystem Functions</a>
|
||||||
<ul class="nav">
|
<ul class="nav">
|
||||||
|
|
||||||
|
<li>
|
||||||
|
<a href="/docs/configuration/functions/abspath.html">abspath</a>
|
||||||
|
</li>
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
<a href="/docs/configuration/functions/dirname.html">dirname</a>
|
<a href="/docs/configuration/functions/dirname.html">dirname</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
Loading…
Reference in New Issue