Skip to content

Commit

Permalink
fix lint issues and remove net/dial files to net
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier committed Apr 11, 2024
1 parent a534333 commit 90eeff5
Show file tree
Hide file tree
Showing 29 changed files with 428 additions and 217 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: golangci-lint
on:
push:
branches:
- master
pull_request:
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
pull-requests: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.57

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
144 changes: 144 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
service:
golangci-lint-version: 1.57.x # use the fixed version to not introduce new linters unexpectedly

run:
concurrency: 4
# timeout for analysis, e.g. 30s, 5m, default is 1m
deadline: 20m
build-tags:
- integ
- integfuzz

linters:
disable-all: true
enable:
- unused
- errcheck
- exportloopref
- gocritic
- gofumpt
- goimports
- revive
- gosimple
- govet
- ineffassign
- lll
- misspell
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- gci
- gosec
- asciicheck
- prealloc
- predeclared
- makezero
fast: false

linters-settings:
errcheck:
# report about not checking of errors in type assetions: `a := b.(MyStruct)`;
# default is false: such cases aren't reported by default.
check-type-assertions: false

# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
# default is false: such cases aren't reported by default.
check-blank: false
govet:
# report about shadowed variables
check-shadowing: false
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
locale: US
ignore-words:
- cancelled
- marshalled
lll:
# max line length, lines longer will be reported. Default is 120.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
line-length: 160
# tab width in spaces. Default to 1.
tab-width: 1
gocritic:
disabled-checks:
- exitAfterDefer
unused:
check-exported: false
unparam:
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
# with golangci-lint call it on a directory with the changed file.
check-exported: false
gci:
sections:
- standard
- default
- prefix(github.com/fatedier/golib/)
gosec:
severity: "low"
confidence: "low"
excludes:
- G102
- G112
- G306
- G401
- G402
- G404
- G501
- G505

issues:
# List of regexps of issue texts to exclude, empty list by default.
# But independently from this option we use default exclude patterns,
# it can be disabled by `exclude-use-default: false`. To list all
# excluded by default patterns execute `golangci-lint run --help`
# exclude:
# - composite literal uses unkeyed fields

exclude-rules:
# Exclude some linters from running on test files.
- path: _test\.go$|^tests/|^samples/
linters:
- errcheck
- maligned
- linters:
- revive
- stylecheck
text: "use underscores in Go names"
- linters:
- revive
text: "unused-parameter"
- linters:
- unparam
text: "is always"
- linters:
- staticcheck
text: "SA6002"

exclude-dirs:
- genfiles$
- vendor$
- bin$
exclude-files:
- ".*\\.pb\\.go"
- ".*\\.gen\\.go"

# Independently from option `exclude` we use default exclude patterns,
# it can be disabled by this option. To list all
# excluded by default patterns execute `golangci-lint run --help`.
# Default value for this option is true.
exclude-use-default: true

# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-per-linter: 0

# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
max-same-issues: 0
11 changes: 4 additions & 7 deletions control/limit/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ func (l *Limiter) Acquire(timeout time.Duration) (err error) {

if timeout == 0 {
// no timeout limit
select {
case _, ok := <-l.poolCh:
if !ok {
err = ErrClosed
}
_, ok := <-l.poolCh
if !ok {
err = ErrClosed
}
} else {
select {
Expand All @@ -121,11 +119,10 @@ func (l *Limiter) Release() {
}); err != nil {
atomic.AddInt64(&l.current, -1)
}

}

func (l *Limiter) SetLimit(num int64) {
gerr.PanicToError(func() {
_ = gerr.PanicToError(func() {
l.limitCh <- num
})
}
Expand Down
7 changes: 5 additions & 2 deletions crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import (
func TestCryptoWriterAndReader(t *testing.T) {
assert := assert.New(t)

text := "1234567890abcdefghigklmnopqrstuvwxyzeeeeeeeeeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwwwwwwwwwzzzzzzzzzzzzzzzzzzzzzzzzdddddddddddddddddddddddddddddddddddddrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrllllllllllllllllllllllllllllllllllqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwwwww"
text := "1234567890abcdefghigklmnopqrstuvwxyzeeeeeeeeeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwwwwwwwww" +
"zzzzzzzzzzzzzzzzzzzzzzzzdddddddddddddddddddddddddddddddddddddrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr" +
"llllllllllllllllllllllllllllllllllqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" +
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwwwww"
key := "123456"

buffer := bytes.NewBuffer(nil)
Expand All @@ -37,7 +40,7 @@ func TestCryptoWriterAndReader(t *testing.T) {

c := bytes.NewBuffer(nil)
io.Copy(c, decReader)
assert.Equal(text, string(c.Bytes()))
assert.Equal(text, c.String())
}

func TestCryptoEncodeAndDecode(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions crypto/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import (
"golang.org/x/crypto/pbkdf2"
)

var (
DefaultSalt = "crypto"
)
var DefaultSalt = "crypto"

// NewWriter returns a new Writer that encrypts bytes to w.
func NewWriter(w io.Writer, key []byte) (*Writer, error) {
Expand Down
2 changes: 1 addition & 1 deletion errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
func PanicToError(fn func()) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Panic error: %v", r)
err = fmt.Errorf("panic error: %v", r)
}
}()

Expand Down
9 changes: 5 additions & 4 deletions io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (
"io"
"sync"

"github.com/golang/snappy"

"github.com/fatedier/golib/crypto"
"github.com/fatedier/golib/pool"
"github.com/golang/snappy"
)

// Join two io.ReadWriteCloser and do some operations.
Expand Down Expand Up @@ -62,10 +63,10 @@ func WithEncryption(rwc io.ReadWriteCloser, key []byte) (io.ReadWriteCloser, err

func WithCompression(rwc io.ReadWriteCloser) io.ReadWriteCloser {
sr := snappy.NewReader(rwc)
sw := snappy.NewWriter(rwc)
sw := snappy.NewBufferedWriter(rwc)
return WrapReadWriteCloser(sr, sw, func() error {
err := rwc.Close()
return err
_ = sw.Close()
return rwc.Close()
})
}

Expand Down
17 changes: 12 additions & 5 deletions io/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ func TestJoin(t *testing.T) {
n int
err error
)
text1 := "A document that gives tips for writing clear, idiomatic Go code. A must read for any new Go programmer. It augments the tour and the language specification, both of which should be read first."
text2 := "A document that specifies the conditions under which reads of a variable in one goroutine can be guaranteed to observe values produced by writes to the same variable in a different goroutine."
text1 := "A document that gives tips for writing clear, idiomatic Go code. A must read for any new Go programmer." +
" It augments the tour and the language specification, both of which should be read first."
text2 := "A document that specifies the conditions under which reads of a variable in one goroutine can be guaranteed" +
" to observe values produced by writes to the same variable in a different goroutine."

// Forward bytes directly.
pr, pw := io.Pipe()
Expand Down Expand Up @@ -104,8 +106,13 @@ func TestWithEncryption(t *testing.T) {
n int
err error
)
text1 := "Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language."
text2 := "An interactive introduction to Go in three sections. The first section covers basic syntax and data structures; the second discusses methods and interfaces; and the third introduces Go's concurrency primitives. Each section concludes with a few exercises so you can practice what you've learned. You can take the tour online or install it locally with"
text1 := "Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs" +
"that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction." +
" Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection." +
" It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language."
text2 := "An interactive introduction to Go in three sections. The first section covers basic syntax and data structures; " +
"the second discusses methods and interfaces; and the third introduces Go's concurrency primitives." +
" Each section concludes with a few exercises so you can practice what you've learned. You can take the tour online or install it locally with"
key := "authkey"

// Forward enrypted bytes.
Expand Down Expand Up @@ -140,6 +147,6 @@ func TestWithEncryption(t *testing.T) {
assert.NoError(err)
assert.Equal(text1, string(buf[:n]))

n, err = conn1.Read(buf)
_, err = conn1.Read(buf)
assert.NoError(err)
}
28 changes: 18 additions & 10 deletions log/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,49 @@ package log
var DefaultLogger = New()

func Trace(args ...interface{}) {
DefaultLogger.log(TraceLevel, "", args...)
DefaultLogger.log(TraceLevel, 0, "", args...)
}

func Debug(args ...interface{}) {
DefaultLogger.log(DebugLevel, "", args...)
DefaultLogger.log(DebugLevel, 0, "", args...)
}

func Info(args ...interface{}) {
DefaultLogger.log(InfoLevel, "", args...)
DefaultLogger.log(InfoLevel, 0, "", args...)
}

func Warn(args ...interface{}) {
DefaultLogger.log(WarnLevel, "", args...)
DefaultLogger.log(WarnLevel, 0, "", args...)
}

func Error(args ...interface{}) {
DefaultLogger.log(ErrorLevel, "", args...)
DefaultLogger.log(ErrorLevel, 0, "", args...)
}

func Log(level Level, offset int, args ...interface{}) {
DefaultLogger.log(level, offset, "", args...)
}

func Tracef(msg string, args ...interface{}) {
DefaultLogger.log(TraceLevel, msg, args...)
DefaultLogger.log(TraceLevel, 0, msg, args...)
}

func Debugf(msg string, args ...interface{}) {
DefaultLogger.log(DebugLevel, msg, args...)
DefaultLogger.log(DebugLevel, 0, msg, args...)
}

func Infof(msg string, args ...interface{}) {
DefaultLogger.log(InfoLevel, msg, args...)
DefaultLogger.log(InfoLevel, 0, msg, args...)
}

func Warnf(msg string, args ...interface{}) {
DefaultLogger.log(WarnLevel, msg, args...)
DefaultLogger.log(WarnLevel, 0, msg, args...)
}

func Errorf(msg string, args ...interface{}) {
DefaultLogger.log(ErrorLevel, msg, args...)
DefaultLogger.log(ErrorLevel, 0, msg, args...)
}

func Logf(level Level, offset int, msg string, args ...interface{}) {
DefaultLogger.log(level, offset, msg, args...)
}
Loading

0 comments on commit 90eeff5

Please sign in to comment.