Skip to content

Commit e4b49a8

Browse files
committed
bake: add homedir func
Signed-off-by: CrazyMax <[email protected]>
1 parent 5743e3a commit e4b49a8

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

bake/hclparser/stdlib.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package hclparser
22

33
import (
44
"errors"
5+
"os"
6+
"os/user"
57
"path"
8+
"path/filepath"
9+
"runtime"
610
"strings"
711
"time"
812

13+
"github.com/docker/cli/cli/config"
914
"github.com/hashicorp/go-cty-funcs/cidr"
1015
"github.com/hashicorp/go-cty-funcs/crypto"
1116
"github.com/hashicorp/go-cty-funcs/encoding"
@@ -62,6 +67,7 @@ var stdlibFunctions = []funcDef{
6267
{name: "greaterthan", fn: stdlib.GreaterThanFunc},
6368
{name: "greaterthanorequalto", fn: stdlib.GreaterThanOrEqualToFunc},
6469
{name: "hasindex", fn: stdlib.HasIndexFunc},
70+
{name: "homedir", factory: homedirFunc},
6571
{name: "indent", fn: stdlib.IndentFunc},
6672
{name: "index", fn: stdlib.IndexFunc},
6773
{name: "indexof", factory: indexOfFunc},
@@ -254,6 +260,27 @@ func timestampFunc() function.Function {
254260
})
255261
}
256262

263+
// homedirFunc constructs a function that returns the current user's home directory.
264+
func homedirFunc() function.Function {
265+
return function.New(&function.Spec{
266+
Description: `Returns the current user's home directory.`,
267+
Params: []function.Parameter{},
268+
Type: function.StaticReturnType(cty.String),
269+
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
270+
home, err := os.UserHomeDir()
271+
if err != nil {
272+
if home == "" && runtime.GOOS != "windows" {
273+
if u, err := user.Current(); err == nil {
274+
return cty.StringVal(u.HomeDir), nil
275+
}
276+
}
277+
return cty.StringVal(filepath.Dir(config.Dir())), nil
278+
}
279+
return cty.StringVal(home), nil
280+
},
281+
})
282+
}
283+
257284
func Stdlib() map[string]function.Function {
258285
funcs := make(map[string]function.Function, len(stdlibFunctions))
259286
for _, v := range stdlibFunctions {

bake/hclparser/stdlib_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hclparser
22

33
import (
4+
"path/filepath"
45
"testing"
56

67
"github.com/stretchr/testify/require"
@@ -197,3 +198,10 @@ func TestSanitize(t *testing.T) {
197198
})
198199
}
199200
}
201+
202+
func TestHomedir(t *testing.T) {
203+
home, err := homedirFunc().Call(nil)
204+
require.NoError(t, err)
205+
require.NotEmpty(t, home.AsString())
206+
require.True(t, filepath.IsAbs(home.AsString()))
207+
}

docs/bake-stdlib.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ title: Bake standard library functions
4343
| `greaterthan` | Returns true if and only if the second number is greater than the first. |
4444
| `greaterthanorequalto` | Returns true if and only if the second number is greater than or equal to the first. |
4545
| `hasindex` | Returns true if if the given collection can be indexed with the given key without producing an error, or false otherwise. |
46+
| `homedir` | Returns the current user's home directory. |
4647
| `indent` | Adds a given number of spaces after each newline character in the given string. |
4748
| `index` | Returns the element with the given key from the given collection, or raises an error if there is no such element. |
4849
| `indexof` | Finds the element index for a given value in a list. |

0 commit comments

Comments
 (0)