Add Login Manage Functions (#182)

rename Active to Default

manage Default login via CI

use open to edit login config ... for now

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/182
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
6543 2020-09-23 14:23:27 +00:00
parent 3652f1dcb2
commit eacf1be066
3 changed files with 61 additions and 5 deletions

View File

@ -30,7 +30,7 @@ type Login struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Token string `yaml:"token"`
Active bool `yaml:"active"`
Default bool `yaml:"default"`
SSHHost string `yaml:"ssh_host"`
// optional path to the private key
SSHKey string `yaml:"ssh_key"`
@ -119,12 +119,12 @@ func getOwnerAndRepo(repoPath, user string) (string, string) {
return user, repoPath
}
func getActiveLogin() (*Login, error) {
func getDefaultLogin() (*Login, error) {
if len(config.Logins) == 0 {
return nil, errors.New("No available login")
}
for _, l := range config.Logins {
if l.Active {
if l.Default {
return &l, nil
}
}

View File

@ -102,7 +102,7 @@ func initCommand() (*Login, string, string) {
log.Fatal("load config file failed ", yamlConfigPath)
}
if login, err = getActiveLogin(); err != nil {
if login, err = getDefaultLogin(); err != nil {
log.Fatal(err.Error())
}
@ -138,7 +138,7 @@ func initCommandLoginOnly() *Login {
var login *Login
if loginValue == "" {
login, err = getActiveLogin()
login, err = getDefaultLogin()
if err != nil {
log.Fatal(err)
}

View File

@ -17,6 +17,7 @@ import (
"code.gitea.io/sdk/gitea"
"github.com/skratchdot/open-golang/open"
"github.com/urfave/cli/v2"
)
@ -29,9 +30,62 @@ var CmdLogin = cli.Command{
Subcommands: []*cli.Command{
&cmdLoginList,
&cmdLoginAdd,
&cmdLoginEdit,
&cmdLoginSetDefault,
},
}
// cmdLoginEdit represents to login a gitea server.
var cmdLoginEdit = cli.Command{
Name: "edit",
Usage: "Edit Gitea logins",
Description: `Edit Gitea logins`,
Action: runLoginEdit,
Flags: []cli.Flag{&OutputFlag},
}
func runLoginEdit(ctx *cli.Context) error {
return open.Start(yamlConfigPath)
}
// cmdLoginSetDefault represents to login a gitea server.
var cmdLoginSetDefault = cli.Command{
Name: "default",
Usage: "Get or Set Default Login",
Description: `Get or Set Default Login`,
ArgsUsage: "<Login>",
Action: runLoginSetDefault,
Flags: []cli.Flag{&OutputFlag},
}
func runLoginSetDefault(ctx *cli.Context) error {
if err := loadConfig(yamlConfigPath); err != nil {
return err
}
if ctx.Args().Len() == 0 {
l, err := getDefaultLogin()
if err != nil {
return err
}
fmt.Printf("Default Login: %s\n", l.Name)
return nil
}
loginExist := false
for i := range config.Logins {
config.Logins[i].Default = false
if config.Logins[i].Name == ctx.Args().First() {
config.Logins[i].Default = true
loginExist = true
}
}
if !loginExist {
return fmt.Errorf("login '%s' not found", ctx.Args().First())
}
return saveConfig(yamlConfigPath)
}
// CmdLogin represents to login a gitea server.
var cmdLoginAdd = cli.Command{
Name: "add",
@ -287,6 +341,7 @@ func runLoginList(ctx *cli.Context) error {
"URL",
"SSHHost",
"User",
"Default",
}
var values [][]string
@ -297,6 +352,7 @@ func runLoginList(ctx *cli.Context) error {
l.URL,
l.GetSSHHost(),
l.User,
fmt.Sprint(l.Default),
})
}