Skip to content

Commit 54e9ee3

Browse files
authored
format with gofumpt (#18184)
* gofumpt -w -l . * gofumpt -w -l -extra . * Add linter * manual fix * change make fmt
1 parent 1d98d20 commit 54e9ee3

File tree

423 files changed

+1586
-1759
lines changed

Some content is hidden

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

423 files changed

+1586
-1759
lines changed

.golangci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ linters:
1717
- bidichk
1818
- ineffassign
1919
- revive
20+
- gofumpt
2021
enable-all: false
2122
disable-all: true
2223
fast: false
@@ -57,6 +58,9 @@ linters-settings:
5758
- name: errorf
5859
- name: duplicated-imports
5960
- name: modifies-value-receiver
61+
gofumpt:
62+
extra-rules: true
63+
lang-version: 1.16
6064

6165
issues:
6266
exclude-rules:

Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,10 @@ clean:
231231

232232
.PHONY: fmt
233233
fmt:
234-
@echo "Running gitea-fmt(with gofmt)..."
235-
@$(GO) run build/code-batch-process.go gitea-fmt -s -w '{file-list}'
234+
@hash xgogofumpt > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
235+
$(GO) install mvdan.cc/gofumpt@latest; \
236+
fi
237+
gofumpt -w -l -extra -lang 1.16 .
236238

237239
.PHONY: vet
238240
vet:

build/code-batch-process.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (fc *fileCollector) collectFiles() (res [][]string, err error) {
136136
}
137137

138138
// substArgFiles expands the {file-list} to a real file list for commands
139-
func substArgFiles(args []string, files []string) []string {
139+
func substArgFiles(args, files []string) []string {
140140
for i, s := range args {
141141
if s == "{file-list}" {
142142
newArgs := append(args[:i], files...)

build/codeformat/formatimports.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ var importPackageGroupOrders = map[string]int{
2020

2121
var errInvalidCommentBetweenImports = errors.New("comments between imported packages are invalid, please move comments to the end of the package line")
2222

23-
var importBlockBegin = []byte("\nimport (\n")
24-
var importBlockEnd = []byte("\n)")
23+
var (
24+
importBlockBegin = []byte("\nimport (\n")
25+
importBlockEnd = []byte("\n)")
26+
)
2527

2628
type importLineParsed struct {
2729
group string
@@ -59,8 +61,10 @@ func parseImportLine(line string) (*importLineParsed, error) {
5961
return il, nil
6062
}
6163

62-
type importLineGroup []*importLineParsed
63-
type importLineGroupMap map[string]importLineGroup
64+
type (
65+
importLineGroup []*importLineParsed
66+
importLineGroupMap map[string]importLineGroup
67+
)
6468

6569
func formatGoImports(contentBytes []byte) ([]byte, error) {
6670
p1 := bytes.Index(contentBytes, importBlockBegin)
@@ -153,7 +157,7 @@ func formatGoImports(contentBytes []byte) ([]byte, error) {
153157
return formattedBytes, nil
154158
}
155159

156-
//FormatGoImports format the imports by our rules (see unit tests)
160+
// FormatGoImports format the imports by our rules (see unit tests)
157161
func FormatGoImports(file string) error {
158162
f, err := os.Open(file)
159163
if err != nil {
@@ -177,7 +181,7 @@ func FormatGoImports(file string) error {
177181
if bytes.Equal(contentBytes, formattedBytes) {
178182
return nil
179183
}
180-
f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0644)
184+
f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0o644)
181185
if err != nil {
182186
return err
183187
}

build/generate-bindata.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/shurcooL/vfsgen"
2121
)
2222

23-
func needsUpdate(dir string, filename string) (bool, []byte) {
23+
func needsUpdate(dir, filename string) (bool, []byte) {
2424
needRegen := false
2525
_, err := os.Stat(filename)
2626
if err != nil {
@@ -50,7 +50,6 @@ func needsUpdate(dir string, filename string) (bool, []byte) {
5050
newHash := hasher.Sum([]byte{})
5151

5252
if bytes.Compare(oldHash, newHash) != 0 {
53-
5453
return true, newHash
5554
}
5655

@@ -87,5 +86,5 @@ func main() {
8786
if err != nil {
8887
log.Fatalf("%v\n", err)
8988
}
90-
_ = os.WriteFile(filename+".hash", newHash, 0666)
89+
_ = os.WriteFile(filename+".hash", newHash, 0o666)
9190
}

build/generate-emoji.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ const (
3030
maxUnicodeVersion = 12
3131
)
3232

33-
var (
34-
flagOut = flag.String("o", "modules/emoji/emoji_data.go", "out")
35-
)
33+
var flagOut = flag.String("o", "modules/emoji/emoji_data.go", "out")
3634

3735
// Gemoji is a set of emoji data.
3836
type Gemoji []Emoji
@@ -68,7 +66,7 @@ func main() {
6866
}
6967

7068
// write
71-
err = os.WriteFile(*flagOut, buf, 0644)
69+
err = os.WriteFile(*flagOut, buf, 0o644)
7270
if err != nil {
7371
log.Fatal(err)
7472
}
@@ -109,7 +107,7 @@ func generate() ([]byte, error) {
109107
return nil, err
110108
}
111109

112-
var skinTones = make(map[string]string)
110+
skinTones := make(map[string]string)
113111

114112
skinTones["\U0001f3fb"] = "Light Skin Tone"
115113
skinTones["\U0001f3fc"] = "Medium-Light Skin Tone"
@@ -119,7 +117,7 @@ func generate() ([]byte, error) {
119117

120118
var tmp Gemoji
121119

122-
//filter out emoji that require greater than max unicode version
120+
// filter out emoji that require greater than max unicode version
123121
for i := range data {
124122
val, _ := strconv.ParseFloat(data[i].UnicodeVersion, 64)
125123
if int(val) <= maxUnicodeVersion {
@@ -158,7 +156,7 @@ func generate() ([]byte, error) {
158156

159157
// write a JSON file to use with tribute (write before adding skin tones since we can't support them there yet)
160158
file, _ := json.Marshal(data)
161-
_ = os.WriteFile("assets/emoji.json", file, 0644)
159+
_ = os.WriteFile("assets/emoji.json", file, 0o644)
162160

163161
// Add skin tones to emoji that support it
164162
var (

build/generate-gitignores.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func main() {
3434
flag.Parse()
3535

3636
file, err := os.CreateTemp(os.TempDir(), prefix)
37-
3837
if err != nil {
3938
log.Fatalf("Failed to create temp file. %s", err)
4039
}
@@ -65,7 +64,6 @@ func main() {
6564
}
6665

6766
gz, err := gzip.NewReader(file)
68-
6967
if err != nil {
7068
log.Fatalf("Failed to gunzip the archive. %s", err)
7169
}
@@ -96,7 +94,6 @@ func main() {
9694
}
9795

9896
out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".gitignore")))
99-
10097
if err != nil {
10198
log.Fatalf("Failed to create new file. %s", err)
10299
}
@@ -119,7 +116,7 @@ func main() {
119116
}
120117
// Write data to dst
121118
dst = path.Join(destination, dst)
122-
err = os.WriteFile(dst, data, 0644)
119+
err = os.WriteFile(dst, data, 0o644)
123120
if err != nil {
124121
log.Fatalf("Failed to write new file. %s", err)
125122
}

build/generate-licenses.go

-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func main() {
3434
flag.Parse()
3535

3636
file, err := os.CreateTemp(os.TempDir(), prefix)
37-
3837
if err != nil {
3938
log.Fatalf("Failed to create temp file. %s", err)
4039
}
@@ -66,7 +65,6 @@ func main() {
6665
}
6766

6867
gz, err := gzip.NewReader(file)
69-
7068
if err != nil {
7169
log.Fatalf("Failed to gunzip the archive. %s", err)
7270
}
@@ -100,7 +98,6 @@ func main() {
10098
continue
10199
}
102100
out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".txt")))
103-
104101
if err != nil {
105102
log.Fatalf("Failed to create new file. %s", err)
106103
}

build/gocovmerge.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"golang.org/x/tools/cover"
2323
)
2424

25-
func mergeProfiles(p *cover.Profile, merge *cover.Profile) {
25+
func mergeProfiles(p, merge *cover.Profile) {
2626
if p.Mode != merge.Mode {
2727
log.Fatalf("cannot merge profiles with different modes")
2828
}

cmd/admin.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ func runCreateUser(c *cli.Context) error {
525525
}
526526

527527
// always default to true
528-
var changePassword = true
528+
changePassword := true
529529

530530
// If this is the first user being created.
531531
// Take it as the admin and don't force a password update.
@@ -577,7 +577,6 @@ func runListUsers(c *cli.Context) error {
577577
}
578578

579579
users, err := user_model.GetAllUsers()
580-
581580
if err != nil {
582581
return err
583582
}
@@ -601,7 +600,6 @@ func runListUsers(c *cli.Context) error {
601600

602601
w.Flush()
603602
return nil
604-
605603
}
606604

607605
func runDeleteUser(c *cli.Context) error {
@@ -826,7 +824,6 @@ func runUpdateOauth(c *cli.Context) error {
826824

827825
if c.IsSet("required-claim-name") {
828826
oAuth2Config.RequiredClaimName = c.String("required-claim-name")
829-
830827
}
831828
if c.IsSet("required-claim-value") {
832829
oAuth2Config.RequiredClaimValue = c.String("required-claim-value")
@@ -843,7 +840,7 @@ func runUpdateOauth(c *cli.Context) error {
843840
}
844841

845842
// update custom URL mapping
846-
var customURLMapping = &oauth2.CustomURLMapping{}
843+
customURLMapping := &oauth2.CustomURLMapping{}
847844

848845
if oAuth2Config.CustomURLMapping != nil {
849846
customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL
@@ -926,7 +923,7 @@ func runAddSMTP(c *cli.Context) error {
926923
if !c.IsSet("port") {
927924
return errors.New("port must be set")
928925
}
929-
var active = true
926+
active := true
930927
if c.IsSet("active") {
931928
active = c.BoolT("active")
932929
}
@@ -994,7 +991,6 @@ func runListAuth(c *cli.Context) error {
994991
}
995992

996993
authSources, err := auth.Sources()
997-
998994
if err != nil {
999995
return err
1000996
}

cmd/admin_auth_ldap_test.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import (
1717

1818
func TestAddLdapBindDn(t *testing.T) {
1919
// Mock cli functions to do not exit on error
20-
var osExiter = cli.OsExiter
20+
osExiter := cli.OsExiter
2121
defer func() { cli.OsExiter = osExiter }()
2222
cli.OsExiter = func(code int) {}
2323

2424
// Test cases
25-
var cases = []struct {
25+
cases := []struct {
2626
args []string
2727
source *auth.Source
2828
errMsg string
@@ -243,12 +243,12 @@ func TestAddLdapBindDn(t *testing.T) {
243243

244244
func TestAddLdapSimpleAuth(t *testing.T) {
245245
// Mock cli functions to do not exit on error
246-
var osExiter = cli.OsExiter
246+
osExiter := cli.OsExiter
247247
defer func() { cli.OsExiter = osExiter }()
248248
cli.OsExiter = func(code int) {}
249249

250250
// Test cases
251-
var cases = []struct {
251+
cases := []struct {
252252
args []string
253253
authSource *auth.Source
254254
errMsg string
@@ -474,12 +474,12 @@ func TestAddLdapSimpleAuth(t *testing.T) {
474474

475475
func TestUpdateLdapBindDn(t *testing.T) {
476476
// Mock cli functions to do not exit on error
477-
var osExiter = cli.OsExiter
477+
osExiter := cli.OsExiter
478478
defer func() { cli.OsExiter = osExiter }()
479479
cli.OsExiter = func(code int) {}
480480

481481
// Test cases
482-
var cases = []struct {
482+
cases := []struct {
483483
args []string
484484
id int64
485485
existingAuthSource *auth.Source
@@ -907,12 +907,12 @@ func TestUpdateLdapBindDn(t *testing.T) {
907907

908908
func TestUpdateLdapSimpleAuth(t *testing.T) {
909909
// Mock cli functions to do not exit on error
910-
var osExiter = cli.OsExiter
910+
osExiter := cli.OsExiter
911911
defer func() { cli.OsExiter = osExiter }()
912912
cli.OsExiter = func(code int) {}
913913

914914
// Test cases
915-
var cases = []struct {
915+
cases := []struct {
916916
args []string
917917
id int64
918918
existingAuthSource *auth.Source
@@ -1161,7 +1161,6 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
11611161
authSource: &auth.Source{
11621162
Type: auth.DLDAP,
11631163
Cfg: &ldap.Source{
1164-
11651164
AttributeMail: "mail",
11661165
},
11671166
},

cmd/cert.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func runCert(c *cli.Context) error {
180180
}
181181
log.Println("Written cert.pem")
182182

183-
keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
183+
keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
184184
if err != nil {
185185
log.Fatalf("Failed to open key.pem for writing: %v", err)
186186
}

cmd/doctor.go

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func runRecreateTable(ctx *cli.Context) error {
121121
}
122122
return recreateTables(x)
123123
})
124-
125124
}
126125

127126
func runDoctor(ctx *cli.Context) error {

cmd/dump.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func runDump(ctx *cli.Context) error {
370370
fatal("Failed to save %s: %v", fileName, err)
371371
}
372372

373-
if err := os.Chmod(fileName, 0600); err != nil {
373+
if err := os.Chmod(fileName, 0o600); err != nil {
374374
log.Info("Can't change file access permissions mask to 0600: %v", err)
375375
}
376376
}

cmd/dump_repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func runDumpRepository(ctx *cli.Context) error {
107107
}
108108
serviceType = convert.ToGitServiceType(serviceStr)
109109

110-
var opts = base.MigrateOptions{
110+
opts := base.MigrateOptions{
111111
GitServiceType: serviceType,
112112
CloneAddr: cloneAddr,
113113
AuthUsername: ctx.String("auth_username"),

0 commit comments

Comments
 (0)