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

Method hcldecTransform

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

Source from the content-addressed store, hash-verified

311}
312
313func (r *Runner) hcldecTransform(specFile, inputFile string) (cty.Value, hcl.Diagnostics) {
314 var diags hcl.Diagnostics
315 var outBuffer bytes.Buffer
316 var errBuffer bytes.Buffer
317
318 cmd := &exec.Cmd{
319 Path: r.hcldecPath,
320 Args: []string{
321 r.hcldecPath,
322 "--spec=" + specFile,
323 "--diags=json",
324 "--with-type",
325 "--keep-nulls",
326 inputFile,
327 },
328 Stdout: &outBuffer,
329 Stderr: &errBuffer,
330 }
331 err := cmd.Run()
332 if err != nil {
333 if _, isExit := err.(*exec.ExitError); !isExit {
334 diags = append(diags, &hcl.Diagnostic{
335 Severity: hcl.DiagError,
336 Summary: "Failed to run hcldec",
337 Detail: fmt.Sprintf("Sub-program hcldec failed to start: %s.", err),
338 })
339 return cty.DynamicVal, diags
340 }
341
342 // If we exited unsuccessfully then we'll expect diagnostics on stderr
343 moreDiags := decodeJSONDiagnostics(errBuffer.Bytes())
344 diags = append(diags, moreDiags...)
345 return cty.DynamicVal, diags
346 } else {
347 // Otherwise, we expect a JSON result value on stdout. Since we used
348 // --with-type above, we can decode as DynamicPseudoType to recover
349 // exactly the type that was saved, without the usual JSON lossiness.
350 val, err := ctyjson.Unmarshal(outBuffer.Bytes(), cty.DynamicPseudoType)
351 if err != nil {
352 diags = append(diags, &hcl.Diagnostic{
353 Severity: hcl.DiagError,
354 Summary: "Failed to parse hcldec result",
355 Detail: fmt.Sprintf("Sub-program hcldec produced an invalid result: %s.", err),
356 })
357 return cty.DynamicVal, diags
358 }
359 return val, diags
360 }
361}
362
363func (r *Runner) hcldecVariables(specFile, inputFile string) ([]hcl.Traversal, hcl.Diagnostics) {
364 var diags hcl.Diagnostics

Callers 1

runTestInputMethod · 0.95

Calls 3

decodeJSONDiagnosticsFunction · 0.85
RunMethod · 0.80
BytesMethod · 0.45

Tested by

no test coverage detected