-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (73 loc) · 1.79 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//go:build js && wasm
package main
import (
"context"
"encoding/json"
"fmt"
"io/fs"
"path/filepath"
"syscall/js"
"github.com/spf13/afero"
"github.com/coder/preview"
"github.com/coder/preview/types"
)
func main() {
// Create a channel to keep the Go program alive
done := make(chan struct{}, 0)
// Expose the Go function `fibonacciSum` to JavaScript
js.Global().Set("go_preview", js.FuncOf(tfpreview))
js.Global()
// Block the program from exiting
<-done
}
func tfpreview(this js.Value, p []js.Value) any {
tf, err := fileTreeFS(p[0])
if err != nil {
return err
}
output, diags := preview.Preview(context.Background(), preview.Input{
PlanJSONPath: "",
PlanJSON: nil,
ParameterValues: nil,
Owner: types.WorkspaceOwner{},
}, tf)
data, _ := json.Marshal(map[string]any{
"output": output,
"diags": diags,
})
return js.ValueOf(string(data))
}
func fileTreeFS(value js.Value) (fs.FS, error) {
data := js.Global().Get("JSON").Call("stringify", value).String()
var filetree map[string]any
if err := json.Unmarshal([]byte(data), &filetree); err != nil {
return nil, err
}
mem := afero.NewMemMapFs()
loadTree(mem, filetree)
return afero.NewIOFS(mem), nil
}
func loadTree(mem afero.Fs, fileTree map[string]any, path ...string) {
dir := filepath.Join(path...)
err := mem.MkdirAll(dir, 0755)
if err != nil {
fmt.Printf("error creating directory %q: %v\n", dir, err)
}
for k, v := range fileTree {
switch vv := v.(type) {
case string:
fn := filepath.Join(dir, k)
f, err := mem.Create(fn)
if err != nil {
fmt.Printf("error creating file %q: %v\n", fn, err)
continue
}
_, _ = f.WriteString(vv)
f.Close()
case map[string]any:
loadTree(mem, vv, append(path, k)...)
default:
fmt.Printf("unknown type %T for %q\n", v, k)
}
}
}