Skip to content

Commit 96db741

Browse files
authored
Merge branch 'v2' into config_get
2 parents 039a797 + 5b9cc8a commit 96db741

29 files changed

+3560
-471
lines changed

cmd/scw/testdata/test-all-usage-baremetal-server-install-usage.stderr.golden

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ ARGS:
77
server-id Server ID to install
88
os-id ID of the OS to install on the server
99
hostname Hostname of the server
10-
ssh-key-ids.{index} SSH key IDs authorized on the server
10+
[all-ssh-keys] Add all SSH keys on your baremetal instance (cannot be used with ssh-key-ids)
11+
ssh-key-ids.{index} SSH key IDs authorized on the server (cannot be used with all-ssh-keys)
1112
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config (fr-par-2)
1213

1314
FLAGS:

cmd/scw/testdata/test-all-usage-marketplace-image-usage.stderr.golden

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Marketplace images can be used when creating an instance.
2+
13
USAGE:
24
scw marketplace image <command>
35

cmd/scw/testdata/test-all-usage-marketplace-usage.stderr.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ USAGE:
44
scw marketplace <command>
55

66
AVAILABLE COMMANDS:
7-
image
7+
image Marketplace images can be used when creating an instance
88

99
FLAGS:
1010
-h, --help help for marketplace

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ require (
1616
github.com/fatih/color v1.9.0
1717
github.com/getsentry/raven-go v0.2.0
1818
github.com/gorilla/websocket v1.4.2
19-
github.com/hashicorp/go-multierror v1.0.0
2019
github.com/hashicorp/go-version v1.2.0
2120
github.com/kr/pretty v0.1.0 // indirect
2221
github.com/mattn/go-colorable v0.1.4

go.sum

+2-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JY
3434
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
3535
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
3636
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
37-
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
38-
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
39-
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
40-
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
4137
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
4238
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
4339
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
@@ -65,6 +61,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
6561
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
6662
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200511151306-5e35a6b50424 h1:daVeC8IxL9EA3ASo38uwQm2nGcprn0L0XMb8Sj5DL4Y=
6763
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200511151306-5e35a6b50424/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
64+
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
6865
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
6966
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
7067
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
@@ -95,6 +92,7 @@ golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwg
9592
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9693
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
9794
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
95+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
9896
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9997
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
10098
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=

internal/core/bootstrap.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,12 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
9595
}
9696
interactive.SetOutputWriter(config.Stderr) // set printer for interactive function (always stderr).
9797

98+
// An authenticated client will be created later if required.
9899
client := config.Client
100+
isClientFromBootstrapConfig := true
99101
if client == nil {
100-
// Create scw client
101-
client, err = createClient(config.BuildInfo, profileName)
102+
isClientFromBootstrapConfig = false
103+
client, err = createAnonymousClient(config.BuildInfo)
102104
if err != nil {
103105
printErr := globalPrinter.Print(err, nil)
104106
if printErr != nil {
@@ -114,15 +116,17 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
114116
ProfileFlag: profileName,
115117
BinaryName: config.Args[0],
116118
BuildInfo: config.BuildInfo,
117-
stdout: config.Stdout,
118-
stderr: config.Stderr,
119119
Client: client,
120120
Commands: config.Commands,
121121
Printer: globalPrinter,
122122
OverrideEnv: config.OverrideEnv,
123123
OverrideExec: config.OverrideExec,
124-
result: nil, // result is later injected by cobra_utils.go/cobraRun()
125-
command: nil, // command is later injected by cobra_utils.go/cobraRun()
124+
125+
stdout: config.Stdout,
126+
stderr: config.Stderr,
127+
result: nil, // result is later injected by cobra_utils.go/cobraRun()
128+
command: nil, // command is later injected by cobra_utils.go/cobraRun()
129+
isClientFromBootstrapConfig: isClientFromBootstrapConfig,
126130
}
127131
// We make sure OverrideEnv is never nil in meta.
128132
if meta.OverrideEnv == nil {

internal/core/bootstrap_test.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"reflect"
66
"testing"
77

8+
"github.com/alecthomas/assert"
89
"github.com/scaleway/scaleway-cli/internal/args"
910
"github.com/scaleway/scaleway-cli/internal/interactive"
1011
)
@@ -44,7 +45,7 @@ func TestInterruptError(t *testing.T) {
4445
Cmd: "scw test code error",
4546
Check: TestCheckExitCode(99),
4647
}))
47-
t.Run("emtpy-error", Test(&TestConfig{
48+
t.Run("empty-error", Test(&TestConfig{
4849
Commands: NewCommands(
4950
&Command{
5051
Namespace: "test",
@@ -64,7 +65,7 @@ func TestInterruptError(t *testing.T) {
6465
TestCheckStderrGolden(),
6566
),
6667
}))
67-
t.Run("emtpy-error-json", Test(&TestConfig{
68+
t.Run("empty-error-json", Test(&TestConfig{
6869
Commands: NewCommands(
6970
&Command{
7071
Namespace: "test",
@@ -84,7 +85,7 @@ func TestInterruptError(t *testing.T) {
8485
TestCheckStderrGolden(),
8586
),
8687
}))
87-
t.Run("emtpy-success", Test(&TestConfig{
88+
t.Run("empty-success", Test(&TestConfig{
8889
Commands: NewCommands(
8990
&Command{
9091
Namespace: "test",
@@ -107,7 +108,7 @@ func TestInterruptError(t *testing.T) {
107108
Cmd: "scw test empty success",
108109
Check: TestCheckStdoutGolden(),
109110
}))
110-
t.Run("emtpy-success-json", Test(&TestConfig{
111+
t.Run("empty-success-json", Test(&TestConfig{
111112
Commands: NewCommands(
112113
&Command{
113114
Namespace: "test",
@@ -131,4 +132,22 @@ func TestInterruptError(t *testing.T) {
131132
Cmd: "scw -o json test empty success",
132133
Check: TestCheckStdoutGolden(),
133134
}))
135+
t.Run("empty-list-json", Test(&TestConfig{
136+
Commands: NewCommands(
137+
&Command{
138+
Namespace: "test",
139+
Resource: "empty",
140+
Verb: "success",
141+
ArgsType: reflect.TypeOf(args.RawArgs{}),
142+
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
143+
return []int(nil), nil
144+
},
145+
AllowAnonymousClient: true,
146+
},
147+
),
148+
Cmd: "scw -o json test empty success",
149+
Check: func(t *testing.T, ctx *CheckFuncCtx) {
150+
assert.Equal(t, "[]\n", string(ctx.Stdout))
151+
},
152+
}))
134153
}

internal/core/build_info.go

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
latestVersionFileURL = "https://scw-devtools.s3.nl-ams.scw.cloud/scw-cli-v2-version"
4040
latestVersionUpdateFileLocalName = "latest-cli-version"
4141
latestVersionRequestTimeout = 1 * time.Second
42+
userAgentPrefix = "scaleway-cli"
4243
)
4344

4445
// IsRelease returns true when the version of the CLI is an official release:
@@ -48,6 +49,13 @@ func (b *BuildInfo) IsRelease() bool {
4849
return b.Version != nil && b.Version.Metadata() == ""
4950
}
5051

52+
func (b *BuildInfo) GetUserAgent() string {
53+
if b.Version != nil {
54+
return userAgentPrefix + "/" + b.Version.String()
55+
}
56+
return userAgentPrefix
57+
}
58+
5159
func (b *BuildInfo) checkVersion() {
5260
if !b.IsRelease() || os.Getenv(scwDisableCheckVersionEnv) == "true" {
5361
logger.Debugf("skipping check version")

internal/core/client.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ func createClient(buildInfo *BuildInfo, profileName string) (*scw.Client, error)
5656
}
5757
}
5858

59-
userAgent := "scaleway-cli/"
60-
userAgent += buildInfo.Version.String()
6159
opts := []scw.ClientOption{
6260
scw.WithDefaultRegion(scw.RegionFrPar),
6361
scw.WithDefaultZone(scw.ZoneFrPar1),
64-
scw.WithUserAgent(userAgent),
62+
scw.WithUserAgent(buildInfo.GetUserAgent()),
6563
scw.WithProfile(profile),
6664
}
6765

@@ -73,6 +71,21 @@ func createClient(buildInfo *BuildInfo, profileName string) (*scw.Client, error)
7371
return client, nil
7472
}
7573

74+
func createAnonymousClient(buildInfo *BuildInfo) (*scw.Client, error) {
75+
opts := []scw.ClientOption{
76+
scw.WithDefaultRegion(scw.RegionFrPar),
77+
scw.WithDefaultZone(scw.ZoneFrPar1),
78+
scw.WithUserAgent(buildInfo.GetUserAgent()),
79+
}
80+
81+
client, err := scw.NewClient(opts...)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
return client, nil
87+
}
88+
7689
// configErrorDetails generate a detailed error message for an invalid client option.
7790
func configErrorDetails(configKey, varEnv string) string {
7891
// TODO: update the more info link

internal/core/cobra_utils.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string)
1919
meta := extractMeta(ctx)
2020
meta.command = cmd
2121

22-
// If command require a client we make sure a client is present with proper configuration
23-
if !cmd.AllowAnonymousClient {
24-
err := validateClient(meta.Client)
22+
// If command requires authentication and the client was not directly provided in the bootstrap config, we create a new client and overwrite the existing one
23+
if !cmd.AllowAnonymousClient && !meta.isClientFromBootstrapConfig {
24+
client, err := createClient(meta.BuildInfo, ExtractProfileName(ctx))
25+
meta.Client = client
26+
err = validateClient(meta.Client)
2527
if err != nil {
2628
return err
2729
}

internal/core/context.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ type meta struct {
2222
OverrideEnv map[string]string
2323
OverrideExec OverrideExecFunc
2424

25-
command *Command
26-
stdout io.Writer
27-
stderr io.Writer
28-
result interface{}
25+
command *Command
26+
stdout io.Writer
27+
stderr io.Writer
28+
result interface{}
29+
isClientFromBootstrapConfig bool
2930
}
3031

3132
type contextKey int

internal/core/testing.go

+1
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ func getHTTPRecoder(t *testing.T, update bool) (client *http.Client, cleanup fun
537537
r.AddFilter(func(i *cassette.Interaction) error {
538538
delete(i.Request.Headers, "x-auth-token")
539539
delete(i.Request.Headers, "X-Auth-Token")
540+
i.Request.URL = regexp.MustCompile("organization_id=[0-9a-f-]{36}").ReplaceAllString(i.Request.URL, "organization_id=11111111-1111-1111-1111-111111111111")
540541
return nil
541542
})
542543

0 commit comments

Comments
 (0)