Skip to content

Commit 13755b3

Browse files
authored
feat(wasm): add jwt support (#3119)
1 parent 87a2c54 commit 13755b3

25 files changed

+518
-250
lines changed

cmd/scw-wasm/args.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
package main
44

5-
import "os"
5+
import (
6+
"os"
7+
)
68

79
type Args struct {
810
callback string

cmd/scw-wasm/async.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ package main
44

55
import (
66
"fmt"
7+
"runtime/debug"
78
"syscall/js"
9+
10+
"github.com/scaleway/scaleway-cli/v2/internal/jshelpers"
811
)
912

1013
type fn func(this js.Value, args []js.Value) (any, error)
1114

1215
var (
13-
jsErr js.Value = js.Global().Get("Error")
14-
jsObject js.Value = js.Global().Get("Object")
15-
jsPromise js.Value = js.Global().Get("Promise")
16+
jsErr = js.Global().Get("Error")
17+
jsPromise = js.Global().Get("Promise")
1618
)
1719

1820
func asyncFunc(innerFunc fn) js.Func {
@@ -23,15 +25,15 @@ func asyncFunc(innerFunc fn) js.Func {
2325
go func() {
2426
defer func() {
2527
if r := recover(); r != nil {
26-
reject.Invoke(jsErr.New(fmt.Sprint("panic:", r)))
28+
reject.Invoke(jshelpers.NewError(
29+
fmt.Sprintf("panic: %v\n%s", r, string(debug.Stack())),
30+
))
2731
}
2832
}()
2933

3034
res, err := innerFunc(this, args)
3135
if err != nil {
32-
errContent := jsObject.New()
33-
errContent.Set("cause", err.Error())
34-
reject.Invoke(jsErr.New(res, errContent))
36+
reject.Invoke(jshelpers.NewErrorWithCause(res, err.Error()))
3537
} else {
3638
resolve.Invoke(res)
3739
}

cmd/scw-wasm/main.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"syscall/js"
1010

1111
"github.com/scaleway/scaleway-cli/v2/internal/core"
12+
"github.com/scaleway/scaleway-cli/v2/internal/jshelpers"
1213
"github.com/scaleway/scaleway-cli/v2/internal/namespaces"
14+
"github.com/scaleway/scaleway-cli/v2/internal/platform/web"
1315
)
1416

1517
var commands *core.Commands
@@ -21,7 +23,11 @@ func getCommands() *core.Commands {
2123
return commands
2224
}
2325

24-
func runCommand(args []string, stdout io.Writer, stderr io.Writer) chan int {
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 {
2531
ret := make(chan int, 1)
2632
go func() {
2733
exitCode, _, _ := core.Bootstrap(&core.BootstrapConfig{
@@ -31,6 +37,9 @@ func runCommand(args []string, stdout io.Writer, stderr io.Writer) chan int {
3137
Stdout: stdout,
3238
Stderr: stderr,
3339
Stdin: nil,
40+
Platform: &web.Platform{
41+
JWT: cfg.JWT,
42+
},
3443
})
3544
ret <- exitCode
3645
}()
@@ -43,14 +52,23 @@ func wasmRun(this js.Value, args []js.Value) (any, error) {
4352
stdout := bytes.NewBuffer(nil)
4453
stderr := bytes.NewBuffer(nil)
4554

46-
for argIndex, arg := range args {
47-
if arg.Type() != js.TypeString {
48-
return nil, fmt.Errorf("invalid argument at index %d", argIndex)
49-
}
50-
cliArgs = append(cliArgs, arg.String())
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)
5167
}
5268

53-
exitCodeChan := runCommand(cliArgs, stdout, stderr)
69+
cliArgs = append(cliArgs, givenArgs...)
70+
71+
exitCodeChan := runCommand(runCfg, cliArgs, stdout, stderr)
5472
exitCode := <-exitCodeChan
5573
if exitCode != 0 {
5674
errBody := stderr.String()
@@ -77,5 +95,5 @@ func main() {
7795
givenCallback.Invoke()
7896
}
7997
}
80-
<-make(chan struct{}, 0)
98+
<-make(chan struct{})
8199
}

cmd/scw/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/mattn/go-colorable"
1111
"github.com/scaleway/scaleway-cli/v2/internal/core"
1212
"github.com/scaleway/scaleway-cli/v2/internal/namespaces"
13+
"github.com/scaleway/scaleway-cli/v2/internal/platform/terminal"
1314
"github.com/scaleway/scaleway-cli/v2/internal/sentry"
1415
"github.com/scaleway/scaleway-sdk-go/scw"
1516
)
@@ -76,6 +77,7 @@ func main() {
7677
Stderr: colorable.NewColorableStderr(),
7778
Stdin: os.Stdin,
7879
BetaMode: BetaMode,
80+
Platform: terminal.NewPlatform(buildInfo.GetUserAgent()),
7981
})
8082

8183
os.Exit(exitCode)

internal/core/autocomplete_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"testing"
99

10+
"github.com/scaleway/scaleway-cli/v2/internal/platform/terminal"
1011
"github.com/scaleway/scaleway-sdk-go/scw"
1112
"github.com/stretchr/testify/assert"
1213
)
@@ -265,6 +266,7 @@ func TestAutocompleteProfiles(t *testing.T) {
265266
ctx := injectMeta(context.Background(), &meta{
266267
Commands: commands,
267268
betaMode: true,
269+
Platform: terminal.NewPlatform(""),
268270
})
269271

270272
type testCase = autoCompleteTestCase

internal/core/bootstrap.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/scaleway/scaleway-cli/v2/internal/account"
1111
cliConfig "github.com/scaleway/scaleway-cli/v2/internal/config"
1212
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
13+
"github.com/scaleway/scaleway-cli/v2/internal/platform"
1314
"github.com/scaleway/scaleway-sdk-go/logger"
1415
"github.com/scaleway/scaleway-sdk-go/scw"
1516
"github.com/spf13/cobra"
@@ -68,6 +69,9 @@ type BootstrapConfig struct {
6869

6970
// Enable beta functionalities
7071
BetaMode bool
72+
73+
// The current platform, should probably be platform.Default
74+
Platform platform.Platform
7175
}
7276

7377
// Bootstrap is the main entry point. It is directly called from main.
@@ -166,6 +170,7 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
166170
OverrideExec: config.OverrideExec,
167171
ConfigPathFlag: configPathFlag,
168172
Logger: log,
173+
Platform: config.Platform,
169174

170175
stdout: config.Stdout,
171176
stderr: config.Stderr,

internal/core/checks_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ func TestCheckAPIKey(t *testing.T) {
6060
ctx.Meta[metadataKey] = apiKey
6161
cfg := &scw.Config{
6262
Profile: scw.Profile{
63-
AccessKey: &apiKey.AccessKey,
64-
SecretKey: apiKey.SecretKey,
63+
AccessKey: &apiKey.AccessKey,
64+
SecretKey: apiKey.SecretKey,
65+
DefaultProjectID: &apiKey.DefaultProjectID,
66+
DefaultOrganizationID: &apiKey.DefaultProjectID,
6567
},
6668
}
6769
configPath := filepath.Join(ctx.OverrideEnv["HOME"], ".config", "scw", "config.yaml")

0 commit comments

Comments
 (0)