Skip to content

Commit

Permalink
feat(object): add config commands for s3 tools (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone authored Apr 23, 2020
1 parent 37e9f5c commit f92de47
Show file tree
Hide file tree
Showing 20 changed files with 747 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Generate a S3 tool configuration file.

USAGE:
scw object config get [arg=value ...]

EXAMPLES:
Generate a s3cmd config file for Paris region
scw object config get region=fr-par type=s3cmd

Generate a rclone config file for default region
scw object config get type=rclone

Generate a mc (minio) config file for default region
scw object config get type=mc

ARGS:
type Type of S3 tool you want to generate a config for (rclone | s3cmd | mc)
[name=scaleway] Name of the s3 remote you want to generate
[region] Region to target. If none is passed will use default region from the config (fr-par | nl-ams)

FLAGS:
-h, --help help for get

GLOBAL FLAGS:
-D, --debug Enable debug mode
-o, --output string Output format: json or human
-p, --profile string The config profile to use

SEE ALSO:
# Install a S3 tool configuration file
scw object config install
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Install a S3 tool configuration file to its default location.

USAGE:
scw object config install [arg=value ...]

EXAMPLES:
Install a s3cmd config file for Paris region
scw object config install region=fr-par type=s3cmd

Install a rclone config file for default region
scw object config install type=rclone

Install a mc (minio) config file for default region
scw object config install type=mc

ARGS:
type Type of S3 tool you want to generate a config for (rclone | s3cmd | mc)
[name=scaleway] Name of the s3 remote you want to generate
[region] Region to target. If none is passed will use default region from the config (fr-par | nl-ams)

FLAGS:
-h, --help help for install

GLOBAL FLAGS:
-D, --debug Enable debug mode
-o, --output string Output format: json or human
-p, --profile string The config profile to use

SEE ALSO:
# Generate a S3 tool configuration file
scw object config get
18 changes: 18 additions & 0 deletions cmd/scw/testdata/test-all-usage-object-config-usage.stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Configuration generation for S3 tools.

USAGE:
scw object config <command>

AVAILABLE COMMANDS:
get Generate a S3 tool configuration file
install Install a S3 tool configuration file to its default location

FLAGS:
-h, --help help for config

GLOBAL FLAGS:
-D, --debug Enable debug mode
-o, --output string Output format: json or human
-p, --profile string The config profile to use

Use "scw object config [command] --help" for more information about a command.
17 changes: 17 additions & 0 deletions cmd/scw/testdata/test-all-usage-object-usage.stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Object-storage utils

USAGE:
scw object <command>

AVAILABLE COMMANDS:
config Manage configuration files for popular S3 tools

FLAGS:
-h, --help help for object

GLOBAL FLAGS:
-D, --debug Enable debug mode
-o, --output string Output format: json or human
-p, --profile string The config profile to use

Use "scw object [command] --help" for more information about a command.
1 change: 1 addition & 0 deletions cmd/scw/testdata/test-main-usage-usage.stderr.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ AVAILABLE COMMANDS:
config Config file management
account This API allows to manage your scaleway account
autocomplete Install autocomplete script
object Object-storage utils
version Display cli version
registry Docker registry API
help Help about any command
Expand Down
13 changes: 0 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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 h1:C1/pvkxkGN/H03mDxLzItaceYJDBk1HdClgR15suAzI=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200331160105-1181c3dc1bcd h1:ICQFQOSIOyt5n1RxOCAg1rq+DFLunpJpAK2ttjdGhUU=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200331160105-1181c3dc1bcd/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403105108-eb943ac1f1dc h1:YJloAPPmGOEF+nufGL4a9Ppj6jnIMs8FjIorEM3ZBoE=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403105108-eb943ac1f1dc/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403173805-25a10631420d h1:oAG5xtIkUri9hwveoJPv0K3JTSREckKG3MfmNmnXAEQ=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403173805-25a10631420d/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200409105057-297e3dbdccb5 h1:QmrgbtSAIDKRTnzAjdpop3yFAqHFO4MEc8W+/0Epu88=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200409105057-297e3dbdccb5/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200414183251-a6244a393e63 h1:8tRzNwnY+PK6TD21bvKU0p334p+kApiKdHRO3xsZgpg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200414183251-a6244a393e63/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200421151545-c4338a1f64c9 h1:VExNGApruijWPXvqW/ns6wlNwxAFm/8F7KnknJn5pwc=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200421151545-c4338a1f64c9/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200422170208-cd3e8b9e038c h1:/aJXisEbIVbReS1ofNktWS8s1iT1TPP8eTu7b7Qvmxs=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200422170208-cd3e8b9e038c/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
Expand Down
96 changes: 66 additions & 30 deletions internal/core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type CheckFuncCtx struct {

// Scaleway client
Client *scw.Client

// OverrideEnv passed in the TestConfig
OverrideEnv map[string]string
}

// TestCheck is a function that perform assertion on a CheckFuncCtx
Expand All @@ -64,18 +67,20 @@ type BeforeFunc func(ctx *BeforeFuncCtx) error
type AfterFunc func(ctx *AfterFuncCtx) error

type BeforeFuncCtx struct {
T *testing.T
Client *scw.Client
ExecuteCmd func(args []string) interface{}
Meta map[string]interface{}
T *testing.T
Client *scw.Client
ExecuteCmd func(args []string) interface{}
Meta map[string]interface{}
OverrideEnv map[string]string
}

type AfterFuncCtx struct {
T *testing.T
Client *scw.Client
ExecuteCmd func(args []string) interface{}
Meta map[string]interface{}
CmdResult interface{}
T *testing.T
Client *scw.Client
ExecuteCmd func(args []string) interface{}
Meta map[string]interface{}
CmdResult interface{}
OverrideEnv map[string]string
}

// TestConfig contain configuration that can be used with the Test function
Expand Down Expand Up @@ -116,8 +121,15 @@ type TestConfig struct {
// Fake build info for this test.
BuildInfo BuildInfo

// If set, it will create a temporary home directory during the tests.
// Get this folder with ExtractUserHomeDir()
TmpHomeDir bool

// OverrideEnv contains environment variables that will be overridden during the test.
OverrideEnv map[string]string

// Custom client to use for test, if none are provided will create one automatically
Client *scw.Client
}

// getTestFilePath returns a valid filename path based on the go test name and suffix. (Take care of non fs friendly char)
Expand All @@ -135,7 +147,7 @@ func getTestFilePath(t *testing.T, suffix string) string {
return filepath.Join(".", "testdata", fileName)
}

func getTestClient(t *testing.T, testConfig *TestConfig) (client *scw.Client, cleanup func()) {
func createTestClient(t *testing.T, testConfig *TestConfig) (client *scw.Client, cleanup func()) {
var err error
cleanup = func() {}

Expand Down Expand Up @@ -199,11 +211,32 @@ func Test(config *TestConfig) func(t *testing.T) {
return "few seconds ago", nil
})

client, cleanup := getTestClient(t, config)
defer cleanup()
// We try to use the client provided in the config
// if no client is provided in the config we create a test client
client := config.Client
if client == nil {
var cleanup func()
client, cleanup = createTestClient(t, config)
defer cleanup()
}

meta := map[string]interface{}{}

overideEnv := config.OverrideEnv
if overideEnv == nil {
overideEnv = map[string]string{}
}

if config.TmpHomeDir {
dir, err := ioutil.TempDir(os.TempDir(), "scw")
require.NoError(t, err)
defer func() {
err = os.RemoveAll(dir)
assert.NoError(t, err)
}()
overideEnv["HOME"] = dir
}

executeCmd := func(args []string) interface{} {
stdoutBuffer := &bytes.Buffer{}
stderrBuffer := &bytes.Buffer{}
Expand All @@ -216,7 +249,7 @@ func Test(config *TestConfig) func(t *testing.T) {
Stderr: stderrBuffer,
Client: client,
DisableTelemetry: true,
OverrideEnv: config.OverrideEnv,
OverrideEnv: overideEnv,
})
require.NoError(t, err, "stdout: %s\nstderr: %s", stdoutBuffer.String(), stderrBuffer.String())

Expand All @@ -226,10 +259,11 @@ func Test(config *TestConfig) func(t *testing.T) {
// Run config.BeforeFunc
if config.BeforeFunc != nil {
require.NoError(t, config.BeforeFunc(&BeforeFuncCtx{
T: t,
Client: client,
ExecuteCmd: executeCmd,
Meta: meta,
T: t,
Client: client,
ExecuteCmd: executeCmd,
Meta: meta,
OverrideEnv: overideEnv,
}))
}

Expand All @@ -253,29 +287,31 @@ func Test(config *TestConfig) func(t *testing.T) {
Stderr: stderr,
Client: client,
DisableTelemetry: true,
OverrideEnv: config.OverrideEnv,
OverrideEnv: overideEnv,
})

meta["CmdResult"] = result
config.Check(t, &CheckFuncCtx{
ExitCode: exitCode,
Stdout: stdout.Bytes(),
Stderr: stderr.Bytes(),
Meta: meta,
Result: result,
Err: err,
Client: client,
ExitCode: exitCode,
Stdout: stdout.Bytes(),
Stderr: stderr.Bytes(),
Meta: meta,
Result: result,
Err: err,
Client: client,
OverrideEnv: overideEnv,
})
}

// Run config.AfterFunc
if config.AfterFunc != nil {
require.NoError(t, config.AfterFunc(&AfterFuncCtx{
T: t,
Client: client,
ExecuteCmd: executeCmd,
Meta: meta,
CmdResult: result,
T: t,
Client: client,
ExecuteCmd: executeCmd,
Meta: meta,
CmdResult: result,
OverrideEnv: overideEnv,
}))
}
}
Expand Down
2 changes: 2 additions & 0 deletions internal/namespaces/get_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/scaleway/scaleway-cli/internal/namespaces/instance/v1"
k8s "github.com/scaleway/scaleway-cli/internal/namespaces/k8s/v1"
"github.com/scaleway/scaleway-cli/internal/namespaces/marketplace/v1"
"github.com/scaleway/scaleway-cli/internal/namespaces/object/v1"
"github.com/scaleway/scaleway-cli/internal/namespaces/registry/v1"
versionNamespace "github.com/scaleway/scaleway-cli/internal/namespaces/version"
)
Expand All @@ -29,6 +30,7 @@ func GetCommands() *core.Commands {
commands.Merge(configNamespace.GetCommands())
commands.Merge(account.GetCommands())
commands.Merge(autocompleteNamespace.GetCommands())
commands.Merge(object.GetCommands())
commands.Merge(versionNamespace.GetCommands())
commands.Merge(registry.GetCommands())
return commands
Expand Down
30 changes: 30 additions & 0 deletions internal/namespaces/object/v1/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package object

import (
"github.com/scaleway/scaleway-cli/internal/core"
)

func GetCommands() *core.Commands {
return core.NewCommands(
objectRoot(),
objectConfig(),
configGetCommand(),
configInstallCommand(),
)
}

func objectRoot() *core.Command {
return &core.Command{
Short: `Object-storage utils`,
Namespace: "object",
}
}

func objectConfig() *core.Command {
return &core.Command{
Short: `Manage configuration files for popular S3 tools`,
Long: `Configuration generation for S3 tools.`,
Namespace: "object",
Resource: `config`,
}
}
Loading

0 comments on commit f92de47

Please sign in to comment.