Skip to content

Commit e2e4a9f

Browse files
authored
Merge branch 'master' into v1.3646.0
2 parents d444d5d + a41fc4e commit e2e4a9f

File tree

7 files changed

+153
-7
lines changed

7 files changed

+153
-7
lines changed

cmd/scw/web_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"reflect"
6+
"testing"
7+
"text/template"
8+
9+
"github.com/scaleway/scaleway-cli/v2/internal/namespaces"
10+
)
11+
12+
func Test_WebValidateTemplates(t *testing.T) {
13+
cmds := namespaces.GetCommands()
14+
15+
// Test that web urls are valid templates
16+
type failedTemplate struct {
17+
Cmd string
18+
Err error
19+
}
20+
errs := []any(nil)
21+
22+
for _, cmd := range cmds.GetSortedCommand() {
23+
if cmd.WebURL == "" {
24+
continue
25+
}
26+
_, err := template.New("").Parse(cmd.WebURL)
27+
if err != nil {
28+
errs = append(errs, failedTemplate{
29+
Cmd: cmd.GetCommandLine("scw"),
30+
Err: err,
31+
})
32+
}
33+
}
34+
if len(errs) > 0 {
35+
t.Fatal(errs...)
36+
}
37+
}
38+
39+
func Test_WebValidateTemplatesVariables(t *testing.T) {
40+
cmds := namespaces.GetCommands()
41+
42+
// Test that web urls are valid templates
43+
type failedTemplate struct {
44+
Cmd string
45+
Err error
46+
}
47+
errs := []any(nil)
48+
49+
for _, cmd := range cmds.GetSortedCommand() {
50+
if cmd.WebURL == "" {
51+
continue
52+
}
53+
tmpl, err := template.New("").Parse(cmd.WebURL)
54+
if err != nil {
55+
continue
56+
}
57+
var args interface{}
58+
if cmd.ArgsType != nil {
59+
args = reflect.New(cmd.ArgsType).Interface()
60+
}
61+
62+
err = tmpl.Execute(bytes.NewBuffer(nil), args)
63+
if err != nil {
64+
errs = append(errs, failedTemplate{
65+
Cmd: cmd.GetCommandLine("scw"),
66+
Err: err,
67+
})
68+
}
69+
}
70+
if len(errs) > 0 {
71+
t.Fatal(errs...)
72+
}
73+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/opencontainers/go-digest v1.0.0
2626
github.com/pkg/errors v0.9.1
2727
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.17.0.20230605161902-063dd98719c5
28+
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
2829
github.com/spf13/cobra v1.7.0
2930
github.com/spf13/pflag v1.0.5
3031
github.com/stretchr/testify v1.8.4

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
512512
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
513513
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
514514
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
515+
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
516+
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
515517
github.com/skeema/knownhosts v1.1.0 h1:Wvr9V0MxhjRbl3f9nMnKnFfiWTJmtECJ9Njkea3ysW0=
516518
github.com/skeema/knownhosts v1.1.0/go.mod h1:sKFq3RD6/TKZkSWn8boUbDC7Qkgcv+8XXijpFO6roag=
517519
github.com/smartystreets/assertions v1.0.0/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM=

internal/core/cobra_builder.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,7 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command, group
144144
} else {
145145
// If command is not runnable we create a default run function that
146146
// will print usage of the parent command and exit with code 1
147-
cobraCmd.RunE = func(cmd *cobra.Command, args []string) error {
148-
err := cmd.Help()
149-
if err != nil {
150-
return err
151-
}
152-
return &CliError{Empty: true, Code: 1}
153-
}
147+
cobraCmd.RunE = cobraRunHelp(cmd)
154148
}
155149

156150
// If a command has no groups, we add it to the available group.
@@ -176,6 +170,10 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command, group
176170
}
177171
cobraCmd.PersistentFlags().BoolP("wait", "w", false, waitUsage)
178172
}
173+
174+
if commandHasWeb(cmd) {
175+
cobraCmd.PersistentFlags().Bool("web", false, "open console page for the current ressource")
176+
}
179177
}
180178

181179
const usageTemplate = `USAGE:

internal/core/cobra_utils.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ func run(ctx context.Context, cobraCmd *cobra.Command, cmd *Command, rawArgs []s
135135
return nil, err
136136
}
137137

138+
webFlag, err := cobraCmd.PersistentFlags().GetBool("web")
139+
if err == nil && webFlag {
140+
return runWeb(cmd, cmdArgs)
141+
}
142+
138143
// execute the command
139144
interceptor := combineCommandInterceptor(
140145
sdkStdErrorInterceptor,
@@ -228,3 +233,23 @@ Relative time error: %s
228233
return &CliError{Err: unmarshalErr}
229234
}
230235
}
236+
237+
func cobraRunHelp(cmd *Command) func(cmd *cobra.Command, args []string) error {
238+
return func(cobraCmd *cobra.Command, args []string) error {
239+
webFlag, err := cobraCmd.PersistentFlags().GetBool("web")
240+
if err == nil && webFlag {
241+
out, err := runWeb(cmd, nil)
242+
if err != nil {
243+
return err
244+
}
245+
cobraCmd.Println(out)
246+
return nil
247+
}
248+
249+
err = cobraCmd.Help()
250+
if err != nil {
251+
return err
252+
}
253+
return &CliError{Empty: true, Code: 1}
254+
}
255+
}

internal/core/command.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ type Command struct {
7575
// WaitFunc will be called if non-nil when the -w (--wait) flag is passed.
7676
WaitFunc WaitFunc
7777

78+
// WebURL will be used as url to open when the --web flag is passed
79+
// Can contain template of values in request, ex: "url/{{ .Zone }}/{{ .ResourceID }}"
80+
WebURL string
81+
7882
// WaitUsage override the usage for the -w (--wait) flag
7983
WaitUsage string
8084

internal/core/web.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package core
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"text/template"
7+
8+
"github.com/skratchdot/open-golang/open"
9+
)
10+
11+
func commandHasWeb(cmd *Command) bool {
12+
return cmd.WebURL != ""
13+
}
14+
15+
func runWeb(cmd *Command, respI interface{}) (interface{}, error) {
16+
url := cmd.WebURL
17+
18+
if respI != nil {
19+
tmpl, err := template.New("url").Parse(url)
20+
if err != nil {
21+
return nil, err
22+
}
23+
buf := bytes.NewBuffer(nil)
24+
err = tmpl.Execute(buf, respI)
25+
if err != nil {
26+
return nil, err
27+
}
28+
url = buf.String()
29+
}
30+
31+
err := open.Start(url)
32+
if err != nil {
33+
return nil, &CliError{
34+
Err: err,
35+
Message: "Failed to open web url",
36+
Details: fmt.Sprintf("You can open it: %s", url),
37+
Hint: "You may not have a default browser configured",
38+
Code: 1,
39+
}
40+
}
41+
42+
return fmt.Sprintf("Opening %s\n", url), nil
43+
}

0 commit comments

Comments
 (0)