bake: allow attributes in global scope

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2021-02-10 18:46:03 -08:00
parent 0147b92230
commit 0fe2ce7fac
2 changed files with 116 additions and 4 deletions

View File

@@ -133,6 +133,8 @@ type StaticConfig struct {
Variables []*Variable `hcl:"variable,block"`
Remain hcl.Body `hcl:",remain"`
attrs hcl.Attributes
defaults map[string]*hcl.Attribute
env map[string]string
values map[string]cty.Value
@@ -146,6 +148,9 @@ func mergeStaticConfig(scs []*StaticConfig) *StaticConfig {
sc := scs[0]
for _, s := range scs[1:] {
sc.Variables = append(sc.Variables, s.Variables...)
for k, v := range s.attrs {
sc.attrs[k] = v
}
}
return sc
}
@@ -169,6 +174,12 @@ func (sc *StaticConfig) Values(withEnv bool) (map[string]cty.Value, error) {
sc.values = map[string]cty.Value{}
sc.progress = map[string]struct{}{}
for k := range sc.attrs {
if _, err := sc.resolveValue(k); err != nil {
return nil, err
}
}
for k := range sc.defaults {
if _, err := sc.resolveValue(k); err != nil {
return nil, err
@@ -192,10 +203,12 @@ func (sc *StaticConfig) resolveValue(name string) (v *cty.Value, err error) {
}
}()
def, ok := sc.defaults[name]
def, ok := sc.attrs[name]
if !ok {
return nil, errors.Errorf("undefined variable %q", name)
def, ok = sc.defaults[name]
if !ok {
return nil, errors.Errorf("undefined variable %q", name)
}
}
if def == nil {
@@ -233,7 +246,9 @@ func (sc *StaticConfig) resolveValue(name string) (v *cty.Value, err error) {
return nil, diags
}
if envv, ok := sc.env[name]; ok {
_, isVar := sc.defaults[name]
if envv, ok := sc.env[name]; ok && isVar {
if vv.Type().Equals(cty.Bool) {
b, err := strconv.ParseBool(envv)
if err != nil {
@@ -302,6 +317,16 @@ func parseHCLFile(dt []byte, fn string) (f *hcl.File, _ *StaticConfig, err error
return nil, nil, err
}
attrs, diags := f.Body.JustAttributes()
if diags.HasErrors() {
for _, d := range diags {
if d.Detail != "Blocks are not allowed here." {
return nil, nil, diags
}
}
}
sc.attrs = attrs
return f, &sc, nil
}