From f5dbd44ebe401938dfdaf849757db89dde473edf Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Fri, 2 Oct 2020 15:45:55 +0000 Subject: [PATCH] Improve `tea logout` (#213) fix message fix lint Impruve logout Co-authored-by: techknowlogick Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/213 Reviewed-by: Norwin Reviewed-by: techknowlogick --- cmd/logout.go | 35 +++++++++++------------------------ modules/config/login.go | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/cmd/logout.go b/cmd/logout.go index 23e5afe..8880480 100644 --- a/cmd/logout.go +++ b/cmd/logout.go @@ -7,7 +7,6 @@ package cmd import ( "errors" "log" - "os" "code.gitea.io/tea/modules/config" @@ -30,34 +29,22 @@ var CmdLogout = cli.Command{ } func runLogout(ctx *cli.Context) error { - var name string - if len(os.Args) == 3 { - name = os.Args[2] - } else if ctx.IsSet("name") { - name = ctx.String("name") - } else { - return errors.New("Please specify a login name") - } - err := config.LoadConfig() if err != nil { log.Fatal(err) } - var idx = -1 - for i, l := range config.Config.Logins { - if l.Name == name { - idx = i - break - } - } - if idx > -1 { - config.Config.Logins = append(config.Config.Logins[:idx], config.Config.Logins[idx+1:]...) - err = config.SaveConfig() - if err != nil { - log.Fatal(err) - } + var name string + + if ctx.IsSet("name") { + name = ctx.String("name") + } else if len(ctx.Args().First()) != 0 { + name = ctx.Args().First() + } else if len(config.Config.Logins) == 1 { + name = config.Config.Logins[0].Name + } else { + return errors.New("Please specify a login name") } - return nil + return config.DeleteLogin(name) } diff --git a/modules/config/login.go b/modules/config/login.go index 374b074..e781918 100644 --- a/modules/config/login.go +++ b/modules/config/login.go @@ -184,6 +184,24 @@ func AddLogin(name, token, user, passwd, sshKey, giteaURL string, insecure bool) return nil } +// DeleteLogin delete a login by name +func DeleteLogin(name string) error { + var idx = -1 + for i, l := range Config.Logins { + if l.Name == name { + idx = i + break + } + } + if idx == -1 { + return fmt.Errorf("can not delete login '%s', does not exist", name) + } + + Config.Logins = append(Config.Logins[:idx], Config.Logins[idx+1:]...) + + return SaveConfig() +} + // GenerateLoginName generates a name string based on instance URL & adds username if the result is not unique func GenerateLoginName(url, user string) (string, error) { parsedURL, err := utils.NormalizeURL(url)