Improve `tea logout` (#213)

fix message

fix lint

Impruve logout

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/213
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
6543 2020-10-02 15:45:55 +00:00
parent 3bfae84d32
commit f5dbd44ebe
2 changed files with 29 additions and 24 deletions

View File

@ -7,7 +7,6 @@ package cmd
import ( import (
"errors" "errors"
"log" "log"
"os"
"code.gitea.io/tea/modules/config" "code.gitea.io/tea/modules/config"
@ -30,34 +29,22 @@ var CmdLogout = cli.Command{
} }
func runLogout(ctx *cli.Context) error { 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() err := config.LoadConfig()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
var idx = -1 var name string
for i, l := range config.Config.Logins {
if l.Name == name { if ctx.IsSet("name") {
idx = i name = ctx.String("name")
break } else if len(ctx.Args().First()) != 0 {
} name = ctx.Args().First()
} } else if len(config.Config.Logins) == 1 {
if idx > -1 { name = config.Config.Logins[0].Name
config.Config.Logins = append(config.Config.Logins[:idx], config.Config.Logins[idx+1:]...) } else {
err = config.SaveConfig() return errors.New("Please specify a login name")
if err != nil {
log.Fatal(err)
}
} }
return nil return config.DeleteLogin(name)
} }

View File

@ -184,6 +184,24 @@ func AddLogin(name, token, user, passwd, sshKey, giteaURL string, insecure bool)
return nil 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 // GenerateLoginName generates a name string based on instance URL & adds username if the result is not unique
func GenerateLoginName(url, user string) (string, error) { func GenerateLoginName(url, user string) (string, error) {
parsedURL, err := utils.NormalizeURL(url) parsedURL, err := utils.NormalizeURL(url)