Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(object): add config commands for s3 tools #874

Merged
merged 37 commits into from
Apr 23, 2020
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
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 {
remyleone marked this conversation as resolved.
Show resolved Hide resolved
return &core.Command{
Short: `Manage configuration files for popular S3 tools`,
Long: `Configuration generation for S3 tools.`,
Namespace: "object",
Resource: `config`,
}
}
Loading