Skip to content

Commit

Permalink
localplatform HCL func
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Sep 2, 2021
1 parent a8a3b17 commit 32561e6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
21 changes: 21 additions & 0 deletions bake/hclparser/builtin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hclparser

import (
"github.com/containerd/containerd/platforms"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)

var builtinFunctions = map[string]function.Function{
"localplatform": localplatformFunc,
}

// localplatformFunc returns the current platform's default
// platform specification (e.g. linux/amd64).
var localplatformFunc = function.New(&function.Spec{
Params: []function.Parameter{},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
return cty.StringVal(platforms.DefaultString()), nil
},
})
14 changes: 14 additions & 0 deletions bake/hclparser/builtin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package hclparser

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLocalPlatform(t *testing.T) {
p, err := localplatformFunc.Call(nil)
require.NoError(t, err)
assert.NotEmpty(t, p)
}
7 changes: 6 additions & 1 deletion bake/hclparser/hclparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ func Parse(b hcl.Body, opt Opt, val interface{}) hcl.Diagnostics {
}
}

funcs := stdlibFunctions
for k, f := range builtinFunctions {
funcs[k] = f
}

p := &parser{
opt: opt,

Expand All @@ -274,7 +279,7 @@ func Parse(b hcl.Body, opt Opt, val interface{}) hcl.Diagnostics {
doneF: map[string]struct{}{},
ectx: &hcl.EvalContext{
Variables: map[string]cty.Value{},
Functions: stdlibFunctions,
Functions: funcs,
},
}

Expand Down

0 comments on commit 32561e6

Please sign in to comment.