mirror of https://gitea.com/gitea/tea.git
Check if tea is present in helper
This commit is contained in:
parent
ff7b53468f
commit
9f90b0c053
|
@ -40,13 +40,15 @@ var CmdLoginHelper = cli.Command{
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for loginIndex := range logins {
|
||||
login := logins[loginIndex]
|
||||
err = task.SetupHelper(login)
|
||||
for _, login := range logins {
|
||||
added, err := task.SetupHelper(login)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if added {
|
||||
fmt.Printf("Added \"%s\"\n", login.Name)
|
||||
} else {
|
||||
fmt.Printf("\"%s\" has already been added!\n", login.Name)
|
||||
}
|
||||
fmt.Printf("Added %s\n", login.Name)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
|
|
@ -17,23 +17,32 @@ import (
|
|||
)
|
||||
|
||||
// SetupHelper add tea helper to config global
|
||||
func SetupHelper(login config.Login) error {
|
||||
// Remove old helper
|
||||
exec.Command("git", "config", "--global", "--unset-all", fmt.Sprintf("credential.%s.helper", login.URL)).Run()
|
||||
|
||||
// force command line to use tea
|
||||
_, err := exec.Command("git", "config", "--global", fmt.Sprintf("credential.%s.helper", login.URL), "").Output()
|
||||
if err != nil {
|
||||
return err
|
||||
func SetupHelper(login config.Login) (bool, error) {
|
||||
// Check that the URL is not blank
|
||||
if login.URL == "" {
|
||||
return false, fmt.Errorf("Invalid gitea url")
|
||||
}
|
||||
|
||||
// get all helper if present in global config
|
||||
out, err := exec.Command("git", "config", "--global", "--get-all", fmt.Sprintf("credential.%s.helper", login.URL)).Output()
|
||||
if err == nil {
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
if line[len(line)-16:] == "tea login helper" {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get tea binary path
|
||||
binPath, _ := os.Executable()
|
||||
|
||||
// Add tea helper
|
||||
_, err = exec.Command("git", "config", "--global", "--add", fmt.Sprintf("credential.%s.helper", login.URL), "!tea login helper").Output()
|
||||
_, err = exec.Command("git", "config", "--global", "--add", fmt.Sprintf("credential.%s.helper", login.URL), fmt.Sprintf("!%s login helper", binPath)).Output()
|
||||
if err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
|
||||
return nil
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// CreateLogin create a login to be stored in config
|
||||
|
@ -127,7 +136,7 @@ func CreateLogin(name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCe
|
|||
|
||||
fmt.Printf("Login as %s on %s successful. Added this login as %s\n", login.User, login.URL, login.Name)
|
||||
if addHelper {
|
||||
if err = SetupHelper(login); err != nil {
|
||||
if _, err := SetupHelper(login); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue