Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
73 changes: 73 additions & 0 deletions cmd/scw/web_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"bytes"
"reflect"
"testing"
"text/template"

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

func Test_WebValidateTemplates(t *testing.T) {
cmds := namespaces.GetCommands()

// Test that web urls are valid templates
type failedTemplate struct {
Cmd string
Err error
}
errs := []any(nil)

for _, cmd := range cmds.GetSortedCommand() {
if cmd.WebURL == "" {
continue
}
_, err := template.New("").Parse(cmd.WebURL)
if err != nil {
errs = append(errs, failedTemplate{
Cmd: cmd.GetCommandLine("scw"),
Err: err,
})
}
}
if len(errs) > 0 {
t.Fatal(errs...)
}
}

func Test_WebValidateTemplatesVariables(t *testing.T) {
cmds := namespaces.GetCommands()

// Test that web urls are valid templates
type failedTemplate struct {
Cmd string
Err error
}
errs := []any(nil)

for _, cmd := range cmds.GetSortedCommand() {
if cmd.WebURL == "" {
continue
}
tmpl, err := template.New("").Parse(cmd.WebURL)
if err != nil {
continue
}
var args interface{}
if cmd.ArgsType != nil {
args = reflect.New(cmd.ArgsType).Interface()
}

err = tmpl.Execute(bytes.NewBuffer(nil), args)
if err != nil {
errs = append(errs, failedTemplate{
Cmd: cmd.GetCommandLine("scw"),
Err: err,
})
}
}
if len(errs) > 0 {
t.Fatal(errs...)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/moby/buildkit v0.11.6
github.com/opencontainers/go-digest v1.0.0
github.com/pkg/errors v0.9.1
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.17.0.20230601085652-73d98e0bd94d
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/skeema/knownhosts v1.1.0 h1:Wvr9V0MxhjRbl3f9nMnKnFfiWTJmtECJ9Njkea3ysW0=
github.com/skeema/knownhosts v1.1.0/go.mod h1:sKFq3RD6/TKZkSWn8boUbDC7Qkgcv+8XXijpFO6roag=
github.com/smartystreets/assertions v1.0.0/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM=
Expand Down
12 changes: 5 additions & 7 deletions internal/core/cobra_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,7 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command, group
} else {
// If command is not runnable we create a default run function that
// will print usage of the parent command and exit with code 1
cobraCmd.RunE = func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
return err
}
return &CliError{Empty: true, Code: 1}
}
cobraCmd.RunE = cobraRunHelp(cmd)
}

// If a command has no groups, we add it to the available group.
Expand All @@ -176,6 +170,10 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command, group
}
cobraCmd.PersistentFlags().BoolP("wait", "w", false, waitUsage)
}

if commandHasWeb(cmd) {
cobraCmd.PersistentFlags().Bool("web", false, "open console page for the current ressource")
}
}

const usageTemplate = `USAGE:
Expand Down
25 changes: 25 additions & 0 deletions internal/core/cobra_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ func run(ctx context.Context, cobraCmd *cobra.Command, cmd *Command, rawArgs []s
return nil, err
}

webFlag, err := cobraCmd.PersistentFlags().GetBool("web")
if err == nil && webFlag {
return runWeb(cmd, cmdArgs)
}

// execute the command
interceptor := combineCommandInterceptor(
sdkStdErrorInterceptor,
Expand Down Expand Up @@ -228,3 +233,23 @@ Relative time error: %s
return &CliError{Err: unmarshalErr}
}
}

func cobraRunHelp(cmd *Command) func(cmd *cobra.Command, args []string) error {
return func(cobraCmd *cobra.Command, args []string) error {
webFlag, err := cobraCmd.PersistentFlags().GetBool("web")
if err == nil && webFlag {
out, err := runWeb(cmd, nil)
if err != nil {
return err
}
cobraCmd.Println(out)
return nil
}

err = cobraCmd.Help()
if err != nil {
return err
}
return &CliError{Empty: true, Code: 1}
}
}
4 changes: 4 additions & 0 deletions internal/core/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ type Command struct {
// WaitFunc will be called if non-nil when the -w (--wait) flag is passed.
WaitFunc WaitFunc

// WebURL will be used as url to open when the --web flag is passed
// Can contain template of values in request, ex: "url/{{ .Zone }}/{{ .ResourceID }}"
WebURL string

// WaitUsage override the usage for the -w (--wait) flag
WaitUsage string

Expand Down
43 changes: 43 additions & 0 deletions internal/core/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package core

import (
"bytes"
"fmt"
"text/template"

"github.com/skratchdot/open-golang/open"
)

func commandHasWeb(cmd *Command) bool {
return cmd.WebURL != ""
}

func runWeb(cmd *Command, respI interface{}) (interface{}, error) {
url := cmd.WebURL

if respI != nil {
tmpl, err := template.New("url").Parse(url)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(nil)
err = tmpl.Execute(buf, respI)
if err != nil {
return nil, err
}
url = buf.String()
}

err := open.Start(url)
if err != nil {
return nil, &CliError{
Err: err,
Message: "Failed to open web url",
Details: fmt.Sprintf("You can open it: %s", url),
Hint: "You may not have a default browser configured",
Code: 1,
}
}

return fmt.Sprintf("Opening %s\n", url), nil
}