|
3 | 3 | package main |
4 | 4 |
|
5 | 5 | import ( |
6 | | - "bytes" |
7 | | - "fmt" |
8 | | - "io" |
9 | 6 | "syscall/js" |
10 | 7 |
|
11 | | - "github.com/scaleway/scaleway-cli/v2/internal/core" |
12 | 8 | "github.com/scaleway/scaleway-cli/v2/internal/jshelpers" |
13 | | - "github.com/scaleway/scaleway-cli/v2/internal/namespaces" |
14 | | - "github.com/scaleway/scaleway-cli/v2/internal/platform/web" |
| 9 | + "github.com/scaleway/scaleway-cli/v2/internal/wasm" |
15 | 10 | ) |
16 | 11 |
|
17 | | -var commands *core.Commands |
18 | | - |
19 | | -func getCommands() *core.Commands { |
20 | | - if commands == nil { |
21 | | - commands = namespaces.GetCommands() |
22 | | - } |
23 | | - return commands |
24 | | -} |
25 | | - |
26 | | -type RunConfig struct { |
27 | | - JWT string `js:"jwt"` |
28 | | -} |
29 | | - |
30 | | -func runCommand(cfg *RunConfig, args []string, stdout io.Writer, stderr io.Writer) chan int { |
31 | | - ret := make(chan int, 1) |
32 | | - go func() { |
33 | | - exitCode, _, _ := core.Bootstrap(&core.BootstrapConfig{ |
34 | | - Args: args, |
35 | | - Commands: getCommands(), |
36 | | - BuildInfo: &core.BuildInfo{}, |
37 | | - Stdout: stdout, |
38 | | - Stderr: stderr, |
39 | | - Stdin: nil, |
40 | | - Platform: &web.Platform{ |
41 | | - JWT: cfg.JWT, |
42 | | - }, |
43 | | - }) |
44 | | - ret <- exitCode |
45 | | - }() |
46 | | - |
47 | | - return ret |
48 | | -} |
49 | | - |
50 | | -func wasmRun(this js.Value, args []js.Value) (any, error) { |
51 | | - cliArgs := []string{"scw"} |
52 | | - stdout := bytes.NewBuffer(nil) |
53 | | - stderr := bytes.NewBuffer(nil) |
54 | | - |
55 | | - if len(args) < 2 { |
56 | | - return nil, fmt.Errorf("not enough arguments") |
57 | | - } |
58 | | - |
59 | | - runCfg, err := jshelpers.AsObject[RunConfig](args[0]) |
60 | | - if err != nil { |
61 | | - return nil, fmt.Errorf("invalid config given: %w", err) |
62 | | - } |
63 | | - |
64 | | - givenArgs, err := jshelpers.AsSlice[string](args[1]) |
65 | | - if err != nil { |
66 | | - return nil, fmt.Errorf("invalid args given: %w", err) |
67 | | - } |
68 | | - |
69 | | - cliArgs = append(cliArgs, givenArgs...) |
70 | | - |
71 | | - exitCodeChan := runCommand(runCfg, cliArgs, stdout, stderr) |
72 | | - exitCode := <-exitCodeChan |
73 | | - if exitCode != 0 { |
74 | | - errBody := stderr.String() |
75 | | - return js.ValueOf(errBody), fmt.Errorf("exit code: %d", exitCode) |
76 | | - } |
77 | | - |
78 | | - outBody := stdout.String() |
79 | | - |
80 | | - return js.ValueOf(outBody), nil |
81 | | -} |
82 | | - |
83 | 12 | func main() { |
84 | 13 | args := getArgs() |
85 | 14 |
|
86 | 15 | if args.targetObject != "" { |
87 | 16 | cliPackage := js.ValueOf(map[string]any{}) |
88 | | - cliPackage.Set("run", asyncFunc(wasmRun)) |
| 17 | + cliPackage.Set("run", asyncFunc(jshelpers.AsFunction(wasm.Run))) |
89 | 18 | js.Global().Set(args.targetObject, cliPackage) |
90 | 19 | } |
91 | 20 |
|
|
0 commit comments