Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Also log command stderr at TRACE level #206

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading