From f3e55975c0064a06d8275b3d84c1d5173bc5415b Mon Sep 17 00:00:00 2001 From: arafato Date: Tue, 10 Mar 2020 10:47:48 +0100 Subject: [PATCH] added new rm flag for aliyun profile deletion --- cli/rm.go | 19 ++++++++++++++++--- vault/config.go | 11 +++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cli/rm.go b/cli/rm.go index 48fac95..09ad40e 100644 --- a/cli/rm.go +++ b/cli/rm.go @@ -9,8 +9,9 @@ import ( ) type RemoveCommandInput struct { - ProfileName string - Keyring *vault.CredentialKeyring + ProfileName string + AliyunCliProfil bool + Keyring *vault.CredentialKeyring } func ConfigureRemoveCommand(app *kingpin.Application) { @@ -24,6 +25,10 @@ func ConfigureRemoveCommand(app *kingpin.Application) { HintAction(getProfileNames). StringVar(&input.ProfileName) + cmd.Flag("aliyun", "Delete the according Aliyun CLI profile"). + Short('a'). + BoolVar(&input.AliyunCliProfil) + cmd.Action(func(c *kingpin.ParseContext) error { input.Keyring = &vault.CredentialKeyring{Keyring: keyringImpl} RemoveCommand(app, input) @@ -44,5 +49,13 @@ func RemoveCommand(app *kingpin.Application, input RemoveCommandInput) { app.Fatalf(err.Error()) return } - fmt.Printf("Deleted profile.\n") + fmt.Printf("Deleted profile from Keyvault.\n") + + if input.AliyunCliProfil { + if err := configLoader.DeleteProfile(input.ProfileName); err != nil { + app.Fatalf(err.Error()) + return + } + fmt.Printf("Deleted profile from Aliyun CLI config.\n") + } } diff --git a/vault/config.go b/vault/config.go index eb76a3d..a04e1e8 100644 --- a/vault/config.go +++ b/vault/config.go @@ -56,6 +56,17 @@ func (cl *ConfigLoader) LoadProfile(profileName string) (*Config, error) { return &cl.BaseConfig, nil } +func (cl *ConfigLoader) DeleteProfile(profileName string) error { + newConfig := config.NewConfiguration() + for _, p := range cl.AliyunConfig.Profiles { + if p.Name != profileName { + newConfig.PutProfile(p) + } + } + err := config.SaveConfiguration(newConfig) + return err +} + func (cl *ConfigLoader) AddNewProfile(name string) error { p, exists := cl.AliyunConfig.GetProfile(name) if !exists {