chore: apply golangci "forbidigo" to all packages (#39151)

This commit is contained in:
wxiaoguang
2026-08-27 16:34:44 +00:00
committed by GitHub
parent 66c672d0b7
commit b7cfa4e67e
17 changed files with 61 additions and 54 deletions
-3
View File
@@ -146,9 +146,6 @@ linters:
- linters: - linters:
- dupl - dupl
path: modelmigration/v path: modelmigration/v
- linters:
- forbidigo
path: cmd
- linters: - linters:
- dupl - dupl
text: (?i)webhook text: (?i)webhook
+1 -2
View File
@@ -5,7 +5,6 @@ package cmd
import ( import (
"context" "context"
"fmt"
"gitea.dev/modules/private" "gitea.dev/modules/private"
"gitea.dev/modules/setting" "gitea.dev/modules/setting"
@@ -49,6 +48,6 @@ func runGenerateActionsRunnerToken(ctx context.Context, c *cli.Command) error {
if extra.HasError() { if extra.HasError() {
return handleCliResponseExtra(extra) return handleCliResponseExtra(extra)
} }
_, _ = fmt.Printf("%s\n", respText.Text) cprintln(c, respText.Text)
return nil return nil
} }
+1 -1
View File
@@ -73,6 +73,6 @@ func runChangePassword(ctx context.Context, c *cli.Command) error {
} }
} }
fmt.Printf("%s's password has been successfully updated!\n", user.Name) cprintf(c, "%s's password has been successfully updated!\n", user.Name)
return nil return nil
} }
+3 -3
View File
@@ -152,7 +152,7 @@ func runCreateUser(ctx context.Context, c *cli.Command) error {
return err return err
} }
// codeql[disable-next-line=go/clear-text-logging] // codeql[disable-next-line=go/clear-text-logging]
fmt.Printf("generated random password is '%s'\n", password) cprintf(c, "generated random password is '%s'\n", password)
} else if userType == user_model.UserTypeIndividual { } else if userType == user_model.UserTypeIndividual {
return errors.New("must set either password or random-password flag") return errors.New("must set either password or random-password flag")
} }
@@ -228,7 +228,7 @@ func runCreateUser(ctx context.Context, c *cli.Command) error {
if err := user_model.CreateUser(ctx, u, &user_model.Meta{}, overwriteDefault); err != nil { if err := user_model.CreateUser(ctx, u, &user_model.Meta{}, overwriteDefault); err != nil {
return fmt.Errorf("CreateUser: %w", err) return fmt.Errorf("CreateUser: %w", err)
} }
fmt.Printf("New user '%s' has been successfully created!\n", username) cprintf(c, "New user '%s' has been successfully created!\n", username)
// create the access token // create the access token
if accessTokenScope != "" { if accessTokenScope != "" {
@@ -236,7 +236,7 @@ func runCreateUser(ctx context.Context, c *cli.Command) error {
if err := auth_model.NewAccessToken(ctx, t); err != nil { if err := auth_model.NewAccessToken(ctx, t); err != nil {
return err return err
} }
fmt.Printf("Access token was successfully created... %s\n", t.Token) cprintf(c, "Access token was successfully created... %s\n", t.Token)
} }
return nil return nil
} }
+1 -1
View File
@@ -67,6 +67,6 @@ func runDisableTwoFactor(ctx context.Context, c *cli.Command) error {
return err return err
} }
fmt.Printf("Disabled 2FA for user %q (removed %d TOTP and %d WebAuthn credential(s))\n", user.Name, totp, webAuthn) cprintf(c, "Disabled 2FA for user %q (removed %d TOTP and %d WebAuthn credential(s))\n", user.Name, totp, webAuthn)
return nil return nil
} }
+2 -2
View File
@@ -88,9 +88,9 @@ func runGenerateAccessToken(ctx context.Context, c *cli.Command) error {
} }
if c.Bool("raw") { if c.Bool("raw") {
fmt.Printf("%s\n", t.Token) cprintln(c, t.Token)
} else { } else {
fmt.Printf("Access token was successfully created: %s\n", t.Token) cprintf(c, "Access token was successfully created: %s\n", t.Token)
} }
return nil return nil
+1 -2
View File
@@ -6,7 +6,6 @@ package cmd
import ( import (
"context" "context"
"errors" "errors"
"fmt"
user_model "gitea.dev/models/user" user_model "gitea.dev/models/user"
"gitea.dev/modules/setting" "gitea.dev/modules/setting"
@@ -59,6 +58,6 @@ func runMustChangePassword(ctx context.Context, c *cli.Command) error {
} }
// codeql[disable-next-line=go/clear-text-logging] // codeql[disable-next-line=go/clear-text-logging]
fmt.Printf("Updated %d users setting MustChangePassword to %t\n", n, mustChangePassword) cprintf(c, "Updated %d users setting MustChangePassword to %t\n", n, mustChangePassword)
return nil return nil
} }
+2 -2
View File
@@ -114,8 +114,8 @@ func runRecreateTable(ctx context.Context, cmd *cli.Command) error {
setting.Database.LogSQL = debug setting.Database.LogSQL = debug
if err := db.InitEngine(ctx); err != nil { if err := db.InitEngine(ctx); err != nil {
fmt.Println(err) cprintln(cmd, err)
fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.") cprintln(cmd, "Check if you are using the right config file. You can use a --config directive to specify one.")
return nil return nil
} }
+3 -4
View File
@@ -5,7 +5,6 @@ package cmd
import ( import (
"context" "context"
"fmt"
"gitea.dev/models/db" "gitea.dev/models/db"
"gitea.dev/modules/log" "gitea.dev/modules/log"
@@ -40,15 +39,15 @@ func runDoctorConvert(ctx context.Context, cmd *cli.Command) error {
log.Fatal("Failed to convert database & table: %v", err) log.Fatal("Failed to convert database & table: %v", err)
return err return err
} }
fmt.Println("Converted successfully, please confirm your database's character set is now utf8mb4") cprintln(cmd, "Converted successfully, please confirm your database's character set is now utf8mb4")
case setting.Database.Type.IsMSSQL(): case setting.Database.Type.IsMSSQL():
if err := db.ConvertVarcharToNVarchar(); err != nil { if err := db.ConvertVarcharToNVarchar(); err != nil {
log.Fatal("Failed to convert database from varchar to nvarchar: %v", err) log.Fatal("Failed to convert database from varchar to nvarchar: %v", err)
return err return err
} }
fmt.Println("Converted successfully, please confirm your database's all columns character is NVARCHAR now") cprintln(cmd, "Converted successfully, please confirm your database's all columns character is NVARCHAR now")
default: default:
fmt.Println("This command can only be used with a MySQL or MSSQL database") cprintln(cmd, "This command can only be used with a MySQL or MSSQL database")
} }
return nil return nil
+8 -8
View File
@@ -150,7 +150,7 @@ func runListDo(c *cli.Command) error {
} }
for _, a := range matchedAssetFiles { for _, a := range matchedAssetFiles {
fmt.Println(a.path) cprintln(c, a.path)
} }
return nil return nil
@@ -194,7 +194,7 @@ func runExtractDo(c *cli.Command) error {
destdir = c.String("destination") destdir = c.String("destination")
} else if c.Bool("custom") { } else if c.Bool("custom") {
destdir = setting.CustomPath destdir = setting.CustomPath
fmt.Println("Using app.ini at", setting.CustomConf) cprintln(c, "Using app.ini at", setting.CustomConf)
} }
fi, err := os.Stat(destdir) fi, err := os.Stat(destdir)
@@ -213,13 +213,13 @@ func runExtractDo(c *cli.Command) error {
return fmt.Errorf("destination %q is not a directory", destdir) return fmt.Errorf("destination %q is not a directory", destdir)
} }
fmt.Printf("Extracting to %s:\n", destdir) cprintf(c, "Extracting to %s:\n", destdir)
overwrite := c.Bool("overwrite") overwrite := c.Bool("overwrite")
rename := c.Bool("rename") rename := c.Bool("rename")
for _, a := range matchedAssetFiles { for _, a := range matchedAssetFiles {
if err := extractAsset(destdir, a, overwrite, rename); err != nil { if err := extractAsset(c, destdir, a, overwrite, rename); err != nil {
// Non-fatal error // Non-fatal error
_, _ = fmt.Fprintf(os.Stderr, "%s: %v\n", a.path, err) _, _ = fmt.Fprintf(os.Stderr, "%s: %v\n", a.path, err)
} }
@@ -228,7 +228,7 @@ func runExtractDo(c *cli.Command) error {
return nil return nil
} }
func extractAsset(d string, a assetFile, overwrite, rename bool) error { func extractAsset(c *cli.Command, d string, a assetFile, overwrite, rename bool) error {
dest := filepath.Join(d, filepath.FromSlash(a.path)) dest := filepath.Join(d, filepath.FromSlash(a.path))
dir := filepath.Dir(dest) dir := filepath.Dir(dest)
@@ -249,7 +249,7 @@ func extractAsset(d string, a assetFile, overwrite, rename bool) error {
return fmt.Errorf("%s: %w", dest, err) return fmt.Errorf("%s: %w", dest, err)
} }
} else if !overwrite && !rename { } else if !overwrite && !rename {
fmt.Printf("%s already exists; skipped.\n", dest) cprintf(c, "%s already exists; skipped.\n", dest)
return nil return nil
} else if !fi.Mode().IsRegular() { } else if !fi.Mode().IsRegular() {
return fmt.Errorf("%s already exists, but it's not a regular file", dest) return fmt.Errorf("%s already exists, but it's not a regular file", dest)
@@ -271,7 +271,7 @@ func extractAsset(d string, a assetFile, overwrite, rename bool) error {
return fmt.Errorf("%s: %w", dest, err) return fmt.Errorf("%s: %w", dest, err)
} }
fmt.Println(dest) cprintln(c, dest)
return nil return nil
} }
@@ -306,7 +306,7 @@ func compileCollectPatterns(args []string) (_ []glob.Glob, err error) {
pat := make([]glob.Glob, len(args)) pat := make([]glob.Glob, len(args))
for i := range args { for i := range args {
if pat[i], err = glob.Compile(args[i], '/'); err != nil { if pat[i], err = glob.Compile(args[i], '/'); err != nil {
return nil, fmt.Errorf("invalid glob patterh %q: %w", args[i], err) return nil, fmt.Errorf("invalid glob pattern %q: %w", args[i], err)
} }
} }
return pat, nil return pat, nil
+8 -8
View File
@@ -106,10 +106,10 @@ func runGenerateInternalToken(_ context.Context, c *cli.Command) error {
return err return err
} }
fmt.Printf("%s", internalToken) cprintf(c, "%s", internalToken)
if isatty.IsTerminal(os.Stdout.Fd()) { if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Printf("\n") cprintf(c, "\n")
} }
return nil return nil
@@ -117,10 +117,10 @@ func runGenerateInternalToken(_ context.Context, c *cli.Command) error {
func runGenerateLfsJwtSecret(_ context.Context, c *cli.Command) error { func runGenerateLfsJwtSecret(_ context.Context, c *cli.Command) error {
_, jwtSecretBase64 := generate.NewJwtSecretWithBase64() _, jwtSecretBase64 := generate.NewJwtSecretWithBase64()
fmt.Printf("%s", jwtSecretBase64) cprintf(c, "%s", jwtSecretBase64)
if isatty.IsTerminal(os.Stdout.Fd()) { if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Printf("\n") cprintf(c, "\n")
} }
return nil return nil
@@ -133,10 +133,10 @@ func runGenerateSecretKey(_ context.Context, c *cli.Command) error {
} }
// codeql[disable-next-line=go/clear-text-logging] // codeql[disable-next-line=go/clear-text-logging]
fmt.Printf("%s", secretKey) cprintf(c, "%s", secretKey)
if isatty.IsTerminal(os.Stdout.Fd()) { if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Printf("\n") cprintf(c, "\n")
} }
return nil return nil
@@ -168,14 +168,14 @@ func runGenerateKeyPair(_ context.Context, c *cli.Command) error {
// Check if file exists to prevent overwriting // Check if file exists to prevent overwriting
if _, err := os.Stat(file); err == nil { if _, err := os.Stat(file); err == nil {
if !confirm(c.Reader, c.Writer, "%s already exists.\nOverwrite (y/n)? ", file) { if !confirm(c.Reader, c.Writer, "%s already exists.\nOverwrite (y/n)? ", file) {
fmt.Println("Aborting") cprintln(c, "Aborting")
return nil return nil
} }
} }
bits := c.Int("bits") bits := c.Int("bits")
err := ssh.GenKeyPair(file, generate.SSHKeyType(keyType), bits) err := ssh.GenKeyPair(file, generate.SSHKeyType(keyType), bits)
if err == nil { if err == nil {
fmt.Printf("Your SSH key has been saved in %s\n", file) cprintf(c, "Your SSH key has been saved in %s\n", file)
} }
return err return err
} }
+10 -4
View File
@@ -1,8 +1,6 @@
// Copyright 2018 The Gitea Authors. All rights reserved. // Copyright 2018 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
// Package cmd provides subcommands to the gitea binary - such as "web" or
// "admin".
package cmd package cmd
import ( import (
@@ -22,8 +20,8 @@ import (
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
) )
// argsSet checks that all the required arguments are set. args is a list of // argsSet checks that all the required arguments are set.
// arguments that must be set in the passed Context. // args is a list of arguments that must be set in the command context.
func argsSet(c *cli.Command, args ...string) error { func argsSet(c *cli.Command, args ...string) error {
for _, a := range args { for _, a := range args {
if !c.IsSet(a) { if !c.IsSet(a) {
@@ -141,3 +139,11 @@ func isValidDefaultSubCommand(cmd *cli.Command) (string, bool) {
} }
return "", true return "", true
} }
func cprintf(c *cli.Command, format string, args ...any) {
_, _ = fmt.Fprintf(c.Writer, format, args...)
}
func cprintln(c *cli.Command, args ...any) {
_, _ = fmt.Fprintln(c.Writer, args...)
}
+3 -4
View File
@@ -5,7 +5,6 @@ package cmd
import ( import (
"context" "context"
"fmt"
"gitea.dev/modules/private" "gitea.dev/modules/private"
"gitea.dev/modules/setting" "gitea.dev/modules/setting"
@@ -22,11 +21,11 @@ func runSendMail(ctx context.Context, c *cli.Command) error {
if !confirmSkipped { if !confirmSkipped {
if len(body) == 0 { if len(body) == 0 {
fmt.Println("warning: Content is empty") cprintln(c, "warning: Content is empty")
} }
if !confirm(c.Reader, c.Writer, "Proceed with sending email? [Y/n] ") { if !confirm(c.Reader, c.Writer, "Proceed with sending email? [Y/n] ") {
fmt.Println("The mail was not sent") cprintln(c, "The mail was not sent")
return nil return nil
} }
} }
@@ -35,6 +34,6 @@ func runSendMail(ctx context.Context, c *cli.Command) error {
if extra.HasError() { if extra.HasError() {
return handleCliResponseExtra(extra) return handleCliResponseExtra(extra)
} }
_, _ = fmt.Printf("Sent %s email(s) to all users\n", respText.Text) cprintf(c, "Sent %s email(s) to all users\n", respText.Text)
return nil return nil
} }
+12 -7
View File
@@ -29,6 +29,7 @@ import (
"gitea.dev/modules/process" "gitea.dev/modules/process"
repo_module "gitea.dev/modules/repository" repo_module "gitea.dev/modules/repository"
"gitea.dev/modules/setting" "gitea.dev/modules/setting"
"gitea.dev/services/agit"
"gitea.dev/services/lfs" "gitea.dev/services/lfs"
"github.com/kballard/go-shellquote" "github.com/kballard/go-shellquote"
@@ -139,13 +140,13 @@ func runServ(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug")) setup(ctx, c.Bool("debug"))
if setting.SSH.Disabled { if setting.SSH.Disabled {
println("Gitea: SSH has been disabled") cprintln(c, "Gitea: SSH has been disabled")
return nil return nil
} }
if c.NArg() < 1 { if c.NArg() < 1 {
if err := cli.ShowSubcommandHelp(c); err != nil { if err := cli.ShowSubcommandHelp(c); err != nil {
fmt.Printf("error showing subcommand help: %v\n", err) cprintf(c, "error showing subcommand help: %v\n", err)
} }
return nil return nil
} }
@@ -171,15 +172,19 @@ func runServ(ctx context.Context, c *cli.Command) error {
if err != nil { if err != nil {
return fail(ctx, "Key check failed", "Failed to check provided key: %v", err) return fail(ctx, "Key check failed", "Failed to check provided key: %v", err)
} }
var authSuccessMsg string
switch key.Type { switch key.Type {
case asymkey_model.KeyTypeDeploy: case asymkey_model.KeyTypeDeploy:
println("Hi there! You've successfully authenticated with the deploy key named " + key.Name + ", but Gitea does not provide shell access.") authSuccessMsg = "Hi there! You've successfully authenticated with an SSH deploy key."
case asymkey_model.KeyTypePrincipal: case asymkey_model.KeyTypePrincipal:
println("Hi there! You've successfully authenticated with the principal " + key.Content + ", but Gitea does not provide shell access.") authSuccessMsg = "Hi there! You've successfully authenticated with the SSH principal " + key.Content + "."
default: default:
println("Hi there, " + user.Name + "! You've successfully authenticated with the key named " + key.Name + ", but Gitea does not provide shell access.") authSuccessMsg = "Hi there, " + user.Name + "! You've successfully authenticated with the SSH key named " + key.Name + "."
} }
println("If this is unexpected, please log in with password and setup Gitea under another user.") _, _ = fmt.Fprintf(c.ErrWriter, "%s\n%s",
authSuccessMsg,
"Gitea does not provide shell access. If this is unexpected, please setup Gitea under another SSH user or use container to deploy.",
)
return nil return nil
} else if c.Bool("debug") { } else if c.Bool("debug") {
log.Debug("SSH_ORIGINAL_COMMAND: %s", os.Getenv("SSH_ORIGINAL_COMMAND")) log.Debug("SSH_ORIGINAL_COMMAND: %s", os.Getenv("SSH_ORIGINAL_COMMAND"))
@@ -194,7 +199,7 @@ func runServ(ctx context.Context, c *cli.Command) error {
if git.DefaultFeatures().SupportProcReceive { if git.DefaultFeatures().SupportProcReceive {
// for AGit Flow // for AGit Flow
if cmd == "ssh_info" { if cmd == "ssh_info" {
fmt.Print(`{"type":"agit","version":1}`) cprintf(c, "%s", agit.SshInfoJson)
return nil return nil
} }
} }
+2 -1
View File
@@ -17,6 +17,7 @@ import (
"gitea.dev/modules/setting" "gitea.dev/modules/setting"
"gitea.dev/modules/util" "gitea.dev/modules/util"
"gitea.dev/modules/web/middleware" "gitea.dev/modules/web/middleware"
"gitea.dev/services/agit"
"gitea.dev/services/context" "gitea.dev/services/context"
) )
@@ -49,7 +50,7 @@ func SSHInfo(rw http.ResponseWriter, req *http.Request) {
return return
} }
rw.Header().Set("content-type", "text/json;charset=UTF-8") rw.Header().Set("content-type", "text/json;charset=UTF-8")
_, err := rw.Write([]byte(`{"type":"agit","version":1}`)) _, err := rw.Write([]byte(agit.SshInfoJson))
if err != nil { if err != nil {
log.Error("fail to write result: err: %v", err) log.Error("fail to write result: err: %v", err)
rw.WriteHeader(http.StatusInternalServerError) rw.WriteHeader(http.StatusInternalServerError)
+2
View File
@@ -24,6 +24,8 @@ import (
pull_service "gitea.dev/services/pull" pull_service "gitea.dev/services/pull"
) )
const SshInfoJson = `{"type":"agit","version":1}`
func parseAgitPushOptionValue(s string) string { func parseAgitPushOptionValue(s string) string {
if base64Value, ok := strings.CutPrefix(s, "{base64}"); ok { if base64Value, ok := strings.CutPrefix(s, "{base64}"); ok {
decoded, err := base64.StdEncoding.DecodeString(base64Value) decoded, err := base64.StdEncoding.DecodeString(base64Value)
+2 -2
View File
@@ -72,10 +72,10 @@ func TestSSHShellWelcome(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
var stderr bytes.Buffer var stderr bytes.Buffer
session.Stderr = &stderr // "gitea serv" writes the welcome with println, which goes to stderr session.Stderr = &stderr
require.NoError(t, session.Shell()) require.NoError(t, session.Shell())
require.NoError(t, session.Wait()) // fails unless the server reports exit status 0 require.NoError(t, session.Wait()) // fails unless the server reports exit status 0
assert.Contains(t, stderr.String(), "You've successfully authenticated with the key named welcome-key") assert.Contains(t, stderr.String(), "You've successfully authenticated with the SSH key named welcome-key.")
}) })
}) })
} }