From eacf1be0660f8d4b2d4ec7dd0bdc20fa866c0b2e Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Wed, 23 Sep 2020 14:23:27 +0000 Subject: [PATCH] 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 Reviewed-by: Norwin --- cmd/config.go | 6 +++--- cmd/flags.go | 4 ++-- cmd/login.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 4fc52ae..472e1e7 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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 } } diff --git a/cmd/flags.go b/cmd/flags.go index df07edc..2ee623e 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -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) } diff --git a/cmd/login.go b/cmd/login.go index ae57320..97d6053 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -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: "", + 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), }) }