Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@ linters:
- linters:
- dupl
path: modelmigration/v
- linters:
- forbidigo
path: cmd
- linters:
- dupl
text: (?i)webhook
Expand Down
3 changes: 1 addition & 2 deletions cmd/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package cmd

import (
"context"
"fmt"

"gitea.dev/modules/private"
"gitea.dev/modules/setting"
Expand Down Expand Up @@ -49,6 +48,6 @@ func runGenerateActionsRunnerToken(ctx context.Context, c *cli.Command) error {
if extra.HasError() {
return handleCliResponseExtra(extra)
}
_, _ = fmt.Printf("%s\n", respText.Text)
cprintln(c, respText.Text)
return nil
}
2 changes: 1 addition & 1 deletion cmd/admin_user_change_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions cmd/admin_user_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func runCreateUser(ctx context.Context, c *cli.Command) error {
return err
}
// 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 {
return errors.New("must set either password or random-password flag")
}
Expand Down Expand Up @@ -228,15 +228,15 @@ func runCreateUser(ctx context.Context, c *cli.Command) error {
if err := user_model.CreateUser(ctx, u, &user_model.Meta{}, overwriteDefault); err != nil {
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
if accessTokenScope != "" {
t := &auth_model.AccessToken{Name: accessTokenName, UID: u.ID, Scope: accessTokenScope}
if err := auth_model.NewAccessToken(ctx, t); err != nil {
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
}
2 changes: 1 addition & 1 deletion cmd/admin_user_disable_2fa.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ func runDisableTwoFactor(ctx context.Context, c *cli.Command) error {
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
}
4 changes: 2 additions & 2 deletions cmd/admin_user_generate_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func runGenerateAccessToken(ctx context.Context, c *cli.Command) error {
}

if c.Bool("raw") {
fmt.Printf("%s\n", t.Token)
cprintln(c, t.Token)
} 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
Expand Down
3 changes: 1 addition & 2 deletions cmd/admin_user_must_change_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd
import (
"context"
"errors"
"fmt"

user_model "gitea.dev/models/user"
"gitea.dev/modules/setting"
Expand Down Expand Up @@ -59,6 +58,6 @@ func runMustChangePassword(ctx context.Context, c *cli.Command) error {
}

// 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
}
4 changes: 2 additions & 2 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func runRecreateTable(ctx context.Context, cmd *cli.Command) error {

setting.Database.LogSQL = debug
if err := db.InitEngine(ctx); err != nil {
fmt.Println(err)
fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
cprintln(cmd, err)
cprintln(cmd, "Check if you are using the right config file. You can use a --config directive to specify one.")
return nil
}

Expand Down
7 changes: 3 additions & 4 deletions cmd/doctor_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package cmd

import (
"context"
"fmt"

"gitea.dev/models/db"
"gitea.dev/modules/log"
Expand Down Expand Up @@ -40,15 +39,15 @@ func runDoctorConvert(ctx context.Context, cmd *cli.Command) error {
log.Fatal("Failed to convert database & table: %v", 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():
if err := db.ConvertVarcharToNVarchar(); err != nil {
log.Fatal("Failed to convert database from varchar to nvarchar: %v", 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:
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
Expand Down
16 changes: 8 additions & 8 deletions cmd/embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func runListDo(c *cli.Command) error {
}

for _, a := range matchedAssetFiles {
fmt.Println(a.path)
cprintln(c, a.path)
}

return nil
Expand Down Expand Up @@ -194,7 +194,7 @@ func runExtractDo(c *cli.Command) error {
destdir = c.String("destination")
} else if c.Bool("custom") {
destdir = setting.CustomPath
fmt.Println("Using app.ini at", setting.CustomConf)
cprintln(c, "Using app.ini at", setting.CustomConf)
}

fi, err := os.Stat(destdir)
Expand All @@ -213,13 +213,13 @@ func runExtractDo(c *cli.Command) error {
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")
rename := c.Bool("rename")

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
_, _ = fmt.Fprintf(os.Stderr, "%s: %v\n", a.path, err)
}
Expand All @@ -228,7 +228,7 @@ func runExtractDo(c *cli.Command) error {
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))
dir := filepath.Dir(dest)

Expand All @@ -249,7 +249,7 @@ func extractAsset(d string, a assetFile, overwrite, rename bool) error {
return fmt.Errorf("%s: %w", dest, err)
}
} else if !overwrite && !rename {
fmt.Printf("%s already exists; skipped.\n", dest)
cprintf(c, "%s already exists; skipped.\n", dest)
return nil
} else if !fi.Mode().IsRegular() {
return fmt.Errorf("%s already exists, but it's not a regular file", dest)
Expand All @@ -271,7 +271,7 @@ func extractAsset(d string, a assetFile, overwrite, rename bool) error {
return fmt.Errorf("%s: %w", dest, err)
}

fmt.Println(dest)
cprintln(c, dest)

return nil
}
Expand Down Expand Up @@ -306,7 +306,7 @@ func compileCollectPatterns(args []string) (_ []glob.Glob, err error) {
pat := make([]glob.Glob, len(args))
for i := range args {
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
Expand Down
16 changes: 8 additions & 8 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,21 @@ func runGenerateInternalToken(_ context.Context, c *cli.Command) error {
return err
}

fmt.Printf("%s", internalToken)
cprintf(c, "%s", internalToken)

if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Printf("\n")
cprintf(c, "\n")
}

return nil
}

func runGenerateLfsJwtSecret(_ context.Context, c *cli.Command) error {
_, jwtSecretBase64 := generate.NewJwtSecretWithBase64()
fmt.Printf("%s", jwtSecretBase64)
cprintf(c, "%s", jwtSecretBase64)

if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Printf("\n")
cprintf(c, "\n")
}

return nil
Expand All @@ -133,10 +133,10 @@ func runGenerateSecretKey(_ context.Context, c *cli.Command) error {
}

// codeql[disable-next-line=go/clear-text-logging]
fmt.Printf("%s", secretKey)
cprintf(c, "%s", secretKey)

if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Printf("\n")
cprintf(c, "\n")
}

return nil
Expand Down Expand Up @@ -168,14 +168,14 @@ func runGenerateKeyPair(_ context.Context, c *cli.Command) error {
// Check if file exists to prevent overwriting
if _, err := os.Stat(file); err == nil {
if !confirm(c.Reader, c.Writer, "%s already exists.\nOverwrite (y/n)? ", file) {
fmt.Println("Aborting")
cprintln(c, "Aborting")
return nil
}
}
bits := c.Int("bits")
err := ssh.GenKeyPair(file, generate.SSHKeyType(keyType), bits)
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
}
14 changes: 10 additions & 4 deletions cmd/helper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2018 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

// Package cmd provides subcommands to the gitea binary - such as "web" or
// "admin".
package cmd

import (
Expand All @@ -22,8 +20,8 @@ import (
"github.com/urfave/cli/v3"
)

// argsSet checks that all the required arguments are set. args is a list of
// arguments that must be set in the passed Context.
// argsSet checks that all the required arguments are set.
// args is a list of arguments that must be set in the command context.
func argsSet(c *cli.Command, args ...string) error {
for _, a := range args {
if !c.IsSet(a) {
Expand Down Expand Up @@ -141,3 +139,11 @@ func isValidDefaultSubCommand(cmd *cli.Command) (string, bool) {
}
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...)
}
7 changes: 3 additions & 4 deletions cmd/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package cmd

import (
"context"
"fmt"

"gitea.dev/modules/private"
"gitea.dev/modules/setting"
Expand All @@ -22,11 +21,11 @@ func runSendMail(ctx context.Context, c *cli.Command) error {

if !confirmSkipped {
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] ") {
fmt.Println("The mail was not sent")
cprintln(c, "The mail was not sent")
return nil
}
}
Expand All @@ -35,6 +34,6 @@ func runSendMail(ctx context.Context, c *cli.Command) error {
if extra.HasError() {
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
}
19 changes: 12 additions & 7 deletions cmd/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"gitea.dev/modules/process"
repo_module "gitea.dev/modules/repository"
"gitea.dev/modules/setting"
"gitea.dev/services/agit"
"gitea.dev/services/lfs"

"github.com/kballard/go-shellquote"
Expand Down Expand Up @@ -139,13 +140,13 @@ func runServ(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))

if setting.SSH.Disabled {
println("Gitea: SSH has been disabled")
cprintln(c, "Gitea: SSH has been disabled")
return nil
}

if c.NArg() < 1 {
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
}
Expand All @@ -171,15 +172,19 @@ func runServ(ctx context.Context, c *cli.Command) error {
if err != nil {
return fail(ctx, "Key check failed", "Failed to check provided key: %v", err)
}
var authSuccessMsg string
switch key.Type {
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:
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:
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
} else if c.Bool("debug") {
log.Debug("SSH_ORIGINAL_COMMAND: %s", os.Getenv("SSH_ORIGINAL_COMMAND"))
Expand All @@ -194,7 +199,7 @@ func runServ(ctx context.Context, c *cli.Command) error {
if git.DefaultFeatures().SupportProcReceive {
// for AGit Flow
if cmd == "ssh_info" {
fmt.Print(`{"type":"agit","version":1}`)
cprintf(c, "%s", agit.SshInfoJson)
return nil
}
}
Expand Down
3 changes: 2 additions & 1 deletion routers/web/misc/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"gitea.dev/modules/setting"
"gitea.dev/modules/util"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/agit"
"gitea.dev/services/context"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func SSHInfo(rw http.ResponseWriter, req *http.Request) {
return
}
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 {
log.Error("fail to write result: err: %v", err)
rw.WriteHeader(http.StatusInternalServerError)
Expand Down
Loading
Loading