mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-24 16:10:46 -06:00
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.
|
||||
var PathExpandFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
|
@ -31,6 +31,7 @@ func (s *Scope) Functions() map[string]function.Function {
|
||||
|
||||
s.funcs = map[string]function.Function{
|
||||
"abs": stdlib.AbsoluteFunc,
|
||||
"abspath": funcs.AbsPathFunc,
|
||||
"basename": funcs.BasenameFunc,
|
||||
"base64decode": funcs.Base64DecodeFunc,
|
||||
"base64encode": funcs.Base64EncodeFunc,
|
||||
|
@ -2,6 +2,7 @@ package lang
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"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("YWJjMTIzIT8kKiYoKSctPUB+")`,
|
||||
|
30
website/docs/configuration/functions/abspath.html.md
Normal file
30
website/docs/configuration/functions/abspath.html.md
Normal file
@ -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>
|
||||
<ul class="nav">
|
||||
|
||||
<li>
|
||||
<a href="/docs/configuration/functions/abspath.html">abspath</a>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<a href="/docs/configuration/functions/dirname.html">dirname</a>
|
||||
</li>
|
||||
|
Loading…
Reference in New Issue
Block a user