bake: allow BAKE_CMD_CONTEXT builtin var

Allows accessing the main context for bake command from bake
file that has been imported remotely.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2021-07-12 17:53:04 -07:00
parent 5f6ad50df4
commit 1bb425a882
5 changed files with 113 additions and 30 deletions

View File

@ -17,6 +17,7 @@ import (
type Opt struct {
LookupVar func(string) (string, bool)
Vars map[string]string
}
type variable struct {
@ -178,7 +179,7 @@ func (p *parser) resolveValue(name string) (err error) {
}()
def, ok := p.attrs[name]
if !ok {
if _, builtin := p.opt.Vars[name]; !ok && !builtin {
vr, ok := p.vars[name]
if !ok {
return errors.Errorf("undefined variable %q", name)
@ -187,7 +188,10 @@ func (p *parser) resolveValue(name string) (err error) {
}
if def == nil {
val, _ := p.opt.LookupVar(name)
val, ok := p.opt.Vars[name]
if !ok {
val, _ = p.opt.LookupVar(name)
}
vv := cty.StringVal(val)
v = &vv
return
@ -243,6 +247,9 @@ func Parse(b hcl.Body, opt Opt, val interface{}) hcl.Diagnostics {
for _, bs := range schema.Blocks {
reserved[bs.Type] = struct{}{}
}
for k := range opt.Vars {
reserved[k] = struct{}{}
}
var defs inputs
if err := gohcl.DecodeBody(b, nil, &defs); err != nil {
@ -303,6 +310,10 @@ func Parse(b hcl.Body, opt Opt, val interface{}) hcl.Diagnostics {
}
delete(p.attrs, "function")
for k := range p.opt.Vars {
_ = p.resolveValue(k)
}
for k := range p.attrs {
if err := p.resolveValue(k); err != nil {
if diags, ok := err.(hcl.Diagnostics); ok {