Skip to content

Commit f92de47

Browse files
authored
feat(object): add config commands for s3 tools (#874)
1 parent 37e9f5c commit f92de47

20 files changed

+747
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Generate a S3 tool configuration file.
2+
3+
USAGE:
4+
scw object config get [arg=value ...]
5+
6+
EXAMPLES:
7+
Generate a s3cmd config file for Paris region
8+
scw object config get region=fr-par type=s3cmd
9+
10+
Generate a rclone config file for default region
11+
scw object config get type=rclone
12+
13+
Generate a mc (minio) config file for default region
14+
scw object config get type=mc
15+
16+
ARGS:
17+
type Type of S3 tool you want to generate a config for (rclone | s3cmd | mc)
18+
[name=scaleway] Name of the s3 remote you want to generate
19+
[region] Region to target. If none is passed will use default region from the config (fr-par | nl-ams)
20+
21+
FLAGS:
22+
-h, --help help for get
23+
24+
GLOBAL FLAGS:
25+
-D, --debug Enable debug mode
26+
-o, --output string Output format: json or human
27+
-p, --profile string The config profile to use
28+
29+
SEE ALSO:
30+
# Install a S3 tool configuration file
31+
scw object config install
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Install a S3 tool configuration file to its default location.
2+
3+
USAGE:
4+
scw object config install [arg=value ...]
5+
6+
EXAMPLES:
7+
Install a s3cmd config file for Paris region
8+
scw object config install region=fr-par type=s3cmd
9+
10+
Install a rclone config file for default region
11+
scw object config install type=rclone
12+
13+
Install a mc (minio) config file for default region
14+
scw object config install type=mc
15+
16+
ARGS:
17+
type Type of S3 tool you want to generate a config for (rclone | s3cmd | mc)
18+
[name=scaleway] Name of the s3 remote you want to generate
19+
[region] Region to target. If none is passed will use default region from the config (fr-par | nl-ams)
20+
21+
FLAGS:
22+
-h, --help help for install
23+
24+
GLOBAL FLAGS:
25+
-D, --debug Enable debug mode
26+
-o, --output string Output format: json or human
27+
-p, --profile string The config profile to use
28+
29+
SEE ALSO:
30+
# Generate a S3 tool configuration file
31+
scw object config get
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Configuration generation for S3 tools.
2+
3+
USAGE:
4+
scw object config <command>
5+
6+
AVAILABLE COMMANDS:
7+
get Generate a S3 tool configuration file
8+
install Install a S3 tool configuration file to its default location
9+
10+
FLAGS:
11+
-h, --help help for config
12+
13+
GLOBAL FLAGS:
14+
-D, --debug Enable debug mode
15+
-o, --output string Output format: json or human
16+
-p, --profile string The config profile to use
17+
18+
Use "scw object config [command] --help" for more information about a command.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Object-storage utils
2+
3+
USAGE:
4+
scw object <command>
5+
6+
AVAILABLE COMMANDS:
7+
config Manage configuration files for popular S3 tools
8+
9+
FLAGS:
10+
-h, --help help for object
11+
12+
GLOBAL FLAGS:
13+
-D, --debug Enable debug mode
14+
-o, --output string Output format: json or human
15+
-p, --profile string The config profile to use
16+
17+
Use "scw object [command] --help" for more information about a command.

cmd/scw/testdata/test-main-usage-usage.stderr.golden

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ AVAILABLE COMMANDS:
1010
config Config file management
1111
account This API allows to manage your scaleway account
1212
autocomplete Install autocomplete script
13+
object Object-storage utils
1314
version Display cli version
1415
registry Docker registry API
1516
help Help about any command

go.sum

-13
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
5858
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5959
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6060
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
61-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6 h1:C1/pvkxkGN/H03mDxLzItaceYJDBk1HdClgR15suAzI=
62-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200331160105-1181c3dc1bcd h1:ICQFQOSIOyt5n1RxOCAg1rq+DFLunpJpAK2ttjdGhUU=
63-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200331160105-1181c3dc1bcd/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
64-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403105108-eb943ac1f1dc h1:YJloAPPmGOEF+nufGL4a9Ppj6jnIMs8FjIorEM3ZBoE=
65-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403105108-eb943ac1f1dc/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
66-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403173805-25a10631420d h1:oAG5xtIkUri9hwveoJPv0K3JTSREckKG3MfmNmnXAEQ=
67-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403173805-25a10631420d/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
68-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200409105057-297e3dbdccb5 h1:QmrgbtSAIDKRTnzAjdpop3yFAqHFO4MEc8W+/0Epu88=
69-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200409105057-297e3dbdccb5/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
70-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200414183251-a6244a393e63 h1:8tRzNwnY+PK6TD21bvKU0p334p+kApiKdHRO3xsZgpg=
71-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200414183251-a6244a393e63/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
72-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200421151545-c4338a1f64c9 h1:VExNGApruijWPXvqW/ns6wlNwxAFm/8F7KnknJn5pwc=
73-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200421151545-c4338a1f64c9/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
7461
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200422170208-cd3e8b9e038c h1:/aJXisEbIVbReS1ofNktWS8s1iT1TPP8eTu7b7Qvmxs=
7562
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200422170208-cd3e8b9e038c/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
7663
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=

internal/core/testing.go

+66-30
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ type CheckFuncCtx struct {
5454

5555
// Scaleway client
5656
Client *scw.Client
57+
58+
// OverrideEnv passed in the TestConfig
59+
OverrideEnv map[string]string
5760
}
5861

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

6669
type BeforeFuncCtx struct {
67-
T *testing.T
68-
Client *scw.Client
69-
ExecuteCmd func(args []string) interface{}
70-
Meta map[string]interface{}
70+
T *testing.T
71+
Client *scw.Client
72+
ExecuteCmd func(args []string) interface{}
73+
Meta map[string]interface{}
74+
OverrideEnv map[string]string
7175
}
7276

7377
type AfterFuncCtx struct {
74-
T *testing.T
75-
Client *scw.Client
76-
ExecuteCmd func(args []string) interface{}
77-
Meta map[string]interface{}
78-
CmdResult interface{}
78+
T *testing.T
79+
Client *scw.Client
80+
ExecuteCmd func(args []string) interface{}
81+
Meta map[string]interface{}
82+
CmdResult interface{}
83+
OverrideEnv map[string]string
7984
}
8085

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

124+
// If set, it will create a temporary home directory during the tests.
125+
// Get this folder with ExtractUserHomeDir()
126+
TmpHomeDir bool
127+
119128
// OverrideEnv contains environment variables that will be overridden during the test.
120129
OverrideEnv map[string]string
130+
131+
// Custom client to use for test, if none are provided will create one automatically
132+
Client *scw.Client
121133
}
122134

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

138-
func getTestClient(t *testing.T, testConfig *TestConfig) (client *scw.Client, cleanup func()) {
150+
func createTestClient(t *testing.T, testConfig *TestConfig) (client *scw.Client, cleanup func()) {
139151
var err error
140152
cleanup = func() {}
141153

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

202-
client, cleanup := getTestClient(t, config)
203-
defer cleanup()
214+
// We try to use the client provided in the config
215+
// if no client is provided in the config we create a test client
216+
client := config.Client
217+
if client == nil {
218+
var cleanup func()
219+
client, cleanup = createTestClient(t, config)
220+
defer cleanup()
221+
}
204222

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

225+
overideEnv := config.OverrideEnv
226+
if overideEnv == nil {
227+
overideEnv = map[string]string{}
228+
}
229+
230+
if config.TmpHomeDir {
231+
dir, err := ioutil.TempDir(os.TempDir(), "scw")
232+
require.NoError(t, err)
233+
defer func() {
234+
err = os.RemoveAll(dir)
235+
assert.NoError(t, err)
236+
}()
237+
overideEnv["HOME"] = dir
238+
}
239+
207240
executeCmd := func(args []string) interface{} {
208241
stdoutBuffer := &bytes.Buffer{}
209242
stderrBuffer := &bytes.Buffer{}
@@ -216,7 +249,7 @@ func Test(config *TestConfig) func(t *testing.T) {
216249
Stderr: stderrBuffer,
217250
Client: client,
218251
DisableTelemetry: true,
219-
OverrideEnv: config.OverrideEnv,
252+
OverrideEnv: overideEnv,
220253
})
221254
require.NoError(t, err, "stdout: %s\nstderr: %s", stdoutBuffer.String(), stderrBuffer.String())
222255

@@ -226,10 +259,11 @@ func Test(config *TestConfig) func(t *testing.T) {
226259
// Run config.BeforeFunc
227260
if config.BeforeFunc != nil {
228261
require.NoError(t, config.BeforeFunc(&BeforeFuncCtx{
229-
T: t,
230-
Client: client,
231-
ExecuteCmd: executeCmd,
232-
Meta: meta,
262+
T: t,
263+
Client: client,
264+
ExecuteCmd: executeCmd,
265+
Meta: meta,
266+
OverrideEnv: overideEnv,
233267
}))
234268
}
235269

@@ -253,29 +287,31 @@ func Test(config *TestConfig) func(t *testing.T) {
253287
Stderr: stderr,
254288
Client: client,
255289
DisableTelemetry: true,
256-
OverrideEnv: config.OverrideEnv,
290+
OverrideEnv: overideEnv,
257291
})
258292

259293
meta["CmdResult"] = result
260294
config.Check(t, &CheckFuncCtx{
261-
ExitCode: exitCode,
262-
Stdout: stdout.Bytes(),
263-
Stderr: stderr.Bytes(),
264-
Meta: meta,
265-
Result: result,
266-
Err: err,
267-
Client: client,
295+
ExitCode: exitCode,
296+
Stdout: stdout.Bytes(),
297+
Stderr: stderr.Bytes(),
298+
Meta: meta,
299+
Result: result,
300+
Err: err,
301+
Client: client,
302+
OverrideEnv: overideEnv,
268303
})
269304
}
270305

271306
// Run config.AfterFunc
272307
if config.AfterFunc != nil {
273308
require.NoError(t, config.AfterFunc(&AfterFuncCtx{
274-
T: t,
275-
Client: client,
276-
ExecuteCmd: executeCmd,
277-
Meta: meta,
278-
CmdResult: result,
309+
T: t,
310+
Client: client,
311+
ExecuteCmd: executeCmd,
312+
Meta: meta,
313+
CmdResult: result,
314+
OverrideEnv: overideEnv,
279315
}))
280316
}
281317
}

internal/namespaces/get_commands.go

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/scaleway/scaleway-cli/internal/namespaces/instance/v1"
1111
k8s "github.com/scaleway/scaleway-cli/internal/namespaces/k8s/v1"
1212
"github.com/scaleway/scaleway-cli/internal/namespaces/marketplace/v1"
13+
"github.com/scaleway/scaleway-cli/internal/namespaces/object/v1"
1314
"github.com/scaleway/scaleway-cli/internal/namespaces/registry/v1"
1415
versionNamespace "github.com/scaleway/scaleway-cli/internal/namespaces/version"
1516
)
@@ -29,6 +30,7 @@ func GetCommands() *core.Commands {
2930
commands.Merge(configNamespace.GetCommands())
3031
commands.Merge(account.GetCommands())
3132
commands.Merge(autocompleteNamespace.GetCommands())
33+
commands.Merge(object.GetCommands())
3234
commands.Merge(versionNamespace.GetCommands())
3335
commands.Merge(registry.GetCommands())
3436
return commands
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package object
2+
3+
import (
4+
"github.com/scaleway/scaleway-cli/internal/core"
5+
)
6+
7+
func GetCommands() *core.Commands {
8+
return core.NewCommands(
9+
objectRoot(),
10+
objectConfig(),
11+
configGetCommand(),
12+
configInstallCommand(),
13+
)
14+
}
15+
16+
func objectRoot() *core.Command {
17+
return &core.Command{
18+
Short: `Object-storage utils`,
19+
Namespace: "object",
20+
}
21+
}
22+
23+
func objectConfig() *core.Command {
24+
return &core.Command{
25+
Short: `Manage configuration files for popular S3 tools`,
26+
Long: `Configuration generation for S3 tools.`,
27+
Namespace: "object",
28+
Resource: `config`,
29+
}
30+
}

0 commit comments

Comments
 (0)