@@ -2,10 +2,15 @@ package hclparser
22
33import (
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+
257284func Stdlib () map [string ]function.Function {
258285 funcs := make (map [string ]function.Function , len (stdlibFunctions ))
259286 for _ , v := range stdlibFunctions {
0 commit comments