github.com/avenga/couper@v1.12.2/config/configload/environment.go (about) 1 package configload 2 3 import ( 4 "fmt" 5 "github.com/hashicorp/hcl/v2/hclsyntax" 6 ) 7 8 func preprocessEnvironmentBlocks(bodies []*hclsyntax.Body, env string) error { 9 found := false 10 for _, body := range bodies { 11 f, err := preprocessBody(body, env) 12 if err != nil { 13 return err 14 } 15 found = found || f 16 } 17 18 if found && env == "" { 19 return fmt.Errorf(`"environment" blocks found, but "COUPER_ENVIRONMENT" setting is missing`) 20 } 21 22 return nil 23 } 24 25 func preprocessBody(body *hclsyntax.Body, env string) (bool, error) { 26 var blocks []*hclsyntax.Block 27 found := false 28 29 for _, block := range body.Blocks { 30 if block.Type != environment { 31 blocks = append(blocks, block) 32 continue 33 } 34 35 found = true 36 37 if len(block.Labels) == 0 { 38 defRange := block.DefRange() 39 return true, newDiagErr(&defRange, "Missing label(s) for 'environment' block") 40 } 41 42 for i, label := range block.Labels { 43 if err := validLabel(label, &block.LabelRanges[i]); err != nil { 44 return true, err 45 } 46 47 if label == env { 48 blocks = append(blocks, block.Body.Blocks...) 49 50 for name, attr := range block.Body.Attributes { 51 body.Attributes[name] = attr 52 } 53 } 54 } 55 } 56 57 for _, block := range blocks { 58 foundInChildren, err := preprocessBody(block.Body, env) 59 if err != nil { 60 return found || foundInChildren, err 61 } 62 found = found || foundInChildren 63 } 64 65 body.Blocks = blocks 66 67 return found, nil 68 }