Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
4 changes: 3 additions & 1 deletion cmd/scw-wasm/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

package main

import "os"
import (
"os"
)

type Args struct {
callback string
Expand Down
16 changes: 9 additions & 7 deletions cmd/scw-wasm/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ package main

import (
"fmt"
"runtime/debug"
"syscall/js"

"github.com/scaleway/scaleway-cli/v2/internal/jshelpers"
)

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

var (
jsErr js.Value = js.Global().Get("Error")
jsObject js.Value = js.Global().Get("Object")
jsPromise js.Value = js.Global().Get("Promise")
jsErr = js.Global().Get("Error")
jsPromise = js.Global().Get("Promise")
)

func asyncFunc(innerFunc fn) js.Func {
Expand All @@ -23,15 +25,15 @@ func asyncFunc(innerFunc fn) js.Func {
go func() {
defer func() {
if r := recover(); r != nil {
reject.Invoke(jsErr.New(fmt.Sprint("panic:", r)))
reject.Invoke(jshelpers.NewError(
fmt.Sprintf("panic: %v\n%s", r, string(debug.Stack())),
))
}
}()

res, err := innerFunc(this, args)
if err != nil {
errContent := jsObject.New()
errContent.Set("cause", err.Error())
reject.Invoke(jsErr.New(res, errContent))
reject.Invoke(jshelpers.NewErrorWithCause(res, err.Error()))
} else {
resolve.Invoke(res)
}
Expand Down
34 changes: 26 additions & 8 deletions cmd/scw-wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"syscall/js"

"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/jshelpers"
"github.com/scaleway/scaleway-cli/v2/internal/namespaces"
"github.com/scaleway/scaleway-cli/v2/internal/platform/web"
)

var commands *core.Commands
Expand All @@ -21,7 +23,11 @@ func getCommands() *core.Commands {
return commands
}

func runCommand(args []string, stdout io.Writer, stderr io.Writer) chan int {
type RunConfig struct {
JWT string `js:"jwt"`
}

func runCommand(cfg *RunConfig, args []string, stdout io.Writer, stderr io.Writer) chan int {
ret := make(chan int, 1)
go func() {
exitCode, _, _ := core.Bootstrap(&core.BootstrapConfig{
Expand All @@ -31,6 +37,9 @@ func runCommand(args []string, stdout io.Writer, stderr io.Writer) chan int {
Stdout: stdout,
Stderr: stderr,
Stdin: nil,
Platform: &web.Platform{
JWT: cfg.JWT,
},
})
ret <- exitCode
}()
Expand All @@ -43,14 +52,23 @@ func wasmRun(this js.Value, args []js.Value) (any, error) {
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)

for argIndex, arg := range args {
if arg.Type() != js.TypeString {
return nil, fmt.Errorf("invalid argument at index %d", argIndex)
}
cliArgs = append(cliArgs, arg.String())
if len(args) < 2 {
return nil, fmt.Errorf("not enough arguments")
}

runCfg, err := jshelpers.AsObject[RunConfig](args[0])
if err != nil {
return nil, fmt.Errorf("invalid config given: %w", err)
}

givenArgs, err := jshelpers.AsSlice[string](args[1])
if err != nil {
return nil, fmt.Errorf("invalid args given: %w", err)
}

exitCodeChan := runCommand(cliArgs, stdout, stderr)
cliArgs = append(cliArgs, givenArgs...)

exitCodeChan := runCommand(runCfg, cliArgs, stdout, stderr)
exitCode := <-exitCodeChan
if exitCode != 0 {
errBody := stderr.String()
Expand All @@ -77,5 +95,5 @@ func main() {
givenCallback.Invoke()
}
}
<-make(chan struct{}, 0)
<-make(chan struct{})
}
2 changes: 2 additions & 0 deletions cmd/scw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/mattn/go-colorable"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/namespaces"
"github.com/scaleway/scaleway-cli/v2/internal/platform/terminal"
"github.com/scaleway/scaleway-cli/v2/internal/sentry"
"github.com/scaleway/scaleway-sdk-go/scw"
)
Expand Down Expand Up @@ -76,6 +77,7 @@ func main() {
Stderr: colorable.NewColorableStderr(),
Stdin: os.Stdin,
BetaMode: BetaMode,
Platform: terminal.NewPlatform(buildInfo.GetUserAgent()),
})

os.Exit(exitCode)
Expand Down
2 changes: 2 additions & 0 deletions internal/core/autocomplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

"github.com/scaleway/scaleway-cli/v2/internal/platform/terminal"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -265,6 +266,7 @@ func TestAutocompleteProfiles(t *testing.T) {
ctx := injectMeta(context.Background(), &meta{
Commands: commands,
betaMode: true,
Platform: terminal.NewPlatform(""),
})

type testCase = autoCompleteTestCase
Expand Down
5 changes: 5 additions & 0 deletions internal/core/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/scaleway/scaleway-cli/v2/internal/account"
cliConfig "github.com/scaleway/scaleway-cli/v2/internal/config"
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
"github.com/scaleway/scaleway-cli/v2/internal/platform"
"github.com/scaleway/scaleway-sdk-go/logger"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -68,6 +69,9 @@ type BootstrapConfig struct {

// Enable beta functionalities
BetaMode bool

// The current platform, should probably be platform.Default
Platform platform.Platform
}

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

stdout: config.Stdout,
stderr: config.Stderr,
Expand Down
6 changes: 4 additions & 2 deletions internal/core/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ func TestCheckAPIKey(t *testing.T) {
ctx.Meta[metadataKey] = apiKey
cfg := &scw.Config{
Profile: scw.Profile{
AccessKey: &apiKey.AccessKey,
SecretKey: apiKey.SecretKey,
AccessKey: &apiKey.AccessKey,
SecretKey: apiKey.SecretKey,
DefaultProjectID: &apiKey.DefaultProjectID,
DefaultOrganizationID: &apiKey.DefaultProjectID,
},
}
configPath := filepath.Join(ctx.OverrideEnv["HOME"], ".config", "scw", "config.yaml")
Expand Down
Loading