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:
Matheus Sampaio Queiroga
2024-08-26 11:34:36 +00:00
committed by 6543
parent a35bf931ae
commit 2984ad4964
9 changed files with 226 additions and 186 deletions

View File

@ -28,6 +28,7 @@ var CmdLogin = cli.Command{
&login.CmdLoginEdit,
&login.CmdLoginDelete,
&login.CmdLoginSetDefault,
&login.CmdLoginHelper,
},
}

View File

@ -84,6 +84,11 @@ var CmdLoginAdd = cli.Command{
Aliases: []string{"a"},
Usage: "Use SSH public key or SSH fingerprint to login (needs a running ssh-agent with ssh key loaded)",
},
&cli.BoolFlag{
Name: "helper",
Aliases: []string{"j"},
Usage: "Add helper",
},
},
Action: runLoginAdd,
}
@ -113,5 +118,7 @@ func runLoginAdd(ctx *cli.Context) error {
ctx.String("ssh-agent-key"),
ctx.Bool("insecure"),
sshAgent,
!ctx.Bool("no-version-check"))
!ctx.Bool("no-version-check"),
ctx.Bool("helper"),
)
}

112
cmd/login/helper.go Normal file
View File

@ -0,0 +1,112 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package login
import (
"bufio"
"fmt"
"log"
"net/url"
"os"
"strings"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/task"
"github.com/urfave/cli/v2"
)
// CmdLoginHelper represents to login a gitea helper.
var CmdLoginHelper = cli.Command{
Name: "helper",
Aliases: []string{"git-credential"},
Usage: "Git helper",
Description: `Git helper`,
Hidden: true,
Subcommands: []*cli.Command{
{
Name: "store",
Description: "Command drops",
Aliases: []string{"erase"},
Action: func(ctx *cli.Context) error {
return nil
},
},
{
Name: "setup",
Description: "Setup helper to tea authenticate",
Action: func(ctx *cli.Context) error {
logins, err := config.GetLogins()
if err != nil {
return err
}
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)
}
}
return nil
},
},
{
Name: "get",
Description: "Get token to auth",
Action: func(cmd *cli.Context) error {
wants := map[string]string{}
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
line := s.Text()
if line == "" {
break
}
parts := strings.SplitN(line, "=", 2)
if len(parts) < 2 {
continue
}
key, value := parts[0], parts[1]
if key == "url" {
u, err := url.Parse(value)
if err != nil {
return err
}
wants["protocol"] = u.Scheme
wants["host"] = u.Host
wants["path"] = u.Path
wants["username"] = u.User.Username()
wants["password"], _ = u.User.Password()
} else {
wants[key] = value
}
}
if len(wants["host"]) == 0 {
log.Fatal("Require hostname")
} else if len(wants["protocol"]) == 0 {
wants["protocol"] = "http"
}
userConfig := config.GetLoginByHost(wants["host"])
if len(userConfig.Token) == 0 {
log.Fatal("User no set")
}
host, err := url.Parse(userConfig.URL)
if err != nil {
return err
}
_, err = fmt.Fprintf(os.Stdout, "protocol=%s\nhost=%s\nusername=%s\npassword=%s\n", host.Scheme, host.Host, userConfig.User, userConfig.Token)
if err != nil {
return err
}
return nil
},
},
},
}