Compare commits

...

8 Commits

22 changed files with 909 additions and 27 deletions

View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -3,6 +3,6 @@ module git.blauwelle.com/go/crate/cmd/http-reflect-server
go 1.20 go 1.20
require ( require (
git.blauwelle.com/go/crate/exegroup v0.3.0 git.blauwelle.com/go/crate/exegroup v0.4.0
git.blauwelle.com/go/crate/log v0.6.0 git.blauwelle.com/go/crate/log v0.9.0
) )

View File

@@ -1,4 +1,4 @@
git.blauwelle.com/go/crate/exegroup v0.3.0 h1:TBLygDztECKc67NeIIBsFDxlA4KcJpbOmafqqRuKRcM= git.blauwelle.com/go/crate/exegroup v0.4.0 h1:hr9vhYDL+LidvoEBCabdUZ22oekUq0s2NK69tklb42g=
git.blauwelle.com/go/crate/exegroup v0.3.0/go.mod h1:DJoID54YI5WFHGHoTCjBao8oS3HFRzwbWMZW6P57AIQ= git.blauwelle.com/go/crate/exegroup v0.4.0/go.mod h1:DJoID54YI5WFHGHoTCjBao8oS3HFRzwbWMZW6P57AIQ=
git.blauwelle.com/go/crate/log v0.6.0 h1:s/TeJUaV/Y8hHaz/3FumdbwQWCbRMmOx8prrNmByJHs= git.blauwelle.com/go/crate/log v0.9.0 h1:H01AQIKcYybeCZGdReBzMoWhkXPQJAoY1t+K0J1asEk=
git.blauwelle.com/go/crate/log v0.6.0/go.mod h1:jfVfpRODZTA70A8IkApVeGsS1zfLk1D77sLWZM/w+L0= git.blauwelle.com/go/crate/log v0.9.0/go.mod h1:jfVfpRODZTA70A8IkApVeGsS1zfLk1D77sLWZM/w+L0=

View File

@@ -12,6 +12,10 @@ import (
"git.blauwelle.com/go/crate/log" "git.blauwelle.com/go/crate/log"
) )
const (
maxParseMemory = 16 * 1024 * 1024
)
func newHandler() http.HandlerFunc { func newHandler() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) { return func(rw http.ResponseWriter, r *http.Request) {
start := time.Now() start := time.Now()
@@ -59,7 +63,6 @@ func newHandler() http.HandlerFunc {
log.Field("code", code), log.Field("code", code),
log.Field("duration", duration.String()), log.Field("duration", duration.String()),
).Info(ctx, message) ).Info(ctx, message)
} }
} }
@@ -93,6 +96,7 @@ type ResponseRequest struct {
ContentLength int64 `json:"contentLength"` ContentLength int64 `json:"contentLength"`
} }
//nolint:cyclop
func readBody(ctx context.Context, r *http.Request) (any, error) { func readBody(ctx context.Context, r *http.Request) (any, error) {
contentType := r.Header.Get("Content-Type") contentType := r.Header.Get("Content-Type")
switch { switch {
@@ -122,7 +126,7 @@ func readBody(ctx context.Context, r *http.Request) (any, error) {
return string(b), nil return string(b), nil
//case : //case :
case strings.HasPrefix(contentType, "multipart/form-data"): case strings.HasPrefix(contentType, "multipart/form-data"):
if err := r.ParseMultipartForm(16 * 1024 * 1024); err != nil { if err := r.ParseMultipartForm(maxParseMemory); err != nil {
log.Error(ctx, err.Error()) log.Error(ctx, err.Error())
return nil, err return nil, err
} }
@@ -148,8 +152,9 @@ func readBody(ctx context.Context, r *http.Request) (any, error) {
log.Error(ctx, err.Error()) log.Error(ctx, err.Error())
return nil, err return nil, err
} }
if len(b) > 96 { const maxBodyByteSizeToReturn = 96
b = b[:96] if len(b) > maxBodyByteSizeToReturn {
b = b[:maxBodyByteSizeToReturn]
} }
return b, nil return b, nil
} }

View File

@@ -11,7 +11,7 @@ import (
"git.blauwelle.com/go/crate/log/logsdk/logjson" "git.blauwelle.com/go/crate/log/logsdk/logjson"
) )
var port = flag.Int("port", 8080, "HTTP Port") var port = flag.Int("port", 8080, "HTTP Port") //nolint:gomnd
func main() { func main() {
flag.Parse() flag.Parse()
@@ -20,7 +20,7 @@ func main() {
mux := http.NewServeMux() mux := http.NewServeMux()
var handler http.Handler = mux var handler http.Handler = mux
mux.HandleFunc("/", newHandler()) mux.HandleFunc("/", newHandler())
g.New().WithGoStop(exegroup.HttpListenAndServe(*port, handler)) g.New().WithGoStop(exegroup.HTTPListenAndServe(*port, handler))
log.Infof(context.Background(), "listening %d", *port) log.Infof(context.Background(), "listening %d", *port)
log.Error(context.Background(), "exit: ", g.Run(context.Background())) log.Error(context.Background(), "exit: ", g.Run(context.Background()))
} }

138
cmd/retry/.golangci.yaml Normal file
View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -14,7 +14,7 @@ var (
) )
func main() { func main() {
flag.IntVar(&count, "c", 5, "maximum execution times") flag.IntVar(&count, "c", 5, "maximum execution times") //nolint:gomnd
flag.DurationVar(&interval, "i", time.Second, "retry interval") flag.DurationVar(&interval, "i", time.Second, "retry interval")
flag.Parse() flag.Parse()
if count < 1 { if count < 1 {
@@ -34,7 +34,7 @@ func main() {
fmt.Printf("retry %d after %s...\n", i, interval) fmt.Printf("retry %d after %s...\n", i, interval)
time.Sleep(interval) time.Sleep(interval)
} }
cmd := exec.Command(args[0], args[1:]...) cmd := exec.Command(args[0], args[1:]...) // #nosec G204
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
err = cmd.Run() err = cmd.Run()

View File

@@ -54,6 +54,7 @@ log.Logger()
- `SetReportStack` 设置生成调用栈; - `SetReportStack` 设置生成调用栈;
- `SetReportStackLevel` 当日志等级小于设定值时强制生成调用栈; - `SetReportStackLevel` 当日志等级小于设定值时强制生成调用栈;
- `Reset` 把 Logger 恢复到初始状态; - `Reset` 把 Logger 恢复到初始状态;
- `AddBeforeExit` 新增调用 `Logger.Exit` 时优先执行的函数, 先增加的后执行;
- `SetExit` 设置 `Logger` 的退出函数(`Logger.Exit`), 当日志等级为 `LevelFatal` 时调用这个函数; - `SetExit` 设置 `Logger` 的退出函数(`Logger.Exit`), 当日志等级为 `LevelFatal` 时调用这个函数;
### 日志生成 ### 日志生成

View File

@@ -150,6 +150,11 @@ func Panicf(ctx context.Context, format string, args ...any) {
globalLogger.AddCallerSkip(1).Panicf(ctx, format, args...) globalLogger.AddCallerSkip(1).Panicf(ctx, format, args...)
} }
// Exit 调用全局 logger 的 Exit
func Exit(code int) {
globalLogger.Exit(code)
}
// Logger 返回全局 logger, 通常用来在程序启动时对全局 logger 进行配置, // Logger 返回全局 logger, 通常用来在程序启动时对全局 logger 进行配置,
// 业务代码处理日志时直接使用这个包里定义的日志函数. // 业务代码处理日志时直接使用这个包里定义的日志函数.
func Logger() *logsdk.Logger { func Logger() *logsdk.Logger {

View File

@@ -1,6 +1,8 @@
package logsdk package logsdk
import "fmt" import (
"fmt"
)
// Level 日志等级 // Level 日志等级
type Level int type Level int
@@ -12,6 +14,30 @@ func (level Level) String() string {
return "unknown" return "unknown"
} }
// ParseLevel 把 level 字符串解析成 Level,
// 支持的字符串: panic, fatal, error, warn, info, debug, trace,
// 传入不支持的字符串返回 LevelInfo.
func ParseLevel(s string) Level {
switch s {
case LevelPanicValue:
return LevelPanic
case LevelFatalValue:
return LevelFatal
case LevelErrorValue:
return LevelError
case LevelWarnValue:
return LevelWarn
case LevelInfoValue:
return LevelInfo
case LevelDebugValue:
return LevelDebug
case LevelTraceValue:
return LevelTrace
default:
return LevelInfo
}
}
func (level Level) MarshalText() ([]byte, error) { func (level Level) MarshalText() ([]byte, error) {
switch level { switch level {
case LevelPanic: case LevelPanic:

View File

@@ -21,6 +21,7 @@ type entry = Entry
// 使用 Logger 处理日志. // 使用 Logger 处理日志.
type Logger struct { type Logger struct {
exit func(code int) // protected by lock exit func(code int) // protected by lock
beforeExit []func(code int) // protected by lock
levelProcessors levelProcessors // protected by lock levelProcessors levelProcessors // protected by lock
entry entry
lock sync.RWMutex lock sync.RWMutex
@@ -98,6 +99,7 @@ func (logger *Logger) SetReportStackLevel(level Level) {
func (logger *Logger) Reset() { func (logger *Logger) Reset() {
logger.lock.Lock() logger.lock.Lock()
logger.exit = nil logger.exit = nil
logger.beforeExit = nil
logger.levelProcessors = levelProcessors{} logger.levelProcessors = levelProcessors{}
logger.lock.Unlock() logger.lock.Unlock()
@@ -112,9 +114,14 @@ func (logger *Logger) Reset() {
func (logger *Logger) Exit(code int) { func (logger *Logger) Exit(code int) {
logger.lock.RLock() logger.lock.RLock()
exit := logger.exit exit := logger.exit
beforeExit := make([]func(int), len(logger.beforeExit))
copy(beforeExit, logger.beforeExit)
logger.lock.RUnlock() logger.lock.RUnlock()
for i := len(beforeExit) - 1; i >= 0; i-- {
beforeExit[i](code)
}
if exit == nil { if exit == nil {
os.Exit(code) exit = os.Exit
} }
exit(code) exit(code)
} }
@@ -126,6 +133,14 @@ func (logger *Logger) SetExit(fn func(code int)) {
logger.lock.Unlock() logger.lock.Unlock()
} }
// AddBeforeExit 增加 Exit 在调用 SetExit 指定的函数前执行的函数,
// 先增加的后执行.
func (logger *Logger) AddBeforeExit(fn ...func(code int)) {
logger.lock.Lock()
logger.beforeExit = append(logger.beforeExit, fn...)
logger.lock.Unlock()
}
func (logger *Logger) getLevelProcessors(level Level) []EntryProcessor { func (logger *Logger) getLevelProcessors(level Level) []EntryProcessor {
logger.lock.RLock() logger.lock.RLock()
defer logger.lock.RUnlock() defer logger.lock.RUnlock()

138
synchelper/.golangci.yaml Normal file
View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -18,7 +18,7 @@ type syncWriter struct {
lock sync.Mutex lock sync.Mutex
} }
func (w *syncWriter) Write(p []byte) (n int, err error) { func (w *syncWriter) Write(p []byte) (int, error) {
w.lock.Lock() w.lock.Lock()
defer w.lock.Unlock() defer w.lock.Unlock()
return w.writer.Write(p) return w.writer.Write(p)

138
timehelper/.golangci.yaml Normal file
View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -4,5 +4,5 @@ import "time"
// SetupCST 把本地时区固定到CST // SetupCST 把本地时区固定到CST
func SetupCST() { func SetupCST() {
time.Local = time.FixedZone("CST", 8*3600) time.Local = time.FixedZone("CST", 8*3600) //nolint:gomnd,reassign
} }

View File

@@ -14,20 +14,20 @@ func ExampleRFC3339Nano() {
// Output: 2023-01-15T15:16:17.123456789+08:00 // Output: 2023-01-15T15:16:17.123456789+08:00
} }
func ExampleRFC3339Nano2Sec() { func ExampleRFC3339_nano2Sec() {
t, _ := time.Parse(time.RFC3339Nano, "2023-01-15T15:16:17.123456789+08:00") t, _ := time.Parse(time.RFC3339Nano, "2023-01-15T15:16:17.123456789+08:00")
fmt.Println(t.Format(time.RFC3339)) fmt.Println(t.Format(time.RFC3339))
// Output: 2023-01-15T15:16:17+08:00 // Output: 2023-01-15T15:16:17+08:00
} }
// 使用 RFC3339 没有丢失纳秒精度 // 使用 RFC3339 没有丢失纳秒精度
func ExampleRFC3339Sec2Nano() { func ExampleRFC3339_sec2Nano() {
t, _ := time.Parse(time.RFC3339, "2023-01-15T15:16:17.123456789+08:00") t, _ := time.Parse(time.RFC3339, "2023-01-15T15:16:17.123456789+08:00")
fmt.Println(t.Format(time.RFC3339Nano)) fmt.Println(t.Format(time.RFC3339Nano))
// Output: 2023-01-15T15:16:17.123456789+08:00 // Output: 2023-01-15T15:16:17.123456789+08:00
} }
func ExampleRFC3339NanoOtherZone() { func ExampleRFC3339_nanoOtherZone() {
t, _ := time.Parse(time.RFC3339Nano, "2023-01-15T15:16:17.123456789+09:00") t, _ := time.Parse(time.RFC3339Nano, "2023-01-15T15:16:17.123456789+09:00")
fmt.Println(t.Format(time.RFC3339Nano)) fmt.Println(t.Format(time.RFC3339Nano))
// Output: 2023-01-15T15:16:17.123456789+09:00 // Output: 2023-01-15T15:16:17.123456789+09:00

View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -23,7 +23,7 @@ require (
go.opentelemetry.io/otel/sdk/metric v0.36.0 // indirect go.opentelemetry.io/otel/sdk/metric v0.36.0 // indirect
go.opentelemetry.io/otel/trace v1.13.0 // indirect go.opentelemetry.io/otel/trace v1.13.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/net v0.6.0 // indirect golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect golang.org/x/text v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57 // indirect google.golang.org/genproto v0.0.0-20230202175211-008b39050e57 // indirect

View File

@@ -250,6 +250,8 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q= golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

138
wireexample/.golangci.yaml Normal file
View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -16,7 +16,7 @@ func approachA() {
rander := &MockRander{} rander := &MockRander{}
guess := InjectGuessWithoutMock(rander) guess := InjectGuessWithoutMock(rander)
rander.Value = 1 rander.Value = 1
fmt.Println("approachA:", guess.Guess(10)) fmt.Println("approachA:", guess.Guess(10)) //nolint:gomnd
} }
// approach B: // approach B:
@@ -25,5 +25,5 @@ func approachA() {
func approachB() { func approachB() {
guessWithMock := InjectMockGuess() guessWithMock := InjectMockGuess()
guessWithMock.Mock.Value = 1 guessWithMock.Mock.Value = 1
fmt.Println("approachB:", guessWithMock.Guess.Guess(10)) fmt.Println("approachB:", guessWithMock.Guess.Guess(10)) //nolint:gomnd
} }

View File

@@ -14,7 +14,7 @@ type Rander interface {
type DefaultRander struct{} type DefaultRander struct{}
func (r *DefaultRander) Rand() int { func (r *DefaultRander) Rand() int {
return rand.Int() return rand.Int() //nolint:gosec
} }
func NewRander() *DefaultRander { func NewRander() *DefaultRander {