MCPcopy
hub / github.com/hashicorp/hcl / hcldecVariables

Method hcldecVariables

cmd/hclspecsuite/runner.go:363–507  ·  view source on GitHub ↗
(specFile, inputFile string)

Source from the content-addressed store, hash-verified

361}
362
363func (r *Runner) hcldecVariables(specFile, inputFile string) ([]hcl.Traversal, hcl.Diagnostics) {
364 var diags hcl.Diagnostics
365 var outBuffer bytes.Buffer
366 var errBuffer bytes.Buffer
367
368 cmd := &exec.Cmd{
369 Path: r.hcldecPath,
370 Args: []string{
371 r.hcldecPath,
372 "--spec=" + specFile,
373 "--diags=json",
374 "--var-refs",
375 inputFile,
376 },
377 Stdout: &outBuffer,
378 Stderr: &errBuffer,
379 }
380 err := cmd.Run()
381 if err != nil {
382 if _, isExit := err.(*exec.ExitError); !isExit {
383 diags = append(diags, &hcl.Diagnostic{
384 Severity: hcl.DiagError,
385 Summary: "Failed to run hcldec",
386 Detail: fmt.Sprintf("Sub-program hcldec (evaluating input) failed to start: %s.", err),
387 })
388 return nil, diags
389 }
390
391 // If we exited unsuccessfully then we'll expect diagnostics on stderr
392 moreDiags := decodeJSONDiagnostics(errBuffer.Bytes())
393 diags = append(diags, moreDiags...)
394 return nil, diags
395 } else {
396 // Otherwise, we expect a JSON description of the traversals on stdout.
397 type PosJSON struct {
398 Line int `json:"line"`
399 Column int `json:"column"`
400 Byte int `json:"byte"`
401 }
402 type RangeJSON struct {
403 Filename string `json:"filename"`
404 Start PosJSON `json:"start"`
405 End PosJSON `json:"end"`
406 }
407 type StepJSON struct {
408 Kind string `json:"kind"`
409 Name string `json:"name,omitempty"`
410 Key json.RawMessage `json:"key,omitempty"`
411 Range RangeJSON `json:"range"`
412 }
413 type TraversalJSON struct {
414 Steps []StepJSON `json:"steps"`
415 }
416
417 var raw []TraversalJSON
418 err := json.Unmarshal(outBuffer.Bytes(), &raw)
419 if err != nil {
420 diags = append(diags, &hcl.Diagnostic{

Callers 1

runTestInputMethod · 0.95

Calls 3

decodeJSONDiagnosticsFunction · 0.85
RunMethod · 0.80
BytesMethod · 0.45

Tested by

no test coverage detected