diff --git a/Makefile b/Makefile index 1c57243..51551a5 100644 --- a/Makefile +++ b/Makefile @@ -3,4 +3,4 @@ build-wasm: build/preview.wasm mkdir -p public/build build/preview.wasm: $(GO_SRC_FILES) - GOOS=js GOARCH=wasm go build -C ./preview -o ./public/build/preview.wasm . + GOOS=js GOARCH=wasm go build -C ./preview -o ../public/build/preview.wasm . diff --git a/preview/main.go b/preview/main.go index 9ac14f0..39ee27c 100644 --- a/preview/main.go +++ b/preview/main.go @@ -3,13 +3,16 @@ package main import ( + "bytes" "context" "encoding/json" "fmt" "io/fs" + "log/slog" "path/filepath" "syscall/js" + "github.com/hashicorp/hcl/v2" "github.com/spf13/afero" "github.com/coder/preview" @@ -28,23 +31,60 @@ func main() { <-done } -func tfpreview(this js.Value, p []js.Value) any { +type previewOutput struct { + Output *preview.Output `json:"output"` + Diags types.Diagnostics `json:"diags"` + // ParserLogs are trivy logs that occur during parsing the + // Terraform files. This is useful for debugging issues with the + // invalid terraform syntax. + ParserLogs string `json:"parser_logs,omitempty"` +} + +func tfpreview(this js.Value, p []js.Value) (output any) { + var buf bytes.Buffer + defer func() { + // Return a panic as a diagnostic if one occurs. + if r := recover(); r != nil { + data, _ := json.Marshal(previewOutput{ + Output: nil, + ParserLogs: buf.String(), + Diags: types.Diagnostics{ + { + Severity: hcl.DiagError, + Summary: "A panic occurred", + Detail: fmt.Sprintf("%v", r), + Extra: types.DiagnosticExtra{}, + }, + }, + }) + + output = js.ValueOf(string(data)) + } + }() + tf, err := fileTreeFS(p[0]) if err != nil { return err } - output, diags := preview.Preview(context.Background(), preview.Input{ + logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{ + AddSource: false, + Level: slog.LevelDebug, + })) + pOutput, diags := preview.Preview(context.Background(), preview.Input{ PlanJSONPath: "", PlanJSON: nil, ParameterValues: nil, Owner: types.WorkspaceOwner{}, + Logger: logger, }, tf) - data, _ := json.Marshal(map[string]any{ - "output": output, - "diags": diags, + data, _ := json.Marshal(previewOutput{ + Output: pOutput, + Diags: types.Diagnostics(diags), + ParserLogs: buf.String(), }) + return js.ValueOf(string(data)) } diff --git a/preview/public/build/preview.wasm b/preview/public/build/preview.wasm deleted file mode 100755 index d17f34c..0000000 Binary files a/preview/public/build/preview.wasm and /dev/null differ diff --git a/public/build/preview.wasm b/public/build/preview.wasm index 4131faf..9de3e69 100755 Binary files a/public/build/preview.wasm and b/public/build/preview.wasm differ diff --git a/src/Preview.tsx b/src/Preview.tsx index 9f69411..86b9e03 100644 --- a/src/Preview.tsx +++ b/src/Preview.tsx @@ -22,7 +22,9 @@ export const Preview: FC = () => { const getOutput = async () => { try { - const output = await window.go_preview?.(debouncedCode); + const output = await window.go_preview?.({ + "main.tf": debouncedCode + }); if (output === undefined) { console.error("Something went wrong");