Skip to content

Commit

Permalink
format with gofumpt (#18184)
Browse files Browse the repository at this point in the history
* gofumpt -w -l .

* gofumpt -w -l -extra .

* Add linter

* manual fix

* change make fmt
  • Loading branch information
6543 committed Jan 20, 2022
1 parent 1d98d20 commit 54e9ee3
Show file tree
Hide file tree
Showing 423 changed files with 1,586 additions and 1,759 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linters:
- bidichk
- ineffassign
- revive
- gofumpt
enable-all: false
disable-all: true
fast: false
Expand Down Expand Up @@ -57,6 +58,9 @@ linters-settings:
- name: errorf
- name: duplicated-imports
- name: modifies-value-receiver
gofumpt:
extra-rules: true
lang-version: 1.16

issues:
exclude-rules:
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ clean:

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

.PHONY: vet
vet:
Expand Down
2 changes: 1 addition & 1 deletion build/code-batch-process.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (fc *fileCollector) collectFiles() (res [][]string, err error) {
}

// substArgFiles expands the {file-list} to a real file list for commands
func substArgFiles(args []string, files []string) []string {
func substArgFiles(args, files []string) []string {
for i, s := range args {
if s == "{file-list}" {
newArgs := append(args[:i], files...)
Expand Down
16 changes: 10 additions & 6 deletions build/codeformat/formatimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ var importPackageGroupOrders = map[string]int{

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

var importBlockBegin = []byte("\nimport (\n")
var importBlockEnd = []byte("\n)")
var (
importBlockBegin = []byte("\nimport (\n")
importBlockEnd = []byte("\n)")
)

type importLineParsed struct {
group string
Expand Down Expand Up @@ -59,8 +61,10 @@ func parseImportLine(line string) (*importLineParsed, error) {
return il, nil
}

type importLineGroup []*importLineParsed
type importLineGroupMap map[string]importLineGroup
type (
importLineGroup []*importLineParsed
importLineGroupMap map[string]importLineGroup
)

func formatGoImports(contentBytes []byte) ([]byte, error) {
p1 := bytes.Index(contentBytes, importBlockBegin)
Expand Down Expand Up @@ -153,7 +157,7 @@ func formatGoImports(contentBytes []byte) ([]byte, error) {
return formattedBytes, nil
}

//FormatGoImports format the imports by our rules (see unit tests)
// FormatGoImports format the imports by our rules (see unit tests)
func FormatGoImports(file string) error {
f, err := os.Open(file)
if err != nil {
Expand All @@ -177,7 +181,7 @@ func FormatGoImports(file string) error {
if bytes.Equal(contentBytes, formattedBytes) {
return nil
}
f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0644)
f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions build/generate-bindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/shurcooL/vfsgen"
)

func needsUpdate(dir string, filename string) (bool, []byte) {
func needsUpdate(dir, filename string) (bool, []byte) {
needRegen := false
_, err := os.Stat(filename)
if err != nil {
Expand Down Expand Up @@ -50,7 +50,6 @@ func needsUpdate(dir string, filename string) (bool, []byte) {
newHash := hasher.Sum([]byte{})

if bytes.Compare(oldHash, newHash) != 0 {

return true, newHash
}

Expand Down Expand Up @@ -87,5 +86,5 @@ func main() {
if err != nil {
log.Fatalf("%v\n", err)
}
_ = os.WriteFile(filename+".hash", newHash, 0666)
_ = os.WriteFile(filename+".hash", newHash, 0o666)
}
12 changes: 5 additions & 7 deletions build/generate-emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ const (
maxUnicodeVersion = 12
)

var (
flagOut = flag.String("o", "modules/emoji/emoji_data.go", "out")
)
var flagOut = flag.String("o", "modules/emoji/emoji_data.go", "out")

// Gemoji is a set of emoji data.
type Gemoji []Emoji
Expand Down Expand Up @@ -68,7 +66,7 @@ func main() {
}

// write
err = os.WriteFile(*flagOut, buf, 0644)
err = os.WriteFile(*flagOut, buf, 0o644)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -109,7 +107,7 @@ func generate() ([]byte, error) {
return nil, err
}

var skinTones = make(map[string]string)
skinTones := make(map[string]string)

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

var tmp Gemoji

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

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

// Add skin tones to emoji that support it
var (
Expand Down
5 changes: 1 addition & 4 deletions build/generate-gitignores.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func main() {
flag.Parse()

file, err := os.CreateTemp(os.TempDir(), prefix)

if err != nil {
log.Fatalf("Failed to create temp file. %s", err)
}
Expand Down Expand Up @@ -65,7 +64,6 @@ func main() {
}

gz, err := gzip.NewReader(file)

if err != nil {
log.Fatalf("Failed to gunzip the archive. %s", err)
}
Expand Down Expand Up @@ -96,7 +94,6 @@ func main() {
}

out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".gitignore")))

if err != nil {
log.Fatalf("Failed to create new file. %s", err)
}
Expand All @@ -119,7 +116,7 @@ func main() {
}
// Write data to dst
dst = path.Join(destination, dst)
err = os.WriteFile(dst, data, 0644)
err = os.WriteFile(dst, data, 0o644)
if err != nil {
log.Fatalf("Failed to write new file. %s", err)
}
Expand Down
3 changes: 0 additions & 3 deletions build/generate-licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func main() {
flag.Parse()

file, err := os.CreateTemp(os.TempDir(), prefix)

if err != nil {
log.Fatalf("Failed to create temp file. %s", err)
}
Expand Down Expand Up @@ -66,7 +65,6 @@ func main() {
}

gz, err := gzip.NewReader(file)

if err != nil {
log.Fatalf("Failed to gunzip the archive. %s", err)
}
Expand Down Expand Up @@ -100,7 +98,6 @@ func main() {
continue
}
out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".txt")))

if err != nil {
log.Fatalf("Failed to create new file. %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion build/gocovmerge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"golang.org/x/tools/cover"
)

func mergeProfiles(p *cover.Profile, merge *cover.Profile) {
func mergeProfiles(p, merge *cover.Profile) {
if p.Mode != merge.Mode {
log.Fatalf("cannot merge profiles with different modes")
}
Expand Down
10 changes: 3 additions & 7 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func runCreateUser(c *cli.Context) error {
}

// always default to true
var changePassword = true
changePassword := true

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

users, err := user_model.GetAllUsers()

if err != nil {
return err
}
Expand All @@ -601,7 +600,6 @@ func runListUsers(c *cli.Context) error {

w.Flush()
return nil

}

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

if c.IsSet("required-claim-name") {
oAuth2Config.RequiredClaimName = c.String("required-claim-name")

}
if c.IsSet("required-claim-value") {
oAuth2Config.RequiredClaimValue = c.String("required-claim-value")
Expand All @@ -843,7 +840,7 @@ func runUpdateOauth(c *cli.Context) error {
}

// update custom URL mapping
var customURLMapping = &oauth2.CustomURLMapping{}
customURLMapping := &oauth2.CustomURLMapping{}

if oAuth2Config.CustomURLMapping != nil {
customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL
Expand Down Expand Up @@ -926,7 +923,7 @@ func runAddSMTP(c *cli.Context) error {
if !c.IsSet("port") {
return errors.New("port must be set")
}
var active = true
active := true
if c.IsSet("active") {
active = c.BoolT("active")
}
Expand Down Expand Up @@ -994,7 +991,6 @@ func runListAuth(c *cli.Context) error {
}

authSources, err := auth.Sources()

if err != nil {
return err
}
Expand Down
17 changes: 8 additions & 9 deletions cmd/admin_auth_ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (

func TestAddLdapBindDn(t *testing.T) {
// Mock cli functions to do not exit on error
var osExiter = cli.OsExiter
osExiter := cli.OsExiter
defer func() { cli.OsExiter = osExiter }()
cli.OsExiter = func(code int) {}

// Test cases
var cases = []struct {
cases := []struct {
args []string
source *auth.Source
errMsg string
Expand Down Expand Up @@ -243,12 +243,12 @@ func TestAddLdapBindDn(t *testing.T) {

func TestAddLdapSimpleAuth(t *testing.T) {
// Mock cli functions to do not exit on error
var osExiter = cli.OsExiter
osExiter := cli.OsExiter
defer func() { cli.OsExiter = osExiter }()
cli.OsExiter = func(code int) {}

// Test cases
var cases = []struct {
cases := []struct {
args []string
authSource *auth.Source
errMsg string
Expand Down Expand Up @@ -474,12 +474,12 @@ func TestAddLdapSimpleAuth(t *testing.T) {

func TestUpdateLdapBindDn(t *testing.T) {
// Mock cli functions to do not exit on error
var osExiter = cli.OsExiter
osExiter := cli.OsExiter
defer func() { cli.OsExiter = osExiter }()
cli.OsExiter = func(code int) {}

// Test cases
var cases = []struct {
cases := []struct {
args []string
id int64
existingAuthSource *auth.Source
Expand Down Expand Up @@ -907,12 +907,12 @@ func TestUpdateLdapBindDn(t *testing.T) {

func TestUpdateLdapSimpleAuth(t *testing.T) {
// Mock cli functions to do not exit on error
var osExiter = cli.OsExiter
osExiter := cli.OsExiter
defer func() { cli.OsExiter = osExiter }()
cli.OsExiter = func(code int) {}

// Test cases
var cases = []struct {
cases := []struct {
args []string
id int64
existingAuthSource *auth.Source
Expand Down Expand Up @@ -1161,7 +1161,6 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
authSource: &auth.Source{
Type: auth.DLDAP,
Cfg: &ldap.Source{

AttributeMail: "mail",
},
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func runCert(c *cli.Context) error {
}
log.Println("Written cert.pem")

keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
log.Fatalf("Failed to open key.pem for writing: %v", err)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func runRecreateTable(ctx *cli.Context) error {
}
return recreateTables(x)
})

}

func runDoctor(ctx *cli.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func runDump(ctx *cli.Context) error {
fatal("Failed to save %s: %v", fileName, err)
}

if err := os.Chmod(fileName, 0600); err != nil {
if err := os.Chmod(fileName, 0o600); err != nil {
log.Info("Can't change file access permissions mask to 0600: %v", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dump_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func runDumpRepository(ctx *cli.Context) error {
}
serviceType = convert.ToGitServiceType(serviceStr)

var opts = base.MigrateOptions{
opts := base.MigrateOptions{
GitServiceType: serviceType,
CloneAddr: cloneAddr,
AuthUsername: ctx.String("auth_username"),
Expand Down
Loading

0 comments on commit 54e9ee3

Please sign in to comment.