Skip to content

Commit

Permalink
feat: Also log command stderr at TRACE level (#206)
Browse files Browse the repository at this point in the history
* feat: Also log command stderr at TRACE level
  • Loading branch information
pdecat committed Feb 12, 2024
1 parent 4365dbd commit 0998e0f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/NOTES-20240212-161634.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: NOTES
body: 'data-source/external: The stderr output of the executed program will now always
be logged at the TRACE level, regardless of exit code.'
time: 2024-02-12T16:16:34.536837-05:00
custom:
Issue: "67"
31 changes: 13 additions & 18 deletions internal/provider/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"os/exec"
"runtime"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -216,32 +217,25 @@ The program must also be executable according to the platform where Terraform is
cmd.Dir = workingDir
cmd.Stdin = bytes.NewReader(queryJson)

var stderr strings.Builder
cmd.Stderr = &stderr

tflog.Trace(ctx, "Executing external program", map[string]interface{}{"program": cmd.String()})

resultJson, err := cmd.Output()

tflog.Trace(ctx, "Executed external program", map[string]interface{}{"program": cmd.String(), "output": string(resultJson)})
stderrStr := stderr.String()

if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if exitErr.Stderr != nil && len(exitErr.Stderr) > 0 {
resp.Diagnostics.AddAttributeError(
path.Root("program"),
"External Program Execution Failed",
"The data source received an unexpected error while attempting to execute the program."+
fmt.Sprintf("\n\nProgram: %s", cmd.Path)+
fmt.Sprintf("\nError Message: %s", string(exitErr.Stderr))+
fmt.Sprintf("\nState: %s", err),
)
return
}
tflog.Trace(ctx, "Executed external program", map[string]interface{}{"program": cmd.String(), "output": string(resultJson), "stderr": stderrStr})

if err != nil {
if len(stderrStr) > 0 {
resp.Diagnostics.AddAttributeError(
path.Root("program"),
"External Program Execution Failed",
"The data source received an unexpected error while attempting to execute the program.\n\n"+
"The program was executed, however it returned no additional error messaging."+
"The data source received an unexpected error while attempting to execute the program."+
fmt.Sprintf("\n\nProgram: %s", cmd.Path)+
fmt.Sprintf("\nError Message: %s", stderrStr)+
fmt.Sprintf("\nState: %s", err),
)
return
Expand All @@ -250,9 +244,10 @@ The program must also be executable according to the platform where Terraform is
resp.Diagnostics.AddAttributeError(
path.Root("program"),
"External Program Execution Failed",
"The data source received an unexpected error while attempting to execute the program."+
"The data source received an unexpected error while attempting to execute the program.\n\n"+
"The program was executed, however it returned no additional error messaging."+
fmt.Sprintf("\n\nProgram: %s", cmd.Path)+
fmt.Sprintf("\nError: %s", err),
fmt.Sprintf("\nState: %s", err),
)
return
}
Expand Down

0 comments on commit 0998e0f

Please sign in to comment.