Check if tea is present in helper

This commit is contained in:
Matheus Sampaio Queiroga 2024-02-15 21:07:27 -03:00
parent ff7b53468f
commit 9f90b0c053
No known key found for this signature in database
GPG Key ID: 01CCABE2580AFEBC
2 changed files with 27 additions and 16 deletions

View File

@ -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
},

View File

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