mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-27 03:21:25 +01:00
feat!: rebuild tea on Cobra with explicit noun/verb commands (#1126)
- **switch tea to the Cobra command tree:** `internal/app` wires the packages from parts 1–4 into explicit noun/verb commands with `--repo`, `--json` / `--jq`, `--sudo` and `--debug`; `main.go` points at it. Build, release and docs tooling (`tools/cli-docs`, Makefile, goreleaser, snap, CI) and README / CONTRIBUTING are updated. - **remove the urfave/cli command tree:** deletes `cmd/`, `modules/`, the old integration tests, contrib completion scripts and the generated `docs/CLI.md`, and drops their dependencies. This commit only deletes. ## Breaking changes This is a breaking release. It requires Gitea 28 or newer; configuration moves to separate `config.yml` and `hosts.yml` files and requires fresh authentication, and command names, aliases, flags and output formats change. `docs/migration-1.0.md` maps every old command, flag and environment variable to its replacement. ## Issues fixes #1118 fixes #1116 fixes #1115 fixes #1094 fixes #1091 fixes #1073 fixes #1052 fixes #1032 fixes #1023 fixes #966 fixes #908 fixes #833 fixes #831 fixes #817 fixes #804 fixes #779 fixes #770 fixes #737 fixes #697 fixes #726 fixes #677 fixes #672 fixes #633 fixes #632 fixes #560 fixes #556 fixes #523 fixes #516 fixes #510 fixes #504 fixes #500 fixes #482 fixes #406 fixes #403 fixes #373 fixes #351 fixes #344 fixes #326 fixes #325 fixes #323 fixes #246 fixes #240 fixes #232 fixes #197 fixes #172 fixes #141 fixes #69 fixes #1110 fixes #569 fixes #480 fixes #666 fixes #670 fixes #457 fixes #643 fixes #498 fixes #490 fixes #1069 fixes #834 fixes #602 fixes #328 fixes #417 fixes #571 fixes #1029 fixes #489 fixes #1050 fixes #162 fixes #430 fixes #1087 fixes #683 Reviewed-on: https://gitea.com/gitea/tea/pulls/1126 Reviewed-by: TheFox0x7 <95654+thefox0x7@noreply.gitea.com>
This commit is contained in:
1 parent
12e415a7a7
commit
f02245449b
456 files changed
+18892
-34015
No files matched your search
@@ -1,258 +0,0 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package task
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
gitea "gitea.dev/sdk"
|
||||
|
||||
"gitea.dev/tea/modules/config"
|
||||
"gitea.dev/tea/modules/utils"
|
||||
)
|
||||
|
||||
// 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 all helper to URL in git config
|
||||
helperKey := fmt.Sprintf("credential.%s.helper", login.URL)
|
||||
var currentHelpers []byte
|
||||
if currentHelpers, err = exec.Command("git", "config", "--global", "--get-all", helperKey).Output(); err != nil {
|
||||
currentHelpers = []byte{}
|
||||
}
|
||||
|
||||
// Check if ared added tea helper
|
||||
for _, line := range strings.Split(strings.ReplaceAll(string(currentHelpers), "\r", ""), "\n") {
|
||||
if strings.HasSuffix(strings.TrimSpace(line), "login helper") {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Add tea helper
|
||||
if _, err = exec.Command("git", "config", "--global", helperKey, "").Output(); err != nil {
|
||||
return false, fmt.Errorf("git config --global %s, error: %s", helperKey, err)
|
||||
} else if _, err = exec.Command("git", "config", "--global", "--add", helperKey, "!tea login helper").Output(); err != nil {
|
||||
return false, fmt.Errorf("git config --global --add %s %s, error: %s", helperKey, "!tea login helper", err)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// HasGitCredentialHelper reports whether tea is registered as a git credential
|
||||
// helper for the given login. It mirrors the global git config lookup used by
|
||||
// SetupHelper.
|
||||
func HasGitCredentialHelper(login config.Login) bool {
|
||||
if login.URL == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
helperKey := fmt.Sprintf("credential.%s.helper", login.URL)
|
||||
currentHelpers, err := exec.Command("git", "config", "--global", "--get-all", helperKey).Output()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, line := range strings.Split(strings.ReplaceAll(string(currentHelpers), "\r", ""), "\n") {
|
||||
if strings.HasSuffix(strings.TrimSpace(line), "login helper") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// CreateLogin create a login to be stored in config
|
||||
func CreateLogin(ctx stdctx.Context, 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 {
|
||||
return fmt.Errorf("Gitea server URL is required")
|
||||
}
|
||||
|
||||
// ... if there already exist a login with same name
|
||||
if login, err := config.GetLoginByName(name); err != nil {
|
||||
return err
|
||||
} else if login != nil {
|
||||
return fmt.Errorf("login name '%s' has already been used", login.Name)
|
||||
}
|
||||
// ... if we already use this token
|
||||
if shouldCheckTokenUniqueness(token, sshAgent, sshKey, sshCertPrincipal, sshKeyFingerprint) {
|
||||
login, err := config.GetLoginByToken(token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if login != nil {
|
||||
return fmt.Errorf("token already been used, delete login '%s' first", login.Name)
|
||||
}
|
||||
}
|
||||
|
||||
serverURL, err := utils.ValidateAuthenticationMethod(
|
||||
giteaURL,
|
||||
token,
|
||||
user,
|
||||
passwd,
|
||||
sshAgent,
|
||||
sshKey,
|
||||
sshCertPrincipal,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// check if it's a certificate the principal doesn't matter as the user
|
||||
// has explicitly selected this private key
|
||||
if _, err := os.Stat(sshKey + "-cert.pub"); err == nil {
|
||||
sshCertPrincipal = "yes"
|
||||
}
|
||||
|
||||
login := config.Login{
|
||||
Name: name,
|
||||
URL: serverURL.String(),
|
||||
Token: token,
|
||||
Insecure: insecure,
|
||||
SSHKey: sshKey,
|
||||
SSHCertPrincipal: sshCertPrincipal,
|
||||
SSHKeyFingerprint: sshKeyFingerprint,
|
||||
SSHAgent: sshAgent,
|
||||
Created: time.Now().Unix(),
|
||||
VersionCheck: versionCheck,
|
||||
}
|
||||
|
||||
if len(token) == 0 && sshCertPrincipal == "" && !sshAgent && sshKey == "" {
|
||||
if login.Token, err = generateToken(ctx, login, user, passwd, otp, scopes); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
client := login.Client()
|
||||
|
||||
// Verify if authentication works and get user info
|
||||
u, _, err := client.Users.GetMyUserInfo(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
login.User = u.UserName
|
||||
|
||||
if len(login.Name) == 0 {
|
||||
if login.Name, err = GenerateLoginName(giteaURL, login.User); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// we do not have a method to get SSH config from api,
|
||||
// so we just use the host
|
||||
login.SSHHost = serverURL.Host
|
||||
|
||||
if shouldFindSSHKey(sshKey, sshAgent) {
|
||||
login.SSHKey, err = findSSHKey(ctx, client)
|
||||
if err != nil {
|
||||
fmt.Printf("Warning: problem while finding a SSH key: %s\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err = config.AddLogin(&login); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Tip: pass --git-credentials (or run 'tea login helper setup') to authenticate 'git push' and 'git clone' over HTTPS with this token.")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func shouldFindSSHKey(sshKey string, sshAgent bool) bool {
|
||||
return sshKey == "" && !sshAgent
|
||||
}
|
||||
|
||||
func shouldCheckTokenUniqueness(token string, sshAgent bool, sshKey, sshCertPrincipal, sshKeyFingerprint string) bool {
|
||||
if sshAgent || sshKey != "" || sshCertPrincipal != "" || sshKeyFingerprint != "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// generateToken creates a new token when given BasicAuth credentials
|
||||
func generateToken(ctx stdctx.Context, login config.Login, user, pass, otp, scopes string) (string, error) {
|
||||
opts := []gitea.ClientOption{gitea.SetBasicAuth(user, pass)}
|
||||
if otp != "" {
|
||||
opts = append(opts, gitea.SetOTP(otp))
|
||||
}
|
||||
client := login.Client(opts...)
|
||||
|
||||
var tl []*gitea.AccessToken
|
||||
for page := 1; ; {
|
||||
page_tokens, resp, err := client.Users.ListAccessTokens(ctx, gitea.ListAccessTokensOptions{
|
||||
ListOptions: gitea.ListOptions{Page: page, PageSize: 50},
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
tl = append(tl, page_tokens...)
|
||||
if resp == nil || resp.NextPage == 0 {
|
||||
break
|
||||
}
|
||||
page = resp.NextPage
|
||||
}
|
||||
host, _ := os.Hostname()
|
||||
tokenName := host + "-tea"
|
||||
|
||||
// append timestamp, if a token with this hostname already exists
|
||||
for i := range tl {
|
||||
if tl[i].Name == tokenName {
|
||||
tokenName += time.Now().Format("2006-01-02_15-04-05")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
var tokenScopes []gitea.AccessTokenScope
|
||||
if len(scopes) == 0 {
|
||||
tokenScopes = []gitea.AccessTokenScope{gitea.AccessTokenScopeAll}
|
||||
} else {
|
||||
for _, scope := range strings.Split(scopes, ",") {
|
||||
tokenScopes = append(tokenScopes, gitea.AccessTokenScope(strings.TrimSpace(scope)))
|
||||
}
|
||||
}
|
||||
|
||||
t, _, err := client.Users.CreateAccessToken(ctx, gitea.CreateAccessTokenOption{
|
||||
Name: tokenName,
|
||||
Scopes: tokenScopes,
|
||||
})
|
||||
return t.Token, err
|
||||
}
|
||||
|
||||
// GenerateLoginName generates a name string based on instance URL & adds username if the result is not unique
|
||||
func GenerateLoginName(url, user string) (string, error) {
|
||||
parsedURL, err := utils.NormalizeURL(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
name := parsedURL.Host
|
||||
|
||||
// append user name if login name already exists
|
||||
if len(user) != 0 {
|
||||
if login, err := config.GetLoginByName(name); err != nil {
|
||||
return "", err
|
||||
} else if login != nil {
|
||||
return name + "_" + user, nil
|
||||
}
|
||||
}
|
||||
|
||||
return name, nil
|
||||
}
|
||||
Reference in new issue
Block a user