From fdb83e6fd19ba1becd8cf8a2d3c2305a611f0eb1 Mon Sep 17 00:00:00 2001 From: zhuyasen Date: Sun, 1 Dec 2024 15:13:22 +0800 Subject: [PATCH] style: adjustment code --- .gitignore | 1 - .golangci.yml | 6 +++--- LICENSE | 2 +- cmd/sponge/commands/merge/common.go | 5 +++-- pkg/container/group/group.go | 12 ++++++------ pkg/sql2code/sql2code.go | 2 +- pkg/utils/wait_print.go | 4 +++- 7 files changed, 17 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 89b1bbc..4d2ac99 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ *.exe~ *.dll *.so -*.dylib *.log # Test binary, built with `go test -c` diff --git a/.golangci.yml b/.golangci.yml index b2d85a3..c8dc08f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -56,7 +56,7 @@ linters: - unconvert - whitespace - staticcheck - #- bodyclose + - bodyclose #- dupl #- goprintffuncname #- gosec @@ -67,6 +67,8 @@ linters: linters-settings: revive: rules: + - name: indent-error-flow + - name: unused-parameter - name: argument-limit arguments: [ 8 ] - name: atomic @@ -96,7 +98,6 @@ linters-settings: - name: if-return - name: import-shadowing - name: increment-decrement - - name: indent-error-flow - name: modifies-parameter - name: modifies-value-receiver - name: package-comments @@ -113,7 +114,6 @@ linters-settings: - name: unexported-naming - name: unnecessary-stmt - name: unreachable-code - - name: unused-parameter - name: var-declaration - name: var-naming - name: waitgroup-by-value diff --git a/LICENSE b/LICENSE index fabcd8f..3bb7867 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 zhuyasen +Copyright (c) 2022 zhufuyi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/cmd/sponge/commands/merge/common.go b/cmd/sponge/commands/merge/common.go index 12d16bb..4c50674 100644 --- a/cmd/sponge/commands/merge/common.go +++ b/cmd/sponge/commands/merge/common.go @@ -6,6 +6,7 @@ package merge import ( "bufio" "bytes" + "errors" "fmt" "os" "path" @@ -112,7 +113,7 @@ func (m *mergeParam) runMergeCode(file string) (string, error) { count1 := bytes.Count(data1, m.splitLineMark) count2 := bytes.Count(data2, m.splitLineMark) if count1 != count2 { - return "", fmt.Errorf(color.RedString("merge code failed (%s --> %s), manually merge code"+ + return "", errors.New(color.RedString("merge code failed (%s --> %s), manually merge code"+ " reference document https://github.com/zhufuyi/sponge/tree/main/cmd/sponge/commands/merge", cutPathPrefix(file), getTargetFilename(file))) } @@ -135,7 +136,7 @@ func (m *mergeParam) runMergeCode(file string) (string, error) { } if len(data1) > len(data) { - return "", fmt.Errorf(color.RedString("merge code failed (%s --> %s), to avoid replacing logical code, "+ + return "", errors.New(color.RedString("merge code failed (%s --> %s), to avoid replacing logical code, "+ "manually merge code reference document https://github.com/zhufuyi/sponge/tree/main/cmd/sponge/commands/merge", cutPathPrefix(file), getTargetFilename(file))) } diff --git a/pkg/container/group/group.go b/pkg/container/group/group.go index c97496d..7cfcf55 100644 --- a/pkg/container/group/group.go +++ b/pkg/container/group/group.go @@ -13,12 +13,12 @@ type Group struct { } // NewGroup news a group container. -func NewGroup(new func() interface{}) *Group { - if new == nil { +func NewGroup(fn func() interface{}) *Group { + if fn == nil { panic("container.group: can't assign a nil to the new function") } return &Group{ - new: new, + new: fn, vals: make(map[string]interface{}), } } @@ -46,12 +46,12 @@ func (g *Group) Get(key string) interface{} { } // Reset resets the new function and deletes all existing objects. -func (g *Group) Reset(new func() interface{}) { - if new == nil { +func (g *Group) Reset(fn func() interface{}) { + if fn == nil { panic("container.group: can't assign a nil to the new function") } g.Lock() - g.new = new + g.new = fn g.Unlock() g.Clear() } diff --git a/pkg/sql2code/sql2code.go b/pkg/sql2code/sql2code.go index ba658d5..0194621 100644 --- a/pkg/sql2code/sql2code.go +++ b/pkg/sql2code/sql2code.go @@ -116,7 +116,7 @@ func getSQL(args *Args) (string, map[string]string, error) { sqlStr, mongoTypeMap := parser.ConvertToSQLByMgoFields(args.DBTable, fields) return sqlStr, mongoTypeMap, nil default: - return "", nil, fmt.Errorf("getsql error, unsupported database driver: " + dbDriverName) + return "", nil, errors.New("get sql error, unsupported database driver: " + dbDriverName) } } diff --git a/pkg/utils/wait_print.go b/pkg/utils/wait_print.go index 0267793..4722e4d 100644 --- a/pkg/utils/wait_print.go +++ b/pkg/utils/wait_print.go @@ -63,7 +63,9 @@ func (p *WaitPrinter) StopPrint(tip string) { } defer func() { - recover() + if e := recover(); e != nil { + fmt.Println(e) + } }() p.cancel()