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 (
"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)
}

View File

@ -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)