| 146 | } |
| 147 | attrs, diag := f.Body.JustAttributes() |
| 148 | if diag.HasErrors() { |
| 149 | return nil, fmt.Errorf("failed to get attributes from %s: %w", fullPath, diag) |
| 150 | } |
| 151 | |
| 152 | return attrs, nil |
| 153 | } |
| 154 | |
| 155 | func parseVariables(infraDir string) (map[string]*VarSchema, error) { |
| 156 | varsFile := filepath.Join(infraDir, "variables.tf") |
| 157 | src, err := os.ReadFile(varsFile) |
| 158 | if err != nil { |
| 159 | return nil, fmt.Errorf("failed to read %s: %w", varsFile, err) |
| 160 | } |
| 161 | |
| 162 | //nolint:exhaustruct |
| 163 | f, diag := hclsyntax.ParseConfig(src, varsFile, hcl.Pos{Line: 1, Column: 1}) |
| 164 | if diag.HasErrors() { |
| 165 | return nil, fmt.Errorf("failed to parse %s: %w", varsFile, diag) |
| 166 | } |
| 167 | |
| 168 | var body *hclsyntax.Body |
| 169 | switch b := f.Body.(type) { |
| 170 | case *hclsyntax.Body: |
| 171 | body = b |
| 172 | default: |
| 173 | panic(fmt.Sprintf("unexpected body type %T", f.Body)) |
| 174 | } |
| 175 | |
| 176 | schemas := make(map[string]*VarSchema) |
| 177 | for _, block := range body.Blocks { |
| 178 | if block.Type != "variable" { |
| 179 | continue |
| 180 | } |
| 181 | varName := block.Labels[0] |
| 182 | //nolint:exhaustruct |
| 183 | v := &VarSchema{ |
| 184 | Name: varName, |
| 185 | IsMandatory: true, |
| 186 | } |
| 187 | if typeAttr, ok := block.Body.Attributes["type"]; ok { |
| 188 | v.Type = parseTypeExpr(typeAttr.Expr) |
| 189 | } |
| 190 | if _, ok := block.Body.Attributes["default"]; ok { |
| 191 | v.IsMandatory = false |
| 192 | } |
| 193 | schemas[varName] = v |