Skip to content

Commit 68fae1d

Browse files
yfodilremyleone
andauthored
feat(config): add destroy command (#2487)
Co-authored-by: Rémy Léone <rleone@scaleway.com>
1 parent 3f0dcac commit 68fae1d

6 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
3+
Destroy the config file
4+
5+
USAGE:
6+
scw config destroy
7+
8+
FLAGS:
9+
-h, --help help for destroy
10+
11+
GLOBAL FLAGS:
12+
-c, --config string The path to the config file
13+
-D, --debug Enable debug mode
14+
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
15+
-p, --profile string The config profile to use

docs/commands/config.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The following environment variables are supported:
2626

2727
Read more about the config management engine at https://github.com/scaleway/scaleway-sdk-go/tree/master/scw#scaleway-config
2828

29+
- [Destroy the config file](#destroy-the-config-file)
2930
- [Dump the config file](#dump-the-config-file)
3031
- [Get a value from the config file](#get-a-value-from-the-config-file)
3132
- [Allows the deletion of a profile from the config file](#allows-the-deletion-of-a-profile-from-the-config-file)
@@ -36,6 +37,20 @@ Read more about the config management engine at https://github.com/scaleway/scal
3637
- [Unset a line from the config file](#unset-a-line-from-the-config-file)
3738

3839

40+
## Destroy the config file
41+
42+
43+
44+
45+
46+
**Usage:**
47+
48+
```
49+
scw config destroy
50+
```
51+
52+
53+
3954
## Dump the config file
4055

4156

internal/namespaces/config/commands.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"os"
78
"reflect"
89

910
"github.com/scaleway/scaleway-sdk-go/validation"
@@ -28,6 +29,7 @@ func GetCommands() *core.Commands {
2829
configDeleteProfileCommand(),
2930
configActivateProfileCommand(),
3031
configResetCommand(),
32+
configDestroyCommand(),
3133
)
3234
}
3335

@@ -510,6 +512,29 @@ func configResetCommand() *core.Command {
510512
}
511513
}
512514

515+
// configDestroyCommand destroys the config
516+
func configDestroyCommand() *core.Command {
517+
type configDestroyArgs struct{}
518+
519+
return &core.Command{
520+
Short: `Destroy the config file`,
521+
Namespace: "config",
522+
Resource: "destroy",
523+
AllowAnonymousClient: true,
524+
ArgsType: reflect.TypeOf(configDestroyArgs{}),
525+
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
526+
configPath := core.ExtractConfigPath(ctx)
527+
err := os.Remove(configPath)
528+
if err != nil {
529+
return err, nil
530+
}
531+
return &core.SuccessResult{
532+
Message: "successfully destroy config",
533+
}, nil
534+
},
535+
}
536+
}
537+
513538
// Helper functions
514539
func getProfileValue(profile *scw.Profile, fieldName string) (interface{}, error) {
515540
field, err := getProfileField(profile, fieldName)

internal/namespaces/config/commands_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,51 @@ func Test_ConfigDumpCommand(t *testing.T) {
210210
}))
211211
}
212212

213+
func Test_ConfigDestroyCommand(t *testing.T) {
214+
path := "/tmp/test_config_destroy/"
215+
216+
t.Run("Simple", core.Test(&core.TestConfig{
217+
Commands: GetCommands(),
218+
BeforeFunc: beforeFuncCreateFullConfig(),
219+
Cmd: "scw config destroy",
220+
Check: core.TestCheckCombine(
221+
core.TestCheckExitCode(0),
222+
core.TestCheckGolden(),
223+
),
224+
TmpHomeDir: true,
225+
}))
226+
227+
t.Run("Check Config File", core.Test(&core.TestConfig{
228+
Commands: GetCommands(),
229+
BeforeFunc: core.BeforeFuncCombine(
230+
func(ctx *core.BeforeFuncCtx) error {
231+
err := os.MkdirAll(path, os.ModePerm)
232+
if err != nil {
233+
t.Fatalf("MkdirAll %q: %s", path, err)
234+
}
235+
return nil
236+
},
237+
beforeFuncCreateFullConfig(),
238+
core.ExecStoreBeforeCmd(
239+
"Destroy",
240+
"scw config destroy",
241+
),
242+
),
243+
Cmd: "scw config dump",
244+
Check: core.TestCheckCombine(
245+
core.TestCheckExitCode(1),
246+
core.TestCheckGolden(),
247+
),
248+
OverrideEnv: map[string]string{
249+
"HOME": path,
250+
},
251+
AfterFunc: func(ctx *core.AfterFuncCtx) error {
252+
_ = os.RemoveAll(path)
253+
return nil
254+
},
255+
}))
256+
}
257+
213258
func checkConfig(f func(t *testing.T, config *scw.Config)) core.TestCheck {
214259
return func(t *testing.T, ctx *core.CheckFuncCtx) {
215260
homeDir := ctx.OverrideEnv["HOME"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
🎲🎲🎲 EXIT CODE: 1 🎲🎲🎲
2+
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
3+
Scaleway-sdk-go: cannot read config file /tmp/test_config_destroy/.config/scw/config.yaml: no such file or directory
4+
🟥🟥🟥 JSON STDERR 🟥🟥🟥
5+
{
6+
"error": "scaleway-sdk-go: cannot read config file /tmp/test_config_destroy/.config/scw/config.yaml: no such file or directory"
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
✅ Successfully destroy config.
4+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
5+
{
6+
"message": "successfully destroy config",
7+
"details": ""
8+
}

0 commit comments

Comments
 (0)