44 "bytes"
55 "context"
66 "fmt"
7- "path"
87 "reflect"
98
109 "github.com/scaleway/scaleway-sdk-go/validation"
@@ -27,6 +26,7 @@ func GetCommands() *core.Commands {
2726 configDumpCommand (),
2827 configProfileCommand (),
2928 configDeleteProfileCommand (),
29+ configActivateProfileCommand (),
3030 configResetCommand (),
3131 )
3232}
@@ -128,7 +128,7 @@ func configGetCommand() *core.Command {
128128 },
129129 },
130130 Run : func (ctx context.Context , argsI interface {}) (i interface {}, e error ) {
131- config , err := scw .LoadConfigFromPath (extractConfigPath (ctx ))
131+ config , err := scw .LoadConfigFromPath (core . ExtractConfigPath (ctx ))
132132 if err != nil {
133133 return nil , err
134134 }
@@ -254,7 +254,7 @@ The only allowed attributes are access_key, secret_key, default_organization_id,
254254 args := argsI .(* scw.Profile )
255255
256256 // Execute
257- configPath := extractConfigPath (ctx )
257+ configPath := core . ExtractConfigPath (ctx )
258258 config , err := scw .LoadConfigFromPath (configPath )
259259 if err != nil {
260260 return nil , err
@@ -263,7 +263,7 @@ The only allowed attributes are access_key, secret_key, default_organization_id,
263263 // send_telemetry is the only key that is not in a profile but in the config object directly
264264 profileName := core .ExtractProfileName (ctx )
265265 profile := & config .Profile
266- if profileName != "" {
266+ if profileName != scw . DefaultProfileName {
267267 var exist bool
268268 profile , exist = config .Profiles [profileName ]
269269 if ! exist {
@@ -319,7 +319,7 @@ func configUnsetCommand() *core.Command {
319319 },
320320 },
321321 Run : func (ctx context.Context , argsI interface {}) (i interface {}, e error ) {
322- configPath := extractConfigPath (ctx )
322+ configPath := core . ExtractConfigPath (ctx )
323323 config , err := scw .LoadConfigFromPath (configPath )
324324 if err != nil {
325325 return nil , err
@@ -365,7 +365,7 @@ func configDumpCommand() *core.Command {
365365 },
366366 },
367367 Run : func (ctx context.Context , argsI interface {}) (i interface {}, e error ) {
368- configPath := extractConfigPath (ctx )
368+ configPath := core . ExtractConfigPath (ctx )
369369 config , err := scw .LoadConfigFromPath (configPath )
370370 if err != nil {
371371 return nil , err
@@ -406,7 +406,7 @@ func configDeleteProfileCommand() *core.Command {
406406 },
407407 Run : func (ctx context.Context , argsI interface {}) (i interface {}, e error ) {
408408 profileName := argsI .(* configDeleteProfileArgs ).Name
409- configPath := extractConfigPath (ctx )
409+ configPath := core . ExtractConfigPath (ctx )
410410 config , err := scw .LoadConfigFromPath (configPath )
411411 if err != nil {
412412 return nil , err
@@ -428,6 +428,56 @@ func configDeleteProfileCommand() *core.Command {
428428 }
429429}
430430
431+ // configActivateProfileCommand mark a profile as active
432+ func configActivateProfileCommand () * core.Command {
433+ type configActiveProfileArgs struct {
434+ ProfileName string
435+ }
436+
437+ return & core.Command {
438+ Short : `Mark a profile as active in the config file` ,
439+ Namespace : "config" ,
440+ Resource : "profile" ,
441+ Verb : "activate" ,
442+ AllowAnonymousClient : true ,
443+ ArgsType : reflect .TypeOf (configActiveProfileArgs {}),
444+ ArgSpecs : core.ArgSpecs {
445+ {
446+ Name : "profile-name" ,
447+ Required : true ,
448+ Positional : true ,
449+ AutoCompleteFunc : core .AutocompleteProfileName (),
450+ },
451+ },
452+ Run : func (ctx context.Context , argsI interface {}) (i interface {}, e error ) {
453+ profileName := argsI .(* configActiveProfileArgs ).ProfileName
454+ configPath := core .ExtractConfigPath (ctx )
455+ config , err := scw .LoadConfigFromPath (configPath )
456+ if err != nil {
457+ return nil , err
458+ }
459+
460+ if profileName == scw .DefaultProfileName {
461+ config .ActiveProfile = nil
462+ } else {
463+ if _ , exists := config .Profiles [profileName ]; ! exists {
464+ return nil , unknownProfileError (profileName )
465+ }
466+ config .ActiveProfile = & profileName
467+ }
468+
469+ err = config .SaveTo (configPath )
470+ if err != nil {
471+ return nil , err
472+ }
473+
474+ return & core.SuccessResult {
475+ Message : fmt .Sprintf ("successfully activate profile %s" , profileName ),
476+ }, nil
477+ },
478+ }
479+ }
480+
431481// configResetCommand resets the config
432482func configResetCommand () * core.Command {
433483 type configResetArgs struct {}
@@ -498,14 +548,11 @@ func getProfileKeys() []string {
498548 return keys
499549}
500550
501- // This func should be removes when core implement it
502- func extractConfigPath (ctx context.Context ) string {
503- homeDir := core .ExtractUserHomeDir (ctx )
504- return path .Join (homeDir , ".config" , "scw" , "config.yaml" )
505- }
506-
551+ // getProfile return a config profile by its name.
552+ // Warning: This return the profile pointer directly so it can be modified by commands.
553+ // For this reason we cannot rely on config.GetProfileByName method as it create a copy.
507554func getProfile (config * scw.Config , profileName string ) (* scw.Profile , error ) {
508- if profileName == "" {
555+ if profileName == scw . DefaultProfileName {
509556 return & config .Profile , nil
510557 }
511558 profile , exist := config .Profiles [profileName ]
0 commit comments