Skip to content

Commit

Permalink
lang/funcs: add "abspath" function
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiDog committed May 25, 2019
1 parent f68673f commit e167a52
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lang/funcs/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions lang/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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,
Expand Down
14 changes: 14 additions & 0 deletions lang/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lang

import (
"fmt"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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+")`,
Expand Down
30 changes: 30 additions & 0 deletions website/docs/configuration/functions/abspath.html.md
Original file line number Diff line number Diff line change
@@ -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
```
4 changes: 4 additions & 0 deletions website/layouts/functions.erb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,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>
Expand Down

0 comments on commit e167a52

Please sign in to comment.