Skip to content

Commit e0f433b

Browse files
authored
feat(config): add support for edit config (#4916)
1 parent 3023c2e commit e0f433b

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
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+
Edit the configuration file with the default editor
4+
5+
USAGE:
6+
scw config edit
7+
8+
FLAGS:
9+
-h, --help help for edit
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
@@ -28,6 +28,7 @@ Read more about the config management engine at https://github.com/scaleway/scal
2828

2929
- [Destroy the config file](#destroy-the-config-file)
3030
- [Dump the config file](#dump-the-config-file)
31+
- [Edit the configuration file](#edit-the-configuration-file)
3132
- [Get a value from the config file](#get-a-value-from-the-config-file)
3233
- [Import configurations from another file](#import-configurations-from-another-file)
3334
- [Get config values from the config file for the current profile](#get-config-values-from-the-config-file-for-the-current-profile)
@@ -68,6 +69,20 @@ scw config dump
6869

6970

7071

72+
## Edit the configuration file
73+
74+
Edit the configuration file with the default editor
75+
76+
Edit the configuration file with the default editor
77+
78+
**Usage:**
79+
80+
```
81+
scw config edit
82+
```
83+
84+
85+
7186
## Get a value from the config file
7287

7388

internal/namespaces/config/commands.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"errors"
77
"fmt"
88
"os"
9+
"os/exec"
910
"reflect"
1011
"strings"
1112

1213
"github.com/fatih/color"
1314
"github.com/scaleway/scaleway-cli/v2/core"
15+
"github.com/scaleway/scaleway-cli/v2/internal/config"
1416
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
1517
"github.com/scaleway/scaleway-cli/v2/internal/tabwriter"
1618
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
@@ -34,6 +36,7 @@ func GetCommands() *core.Commands {
3436
configInfoCommand(),
3537
configImportCommand(),
3638
configValidateCommand(),
39+
configEditCommand(),
3740
)
3841
}
3942

@@ -770,6 +773,38 @@ The command goes through each profile present in the config file and validates i
770773
}
771774
}
772775

776+
func configEditCommand() *core.Command {
777+
type configEditArgs struct{}
778+
779+
return &core.Command{
780+
Namespace: "config",
781+
Resource: "edit",
782+
Short: "Edit the configuration file",
783+
Long: "Edit the configuration file with the default editor",
784+
ArgsType: reflect.TypeOf(configEditArgs{}),
785+
AllowAnonymousClient: true,
786+
Run: func(ctx context.Context, _ any) (i any, e error) {
787+
configPath := core.ExtractConfigPath(ctx)
788+
789+
defaultEditor := config.GetDefaultEditor()
790+
args := []string{configPath}
791+
792+
cmd := exec.Command(defaultEditor, args...)
793+
cmd.Stdin = os.Stdin
794+
cmd.Stdout = os.Stdout
795+
796+
err := cmd.Run()
797+
if err != nil {
798+
return nil, fmt.Errorf("failed to edit file %q: %w", configPath, err)
799+
}
800+
801+
return &core.SuccessResult{
802+
Message: "successfully wrote config",
803+
}, nil
804+
},
805+
}
806+
}
807+
773808
// Helper functions
774809
func getProfileValue(profile *scw.Profile, fieldName string) (any, error) {
775810
field, err := getProfileField(profile, fieldName)

0 commit comments

Comments
 (0)