mirror of
https://gitea.com/gitea/tea.git
synced 2025-09-02 01:48:30 +02:00
Add git helper (#612)
Add support to tea login with helper same another tools and `gh` Reviewed-on: https://gitea.com/gitea/tea/pulls/612 Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com> Co-committed-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
This commit is contained in:

committed by
6543

parent
a35bf931ae
commit
2984ad4964
@ -18,10 +18,11 @@ import (
|
||||
func CreateLogin() error {
|
||||
var (
|
||||
name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCertPrincipal, sshKeyFingerprint string
|
||||
insecure, sshAgent, versionCheck bool
|
||||
insecure, sshAgent, versionCheck, helper bool
|
||||
)
|
||||
|
||||
versionCheck = true
|
||||
helper = false
|
||||
|
||||
promptI := &survey.Input{Message: "URL of Gitea instance: "}
|
||||
if err := survey.AskOne(promptI, &giteaURL, survey.WithValidator(survey.Required)); err != nil {
|
||||
@ -38,12 +39,12 @@ func CreateLogin() error {
|
||||
return err
|
||||
}
|
||||
|
||||
promptI = &survey.Input{Message: "Name of new Login [" + name + "]: "}
|
||||
promptI = &survey.Input{Message: "Name of new Login: ", Default: name}
|
||||
if err := survey.AskOne(promptI, &name); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
loginMethod, err := promptSelect("Login with: ", []string{"token", "ssh-key/certificate"}, "", "")
|
||||
loginMethod, err := promptSelectV2("Login with: ", []string{"token", "ssh-key/certificate"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -145,6 +146,14 @@ func CreateLogin() error {
|
||||
return err
|
||||
}
|
||||
|
||||
promptYN = &survey.Confirm{
|
||||
Message: "Add git helper: ",
|
||||
Default: false,
|
||||
}
|
||||
if err = survey.AskOne(promptYN, &helper); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
promptYN = &survey.Confirm{
|
||||
Message: "Check version of Gitea instance: ",
|
||||
Default: true,
|
||||
@ -155,7 +164,7 @@ func CreateLogin() error {
|
||||
|
||||
}
|
||||
|
||||
return task.CreateLogin(name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCertPrincipal, sshKeyFingerprint, insecure, sshAgent, versionCheck)
|
||||
return task.CreateLogin(name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCertPrincipal, sshKeyFingerprint, insecure, sshAgent, versionCheck, helper)
|
||||
}
|
||||
|
||||
var tokenScopeOpts = []string{
|
||||
|
@ -127,6 +127,24 @@ func promptMultiSelect(prompt string, options []string, customVal string) ([]str
|
||||
return promptCustomVal(prompt, customVal, selection)
|
||||
}
|
||||
|
||||
// promptSelectV2 creates a generic select prompt
|
||||
func promptSelectV2(prompt string, options []string) (string, error) {
|
||||
if len(options) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
var selection string
|
||||
promptA := &survey.Select{
|
||||
Message: prompt,
|
||||
Options: options,
|
||||
VimMode: true,
|
||||
Default: options[0],
|
||||
}
|
||||
if err := survey.AskOne(promptA, &selection); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return selection, nil
|
||||
}
|
||||
|
||||
// promptSelect creates a generic select prompt, with processing of custom values or none-option.
|
||||
func promptSelect(prompt string, options []string, customVal, noneVal string) (string, error) {
|
||||
var selection string
|
||||
|
@ -6,6 +6,7 @@ package task
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -15,8 +16,49 @@ import (
|
||||
"code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
// SetupHelper add tea helper to config global
|
||||
func SetupHelper(login config.Login) (ok bool, err error) {
|
||||
// Check that the URL is not blank
|
||||
if login.URL == "" {
|
||||
return false, fmt.Errorf("Invalid gitea url")
|
||||
}
|
||||
|
||||
// get tea binary path
|
||||
var binPath string
|
||||
if binPath, err = os.Executable(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// get all helper to URL in git config
|
||||
var currentHelpers []byte
|
||||
if currentHelpers, err = exec.Command("git", "config", "--global", "--get-all", fmt.Sprintf("credential.%s.helper", login.URL)).Output(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// Check if ared added tea helper
|
||||
for _, line := range strings.Split(strings.ReplaceAll(string(currentHelpers), "\r", ""), "\n") {
|
||||
if strings.TrimSpace(line) == "" {
|
||||
continue
|
||||
} else if strings.HasPrefix(line, binPath) && strings.Contains(line[len(binPath):], "login helper") {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Check if tea path have space, if have add quotes
|
||||
if strings.Contains(binPath, " ") {
|
||||
binPath = fmt.Sprintf("%q", binPath)
|
||||
}
|
||||
|
||||
// Add tea helper
|
||||
if _, err = exec.Command("git", "config", "--global", "--add", fmt.Sprintf("credential.%s.helper", login.URL), fmt.Sprintf("!%s login helper", binPath)).Output(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// CreateLogin create a login to be stored in config
|
||||
func CreateLogin(name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCertPrincipal, sshKeyFingerprint string, insecure, sshAgent, versionCheck bool) error {
|
||||
func CreateLogin(name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCertPrincipal, sshKeyFingerprint string, insecure, sshAgent, versionCheck, addHelper bool) error {
|
||||
// checks ...
|
||||
// ... if we have a url
|
||||
if len(giteaURL) == 0 {
|
||||
@ -105,6 +147,11 @@ 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 {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user