Skip to content

Commit b4e3cc4

Browse files
authored
feat: add support for scw config profile list (#5200)
1 parent a7d722d commit b4e3cc4

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-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+
List all profiles in the config file
4+
5+
USAGE:
6+
scw config profile list
7+
8+
FLAGS:
9+
-h, --help help for list
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

cmd/scw/testdata/test-all-usage-config-profile-usage.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ USAGE:
88
CONFIGURATION COMMANDS:
99
activate Mark a profile as active in the config file
1010
delete Delete a profile from the config file
11+
list List all profiles in the config file
1112

1213
FLAGS:
1314
-h, --help help for profile

docs/commands/config.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Read more about the config management engine at https://github.com/scaleway/scal
3535
- [Allows the activation and deletion of a profile from the config file](#allows-the-activation-and-deletion-of-a-profile-from-the-config-file)
3636
- [Mark a profile as active in the config file](#mark-a-profile-as-active-in-the-config-file)
3737
- [Delete a profile from the config file](#delete-a-profile-from-the-config-file)
38+
- [List all profiles in the config file](#list-all-profiles-in-the-config-file)
3839
- [Reset the config](#reset-the-config)
3940
- [Set a line from the config file](#set-a-line-from-the-config-file)
4041
- [Unset a line from the config file](#unset-a-line-from-the-config-file)
@@ -212,6 +213,18 @@ scw config profile delete <name ...> [arg=value ...]
212213

213214

214215

216+
### List all profiles in the config file
217+
218+
219+
220+
**Usage:**
221+
222+
```
223+
scw config profile list
224+
```
225+
226+
227+
215228
## Reset the config
216229

217230

internal/namespaces/config/commands.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"os/exec"
1010
"reflect"
11+
"sort"
1112
"strings"
1213

1314
"github.com/fatih/color"
@@ -29,6 +30,7 @@ func GetCommands() *core.Commands {
2930
configUnsetCommand(),
3031
configDumpCommand(),
3132
configProfileCommand(),
33+
configListProfilesCommand(),
3234
configDeleteProfileCommand(),
3335
configActivateProfileCommand(),
3436
configResetCommand(),
@@ -424,6 +426,54 @@ func configProfileCommand() *core.Command {
424426
}
425427
}
426428

429+
// configListProfilesCommand lists all profiles in the config file
430+
func configListProfilesCommand() *core.Command {
431+
return &core.Command{
432+
Groups: []string{"config"},
433+
Short: `List all profiles in the config file`,
434+
Namespace: "config",
435+
Resource: "profile",
436+
Verb: "list",
437+
AllowAnonymousClient: true,
438+
ArgsType: reflect.TypeOf(struct{}{}),
439+
ArgSpecs: core.ArgSpecs{},
440+
Run: func(ctx context.Context, argsI any) (i any, e error) {
441+
configPath := core.ExtractConfigPath(ctx)
442+
config, err := scw.LoadConfigFromPath(configPath)
443+
type profile struct {
444+
Name string
445+
DefaultZone *string
446+
DefaultRegion *string
447+
DefaultProjectID *string
448+
DefaultOrganizationID *string
449+
APIURL *string
450+
}
451+
if err != nil {
452+
return nil, err
453+
}
454+
455+
profiles := []profile{}
456+
457+
for key, value := range config.Profiles {
458+
profiles = append(profiles, profile{
459+
Name: key,
460+
DefaultRegion: value.DefaultRegion,
461+
DefaultZone: value.DefaultZone,
462+
DefaultOrganizationID: value.DefaultOrganizationID,
463+
DefaultProjectID: value.DefaultProjectID,
464+
APIURL: value.APIURL,
465+
})
466+
}
467+
468+
sort.Slice(profiles, func(i, j int) bool {
469+
return profiles[i].Name < profiles[j].Name
470+
})
471+
472+
return profiles, nil
473+
},
474+
}
475+
}
476+
427477
// configDeleteProfileCommand deletes a profile from the config
428478
func configDeleteProfileCommand() *core.Command {
429479
type configDeleteProfileArgs struct {

0 commit comments

Comments
 (0)