Skip to content

Commit 99d6972

Browse files
chavacavaGusted
chavacava
authored and
Gusted
committed
chore(cleanup): replaces unnecessary calls to formatting functions by non-formatting equivalents (go-gitea#7994)
This PR replaces unnecessary calls to formatting functions (`fmt.Printf`, `fmt.Errorf`, ...) by non-formatting equivalents. Resolves go-gitea#7967 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7994 Reviewed-by: Gusted <[email protected]> Co-authored-by: chavacava <[email protected]> Co-committed-by: chavacava <[email protected]>
1 parent 25f3f8e commit 99d6972

File tree

126 files changed

+340
-281
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+340
-281
lines changed

cmd/admin_auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func runListAuth(c *cli.Context) error {
8181

8282
// loop through each source and print
8383
w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags)
84-
fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
84+
fmt.Fprint(w, "ID\tName\tType\tEnabled\n")
8585
for _, source := range authSources {
8686
fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, source.Type.String(), source.IsActive)
8787
}

cmd/admin_user_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func runCreateUser(c *cli.Context) error {
108108
username = c.String("username")
109109
} else {
110110
username = c.String("name")
111-
_, _ = fmt.Fprintf(c.App.ErrWriter, "--name flag is deprecated. Use --username instead.\n")
111+
_, _ = fmt.Fprint(c.App.ErrWriter, "--name flag is deprecated. Use --username instead.\n")
112112
}
113113

114114
ctx, cancel := installSignals()

cmd/admin_user_list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ func runListUsers(c *cli.Context) error {
4141
w := tabwriter.NewWriter(os.Stdout, 5, 0, 1, ' ', 0)
4242

4343
if c.IsSet("admin") {
44-
fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\n")
44+
fmt.Fprint(w, "ID\tUsername\tEmail\tIsActive\n")
4545
for _, u := range users {
4646
if u.IsAdmin {
4747
fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", u.ID, u.Name, u.Email, u.IsActive)
4848
}
4949
}
5050
} else {
5151
twofa := user_model.UserList(users).GetTwoFaStatus(ctx)
52-
fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\tIsAdmin\t2FA\n")
52+
fmt.Fprint(w, "ID\tUsername\tEmail\tIsActive\tIsAdmin\t2FA\n")
5353
for _, u := range users {
5454
fmt.Fprintf(w, "%d\t%s\t%s\t%t\t%t\t%t\n", u.ID, u.Name, u.Email, u.IsActive, u.IsAdmin, twofa[u.ID])
5555
}

cmd/dump.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package cmd
66

77
import (
8+
"errors"
89
"fmt"
910
"io"
1011
"os"
@@ -212,13 +213,13 @@ func runDump(ctx *cli.Context) error {
212213

213214
if !setting.InstallLock {
214215
log.Error("Is '%s' really the right config path?\n", setting.CustomConf)
215-
return fmt.Errorf("forgejo is not initialized")
216+
return errors.New("forgejo is not initialized")
216217
}
217218
setting.LoadSettings() // cannot access session settings otherwise
218219

219220
verbose := ctx.Bool("verbose")
220221
if verbose && ctx.Bool("quiet") {
221-
return fmt.Errorf("--quiet and --verbose cannot both be set")
222+
return errors.New("--quiet and --verbose cannot both be set")
222223
}
223224

224225
stdCtx, cancel := installSignals()

cmd/forgejo/actions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package forgejo
66
import (
77
"context"
88
"encoding/hex"
9+
"errors"
910
"fmt"
1011
"io"
1112
"os"
@@ -124,7 +125,7 @@ func readSecret(ctx context.Context, cliCtx *cli.Context) (string, error) {
124125
}
125126
return string(buf), nil
126127
}
127-
return "", fmt.Errorf("at least one of the --secret, --secret-stdin, --secret-file options is required")
128+
return "", errors.New("at least one of the --secret, --secret-stdin, --secret-file options is required")
128129
}
129130

130131
func validateSecret(secret string) error {
@@ -144,7 +145,7 @@ func getLabels(cliCtx *cli.Context) (*[]string, error) {
144145
return &lblValue, nil
145146
}
146147
if cliCtx.String("labels") != "" {
147-
return nil, fmt.Errorf("--labels and --keep-labels should not be used together")
148+
return nil, errors.New("--labels and --keep-labels should not be used together")
148149
}
149150
return nil, nil
150151
}

cmd/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func runGenerateInternalToken(c *cli.Context) error {
6363
fmt.Printf("%s", internalToken)
6464

6565
if isatty.IsTerminal(os.Stdout.Fd()) {
66-
fmt.Printf("\n")
66+
fmt.Println()
6767
}
6868

6969
return nil
@@ -75,7 +75,7 @@ func runGenerateLfsJwtSecret(c *cli.Context) error {
7575
fmt.Printf("%s", jwtSecretBase64)
7676

7777
if isatty.IsTerminal(os.Stdout.Fd()) {
78-
fmt.Printf("\n")
78+
fmt.Print("\n")
7979
}
8080

8181
return nil
@@ -90,7 +90,7 @@ func runGenerateSecretKey(c *cli.Context) error {
9090
fmt.Printf("%s", secretKey)
9191

9292
if isatty.IsTerminal(os.Stdout.Fd()) {
93-
fmt.Printf("\n")
93+
fmt.Print("\n")
9494
}
9595

9696
return nil

cmd/hook.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ Forgejo or set your environment appropriately.`, "")
247247
newCommitIDs[count] = newCommitID
248248
refFullNames[count] = refFullName
249249
count++
250-
fmt.Fprintf(out, "*")
250+
fmt.Fprint(out, "*")
251251

252252
if count >= hookBatchSize {
253253
fmt.Fprintf(out, " Checking %d references\n", count)
@@ -263,10 +263,10 @@ Forgejo or set your environment appropriately.`, "")
263263
lastline = 0
264264
}
265265
} else {
266-
fmt.Fprintf(out, ".")
266+
fmt.Fprint(out, ".")
267267
}
268268
if lastline >= hookBatchSize {
269-
fmt.Fprintf(out, "\n")
269+
fmt.Fprint(out, "\n")
270270
lastline = 0
271271
}
272272
}
@@ -283,7 +283,7 @@ Forgejo or set your environment appropriately.`, "")
283283
return fail(ctx, extra.UserMsg, "HookPreReceive(last) failed: %v", extra.Error)
284284
}
285285
} else if lastline > 0 {
286-
fmt.Fprintf(out, "\n")
286+
fmt.Fprint(out, "\n")
287287
}
288288

289289
fmt.Fprintf(out, "Checked %d references in total\n", total)
@@ -399,7 +399,7 @@ Forgejo or set your environment appropriately.`, "")
399399
continue
400400
}
401401

402-
fmt.Fprintf(out, ".")
402+
fmt.Fprint(out, ".")
403403
oldCommitIDs[count] = string(fields[0])
404404
newCommitIDs[count] = string(fields[1])
405405
refFullNames[count] = git.RefName(fields[2])

cmd/main_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cmd
55

66
import (
7+
"errors"
78
"fmt"
89
"io"
910
"path/filepath"
@@ -128,7 +129,7 @@ func TestCliCmd(t *testing.T) {
128129
}
129130

130131
func TestCliCmdError(t *testing.T) {
131-
app := newTestApp(func(ctx *cli.Context) error { return fmt.Errorf("normal error") })
132+
app := newTestApp(func(ctx *cli.Context) error { return errors.New("normal error") })
132133
r, err := runTestApp(app, "./gitea", "test-cmd")
133134
require.Error(t, err)
134135
assert.Equal(t, 1, r.ExitCode)

models/actions/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package actions
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"slices"
1011
"strings"
@@ -355,7 +356,7 @@ func UpdateRunWithoutNotification(ctx context.Context, run *ActionRun, cols ...s
355356
return err
356357
}
357358
if affected == 0 {
358-
return fmt.Errorf("run has changed")
359+
return errors.New("run has changed")
359360
// It's impossible that the run is not found, since Gitea never deletes runs.
360361
}
361362

models/activities/action.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package activities
77

88
import (
99
"context"
10+
"errors"
1011
"fmt"
1112
"net/url"
1213
"path"
@@ -458,7 +459,7 @@ type GetFeedsOptions struct {
458459
// GetFeeds returns actions according to the provided options
459460
func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, error) {
460461
if opts.RequestedUser == nil && opts.RequestedTeam == nil && opts.RequestedRepo == nil {
461-
return nil, 0, fmt.Errorf("need at least one of these filters: RequestedUser, RequestedTeam, RequestedRepo")
462+
return nil, 0, errors.New("need at least one of these filters: RequestedUser, RequestedTeam, RequestedRepo")
462463
}
463464

464465
cond, err := activityQueryCondition(ctx, opts)

models/asymkey/gpg_key.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package asymkey
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"strings"
1011
"time"
@@ -209,7 +210,7 @@ func parseGPGKey(ctx context.Context, ownerID int64, e *openpgp.Entity, verified
209210
// deleteGPGKey does the actual key deletion
210211
func deleteGPGKey(ctx context.Context, keyID string) (int64, error) {
211212
if keyID == "" {
212-
return 0, fmt.Errorf("empty KeyId forbidden") // Should never happen but just to be sure
213+
return 0, errors.New("empty KeyId forbidden") // Should never happen but just to be sure
213214
}
214215
// Delete imported key
215216
n, err := db.GetEngine(ctx).Where("key_id=?", keyID).Delete(new(GPGKeyImport))

models/asymkey/gpg_key_common.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"crypto"
99
"encoding/base64"
10+
"errors"
1011
"fmt"
1112
"hash"
1213
"io"
@@ -75,7 +76,7 @@ func base64DecPubKey(content string) (*packet.PublicKey, error) {
7576
// Check type
7677
pkey, ok := p.(*packet.PublicKey)
7778
if !ok {
78-
return nil, fmt.Errorf("key is not a public key")
79+
return nil, errors.New("key is not a public key")
7980
}
8081
return pkey, nil
8182
}
@@ -122,15 +123,15 @@ func readArmoredSign(r io.Reader) (body io.Reader, err error) {
122123
func extractSignature(s string) (*packet.Signature, error) {
123124
r, err := readArmoredSign(strings.NewReader(s))
124125
if err != nil {
125-
return nil, fmt.Errorf("Failed to read signature armor")
126+
return nil, errors.New("Failed to read signature armor")
126127
}
127128
p, err := packet.Read(r)
128129
if err != nil {
129-
return nil, fmt.Errorf("Failed to read signature packet")
130+
return nil, errors.New("Failed to read signature packet")
130131
}
131132
sig, ok := p.(*packet.Signature)
132133
if !ok {
133-
return nil, fmt.Errorf("Packet is not a signature")
134+
return nil, errors.New("Packet is not a signature")
134135
}
135136
return sig, nil
136137
}

models/asymkey/gpg_key_object_verification.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package asymkey
66

77
import (
88
"context"
9+
"errors"
910
"fmt"
1011
"hash"
1112
"strings"
@@ -316,7 +317,7 @@ func verifyWithGPGSettings(ctx context.Context, gpgSettings *git.GPGSettings, si
316317
func verifySign(s *packet.Signature, h hash.Hash, k *GPGKey) error {
317318
// Check if key can sign
318319
if !k.CanSign {
319-
return fmt.Errorf("key can not sign")
320+
return errors.New("key can not sign")
320321
}
321322
// Decode key
322323
pkey, err := base64DecPubKey(k.Content)

models/asymkey/ssh_key_parse.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/base64"
1111
"encoding/binary"
1212
"encoding/pem"
13+
"errors"
1314
"fmt"
1415
"math/big"
1516
"os"
@@ -93,7 +94,7 @@ func parseKeyString(content string) (string, error) {
9394

9495
block, _ := pem.Decode([]byte(content))
9596
if block == nil {
96-
return "", fmt.Errorf("failed to parse PEM block containing the public key")
97+
return "", errors.New("failed to parse PEM block containing the public key")
9798
}
9899
if strings.Contains(block.Type, "PRIVATE") {
99100
return "", ErrKeyIsPrivate

models/forgefed/federationhost_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func Test_FederationHostValidation(t *testing.T) {
3535
HostSchema: "https",
3636
}
3737
if res, _ := validation.IsValid(sut); res {
38-
t.Errorf("sut should be invalid: HostFqdn empty")
38+
t.Error("sut should be invalid: HostFqdn empty")
3939
}
4040

4141
sut = FederationHost{
@@ -48,7 +48,7 @@ func Test_FederationHostValidation(t *testing.T) {
4848
HostSchema: "https",
4949
}
5050
if res, _ := validation.IsValid(sut); res {
51-
t.Errorf("sut should be invalid: HostFqdn too long (len=256)")
51+
t.Error("sut should be invalid: HostFqdn too long (len=256)")
5252
}
5353

5454
sut = FederationHost{
@@ -59,7 +59,7 @@ func Test_FederationHostValidation(t *testing.T) {
5959
HostSchema: "https",
6060
}
6161
if res, _ := validation.IsValid(sut); res {
62-
t.Errorf("sut should be invalid: NodeInfo invalid")
62+
t.Error("sut should be invalid: NodeInfo invalid")
6363
}
6464

6565
sut = FederationHost{
@@ -72,7 +72,7 @@ func Test_FederationHostValidation(t *testing.T) {
7272
HostSchema: "https",
7373
}
7474
if res, _ := validation.IsValid(sut); res {
75-
t.Errorf("sut should be invalid: Future timestamp")
75+
t.Error("sut should be invalid: Future timestamp")
7676
}
7777

7878
sut = FederationHost{
@@ -85,6 +85,6 @@ func Test_FederationHostValidation(t *testing.T) {
8585
HostSchema: "https",
8686
}
8787
if res, _ := validation.IsValid(sut); res {
88-
t.Errorf("sut should be invalid: HostFqdn lower case")
88+
t.Error("sut should be invalid: HostFqdn lower case")
8989
}
9090
}

models/forgefed/nodeinfo_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package forgefed
55

66
import (
7-
"fmt"
7+
"errors"
88
"reflect"
99
"strings"
1010
"testing"
@@ -28,7 +28,7 @@ func Test_NodeInfoWellKnownUnmarshalJSON(t *testing.T) {
2828
},
2929
"empty": {
3030
item: []byte(``),
31-
wantErr: fmt.Errorf("cannot parse JSON: cannot parse empty string; unparsed tail: \"\""),
31+
wantErr: errors.New("cannot parse JSON: cannot parse empty string; unparsed tail: \"\""),
3232
},
3333
}
3434

@@ -74,7 +74,7 @@ func Test_NewNodeInfoWellKnown(t *testing.T) {
7474

7575
_, err := NewNodeInfoWellKnown([]byte(`invalid`))
7676
if err == nil {
77-
t.Errorf("error was expected here")
77+
t.Error("error was expected here")
7878
}
7979
}
8080

@@ -87,6 +87,6 @@ func Test_NewNodeInfo(t *testing.T) {
8787

8888
_, err := NewNodeInfo([]byte(`invalid`))
8989
if err == nil {
90-
t.Errorf("error was expected here")
90+
t.Error("error was expected here")
9191
}
9292
}

models/forgejo_migrations/migrate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package forgejo_migrations //nolint:revive
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"os"
1011

@@ -130,7 +131,7 @@ func EnsureUpToDate(x *xorm.Engine) error {
130131
}
131132

132133
if currentDB < 0 {
133-
return fmt.Errorf("database has not been initialized")
134+
return errors.New("database has not been initialized")
134135
}
135136

136137
expected := ExpectedVersion()

0 commit comments

Comments
 (0)