Skip to content

Commit

Permalink
Merge branch 'v2' into config_get
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone authored Jun 1, 2020
2 parents 039a797 + 5b9cc8a commit 96db741
Show file tree
Hide file tree
Showing 29 changed files with 3,560 additions and 471 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ ARGS:
server-id Server ID to install
os-id ID of the OS to install on the server
hostname Hostname of the server
ssh-key-ids.{index} SSH key IDs authorized on the server
[all-ssh-keys] Add all SSH keys on your baremetal instance (cannot be used with ssh-key-ids)
ssh-key-ids.{index} SSH key IDs authorized on the server (cannot be used with all-ssh-keys)
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config (fr-par-2)

FLAGS:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Marketplace images can be used when creating an instance.

USAGE:
scw marketplace image <command>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ USAGE:
scw marketplace <command>

AVAILABLE COMMANDS:
image
image Marketplace images can be used when creating an instance

FLAGS:
-h, --help help for marketplace
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
github.com/fatih/color v1.9.0
github.com/getsentry/raven-go v0.2.0
github.com/gorilla/websocket v1.4.2
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/go-version v1.2.0
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.4
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JY
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
Expand Down Expand Up @@ -65,6 +61,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200511151306-5e35a6b50424 h1:daVeC8IxL9EA3ASo38uwQm2nGcprn0L0XMb8Sj5DL4Y=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200511151306-5e35a6b50424/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
Expand Down Expand Up @@ -95,6 +92,7 @@ golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwg
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
Expand Down
16 changes: 10 additions & 6 deletions internal/core/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
}
interactive.SetOutputWriter(config.Stderr) // set printer for interactive function (always stderr).

// An authenticated client will be created later if required.
client := config.Client
isClientFromBootstrapConfig := true
if client == nil {
// Create scw client
client, err = createClient(config.BuildInfo, profileName)
isClientFromBootstrapConfig = false
client, err = createAnonymousClient(config.BuildInfo)
if err != nil {
printErr := globalPrinter.Print(err, nil)
if printErr != nil {
Expand All @@ -114,15 +116,17 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
ProfileFlag: profileName,
BinaryName: config.Args[0],
BuildInfo: config.BuildInfo,
stdout: config.Stdout,
stderr: config.Stderr,
Client: client,
Commands: config.Commands,
Printer: globalPrinter,
OverrideEnv: config.OverrideEnv,
OverrideExec: config.OverrideExec,
result: nil, // result is later injected by cobra_utils.go/cobraRun()
command: nil, // command is later injected by cobra_utils.go/cobraRun()

stdout: config.Stdout,
stderr: config.Stderr,
result: nil, // result is later injected by cobra_utils.go/cobraRun()
command: nil, // command is later injected by cobra_utils.go/cobraRun()
isClientFromBootstrapConfig: isClientFromBootstrapConfig,
}
// We make sure OverrideEnv is never nil in meta.
if meta.OverrideEnv == nil {
Expand Down
27 changes: 23 additions & 4 deletions internal/core/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"testing"

"github.com/alecthomas/assert"
"github.com/scaleway/scaleway-cli/internal/args"
"github.com/scaleway/scaleway-cli/internal/interactive"
)
Expand Down Expand Up @@ -44,7 +45,7 @@ func TestInterruptError(t *testing.T) {
Cmd: "scw test code error",
Check: TestCheckExitCode(99),
}))
t.Run("emtpy-error", Test(&TestConfig{
t.Run("empty-error", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Expand All @@ -64,7 +65,7 @@ func TestInterruptError(t *testing.T) {
TestCheckStderrGolden(),
),
}))
t.Run("emtpy-error-json", Test(&TestConfig{
t.Run("empty-error-json", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Expand All @@ -84,7 +85,7 @@ func TestInterruptError(t *testing.T) {
TestCheckStderrGolden(),
),
}))
t.Run("emtpy-success", Test(&TestConfig{
t.Run("empty-success", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Expand All @@ -107,7 +108,7 @@ func TestInterruptError(t *testing.T) {
Cmd: "scw test empty success",
Check: TestCheckStdoutGolden(),
}))
t.Run("emtpy-success-json", Test(&TestConfig{
t.Run("empty-success-json", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Expand All @@ -131,4 +132,22 @@ func TestInterruptError(t *testing.T) {
Cmd: "scw -o json test empty success",
Check: TestCheckStdoutGolden(),
}))
t.Run("empty-list-json", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Resource: "empty",
Verb: "success",
ArgsType: reflect.TypeOf(args.RawArgs{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
return []int(nil), nil
},
AllowAnonymousClient: true,
},
),
Cmd: "scw -o json test empty success",
Check: func(t *testing.T, ctx *CheckFuncCtx) {
assert.Equal(t, "[]\n", string(ctx.Stdout))
},
}))
}
8 changes: 8 additions & 0 deletions internal/core/build_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
latestVersionFileURL = "https://scw-devtools.s3.nl-ams.scw.cloud/scw-cli-v2-version"
latestVersionUpdateFileLocalName = "latest-cli-version"
latestVersionRequestTimeout = 1 * time.Second
userAgentPrefix = "scaleway-cli"
)

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

func (b *BuildInfo) GetUserAgent() string {
if b.Version != nil {
return userAgentPrefix + "/" + b.Version.String()
}
return userAgentPrefix
}

func (b *BuildInfo) checkVersion() {
if !b.IsRelease() || os.Getenv(scwDisableCheckVersionEnv) == "true" {
logger.Debugf("skipping check version")
Expand Down
19 changes: 16 additions & 3 deletions internal/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ func createClient(buildInfo *BuildInfo, profileName string) (*scw.Client, error)
}
}

userAgent := "scaleway-cli/"
userAgent += buildInfo.Version.String()
opts := []scw.ClientOption{
scw.WithDefaultRegion(scw.RegionFrPar),
scw.WithDefaultZone(scw.ZoneFrPar1),
scw.WithUserAgent(userAgent),
scw.WithUserAgent(buildInfo.GetUserAgent()),
scw.WithProfile(profile),
}

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

func createAnonymousClient(buildInfo *BuildInfo) (*scw.Client, error) {
opts := []scw.ClientOption{
scw.WithDefaultRegion(scw.RegionFrPar),
scw.WithDefaultZone(scw.ZoneFrPar1),
scw.WithUserAgent(buildInfo.GetUserAgent()),
}

client, err := scw.NewClient(opts...)
if err != nil {
return nil, err
}

return client, nil
}

// configErrorDetails generate a detailed error message for an invalid client option.
func configErrorDetails(configKey, varEnv string) string {
// TODO: update the more info link
Expand Down
8 changes: 5 additions & 3 deletions internal/core/cobra_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string)
meta := extractMeta(ctx)
meta.command = cmd

// If command require a client we make sure a client is present with proper configuration
if !cmd.AllowAnonymousClient {
err := validateClient(meta.Client)
// 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
if !cmd.AllowAnonymousClient && !meta.isClientFromBootstrapConfig {
client, err := createClient(meta.BuildInfo, ExtractProfileName(ctx))
meta.Client = client
err = validateClient(meta.Client)
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions internal/core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ type meta struct {
OverrideEnv map[string]string
OverrideExec OverrideExecFunc

command *Command
stdout io.Writer
stderr io.Writer
result interface{}
command *Command
stdout io.Writer
stderr io.Writer
result interface{}
isClientFromBootstrapConfig bool
}

type contextKey int
Expand Down
1 change: 1 addition & 0 deletions internal/core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ func getHTTPRecoder(t *testing.T, update bool) (client *http.Client, cleanup fun
r.AddFilter(func(i *cassette.Interaction) error {
delete(i.Request.Headers, "x-auth-token")
delete(i.Request.Headers, "X-Auth-Token")
i.Request.URL = regexp.MustCompile("organization_id=[0-9a-f-]{36}").ReplaceAllString(i.Request.URL, "organization_id=11111111-1111-1111-1111-111111111111")
return nil
})

Expand Down
Loading

0 comments on commit 96db741

Please sign in to comment.