Skip to content

Commit

Permalink
linters
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Feb 9, 2025
1 parent 1a5821b commit 1ceb87e
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 150 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.59
version: v1.63.4

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
14 changes: 11 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ linters-settings:
excludes:
- G108
- G114
- G115
revive:
rules:
- name: unused-parameter
Expand All @@ -33,16 +34,17 @@ linters-settings:
- github.com/getsentry/sentry-go
- github.com/gin-gonic/gin
- github.com/nikandfor/assert
- github.com/nikandfor/hacked
- github.com/opentracing/opentracing-go
- github.com/prometheus/client_golang
- github.com/prometheus/client_model
- github.com/stretchr/testify/assert
- github.com/stretchr/testify/require
- go.opentelemetry.io/otel
- golang.org/x
- nikand.dev/go/cbor
- nikand.dev/go/cli
- nikand.dev/go/graceful
- nikand.dev/go/hacked
- tlog.app/go/eazy
- tlog.app/go/errors
- tlog.app/go/loc
Expand Down Expand Up @@ -74,6 +76,8 @@ linters:
enable-all: true
disable:
- cyclop
- dogsled
- err113
- exhaustive
- exhaustivestruct
- exhaustruct
Expand All @@ -86,15 +90,16 @@ linters:
- goconst
- gocyclo
- godox
- err113
- golint
- gomnd
- mnd
- gosmopolitan
- ifshort
- inamedparam
- ireturn
- lll
- maintidx
- maligned
- mnd
- nakedret
- nestif
- nlreturn
Expand All @@ -103,6 +108,9 @@ linters:
- paralleltest
- prealloc
- predeclared
- recvcheck
- tagalign
- testifylint
- testpackage
- thelper
- typecheck
Expand Down
10 changes: 5 additions & 5 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type (
subs []sub

streams []*stream
files []*file
// files []*file

// end of mu

Expand Down Expand Up @@ -202,7 +202,7 @@ func (a *Agent) writeFile(s *stream, f *file, p []byte, ts int64) (n int, err er
tlog.Printw("write message", "i", geti(p))

s.zbuf = s.zbuf[:0]
n, err = s.z.Write(p)
_, err = s.z.Write(p)
if err != nil {
return 0, errors.Wrap(err, "eazy")
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func (a *Agent) writeFile(s *stream, f *file, p []byte, ts int64) (n int, err er

if s.boff == 0 {
s.zbuf = s.zbuf[:0]
n, err = s.z.Write(p)
_, err = s.z.Write(p)
if err != nil {
return 0, errors.Wrap(err, "eazy")
}
Expand Down Expand Up @@ -307,13 +307,13 @@ func (a *Agent) newFile(s *stream, part, ts int64) (*file, error) {
}

func (a *Agent) padFile(s *stream, f *file) error {
if f.off%int64(a.BlockSize) == 0 {
if f.off%a.BlockSize == 0 {
s.boff = 0

return nil
}

off := f.off + int64(a.BlockSize) - f.off%int64(a.BlockSize)
off := f.off + a.BlockSize - f.off%a.BlockSize

if s, ok := f.w.(interface {
Truncate(int64) error
Expand Down
8 changes: 5 additions & 3 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,14 +726,15 @@ func (w *ConsoleWriter) ConvertValue(b, p []byte, st, ff int) (_ []byte, i int)
}
}

if quote && haveQuotes && !haveBackQuotes && w.QuoteUseBackQuotes {
switch {
case quote && haveQuotes && !haveBackQuotes && w.QuoteUseBackQuotes:
b = append(b, '`')
b = append(b, s...)
b = append(b, '`')
} else if quote {
case quote:
ss := tlow.UnsafeBytesToString(s)
b = strconv.AppendQuote(b, ss)
} else {
default:
b = append(b, s...)
}

Expand Down Expand Up @@ -918,6 +919,7 @@ func (w *ConsoleWriter) AppendDuration(b []byte, d time.Duration) []byte {
d = add(d, time.Hour, 'h')
d = add(d, time.Minute, 'm')
d = add(d, time.Second, 's')
_ = d

return append(b, buf[:i]...)
}
Expand Down
46 changes: 26 additions & 20 deletions convert/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,26 +253,7 @@ func (w *JSON) ConvertValue(b, p []byte, st int) (_ []byte, i int) {

id.FormatTo(b, bst, 'u')
case tlwire.Caller:
var pc loc.PC
var pcs loc.PCs
pc, pcs, i = w.d.Callers(p, st)

if pcs != nil {
b = append(b, '[')
for i, pc := range pcs {
if i != 0 {
b = append(b, ',')
}

_, file, line := pc.NameFileLine()
b = fmt.Appendf(b, `"%v:%d"`, filepath.Base(file), line)
}
b = append(b, ']')
} else {
_, file, line := pc.NameFileLine()

b = fmt.Appendf(b, `"%v:%d"`, filepath.Base(file), line)
}
b, i = appendCallers(b, p, st, w.d)
default:
b, i = w.ConvertValue(b, p, i)
}
Expand Down Expand Up @@ -337,3 +318,28 @@ func (r SimpleRenamer) fallback(b, p, k []byte, i int) ([]byte, bool) {

return r.Fallback(b, p, k, i)
}

func appendCallers(b, p []byte, st int, d tlwire.Decoder) ([]byte, int) {
var pc loc.PC
var pcs loc.PCs
pc, pcs, i := d.Callers(p, st)

if pcs != nil {
b = append(b, '[')
for i, pc := range pcs {
if i != 0 {
b = append(b, ',')
}

_, file, line := pc.NameFileLine()
b = fmt.Appendf(b, `"%v:%d"`, filepath.Base(file), line)
}
b = append(b, ']')
} else {
_, file, line := pc.NameFileLine()

b = fmt.Appendf(b, `"%v:%d"`, filepath.Base(file), line)
}

return b, i
}
23 changes: 1 addition & 22 deletions convert/logfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"errors"
"fmt"
"io"
"path/filepath"
"strconv"
"time"

"golang.org/x/term"
"nikand.dev/go/hacked/low"
"tlog.app/go/loc"

"tlog.app/go/tlog"
tlow "tlog.app/go/tlog/low"
Expand Down Expand Up @@ -259,26 +257,7 @@ func (w *Logfmt) ConvertValue(b, p, k []byte, st int) (_ []byte, i int) {

id.FormatTo(b, bst, 'u')
case tlwire.Caller:
var pc loc.PC
var pcs loc.PCs
pc, pcs, i = w.d.Callers(p, st)

if pcs != nil {
b = append(b, '[')
for i, pc := range pcs {
if i != 0 {
b = append(b, ',')
}

_, file, line := pc.NameFileLine()
b = fmt.Appendf(b, `"%v:%d"`, filepath.Base(file), line)
}
b = append(b, ']')
} else {
_, file, line := pc.NameFileLine()

b = fmt.Appendf(b, `"%v:%d"`, filepath.Base(file), line)
}
b, i = appendCallers(b, p, st, w.d)
default:
b, i = w.ConvertValue(b, p, k, i)
}
Expand Down
2 changes: 1 addition & 1 deletion convert/logfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"
"time"

"nikand.dev/go/hacked/low"
"github.com/stretchr/testify/assert"
"nikand.dev/go/hacked/low"

"tlog.app/go/tlog"
"tlog.app/go/tlog/tlwire"
Expand Down
3 changes: 0 additions & 3 deletions convert/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ type (
PickMessage bool

d tlwire.Decoder
l Logfmt
j JSON
c *tlog.ConsoleWriter

s []tlog.ID
m []byte

time, last []byte

Expand Down
2 changes: 0 additions & 2 deletions ext/tlflag/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ func TestURLWriter(t *testing.T) { //nolint:dupl
func TestRotatedWriter(t *testing.T) {
OpenFileWriter = TestingFileOpener

const CompressorBlockSize = 1 * eazy.MiB

with := func(f *rotating.File, wrap func(*rotating.File)) *rotating.File {
wrap(f)

Expand Down
2 changes: 1 addition & 1 deletion id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestIDJSON(t *testing.T) {
data, err := json.Marshal(id)
assert.NoError(t, err)

t.Logf("json encoded id: %s (% x)", data, []byte(id[:]))
t.Logf("json encoded id: %s (% x)", data, id[:])

var back ID
err = json.Unmarshal(data, &back)
Expand Down
9 changes: 0 additions & 9 deletions rotating/ctime_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import (
"time"
)

func fileCtime(fstat func(string) (fs.FileInfo, error), name string, now time.Time) time.Time {
inf, err := fstat(name)
if err != nil {
return now
}

return ctime(inf, now)
}

func ctime(inf fs.FileInfo, now time.Time) time.Time {
stat, ok := inf.Sys().(*syscall.Stat_t)
if !ok {
Expand Down
9 changes: 0 additions & 9 deletions rotating/ctime_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import (
"time"
)

func fileCtime(fstat func(string) (fs.FileInfo, error), name string, now time.Time) time.Time {
inf, err := fstat(name)
if err != nil {
return now
}

return ctime(inf, now)
}

func ctime(inf fs.FileInfo, now time.Time) time.Time {
stat, ok := inf.Sys().(*syscall.Stat_t)
if !ok {
Expand Down
9 changes: 0 additions & 9 deletions rotating/ctime_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import (
"time"
)

func fileCtime(fstat func(string) (fs.FileInfo, error), name string, now time.Time) time.Time {
inf, err := fstat(name)
if err != nil {
return now
}

return ctime(inf, now)
}

func ctime(inf fs.FileInfo, now time.Time) time.Time {
stat, ok := inf.Sys().(*syscall.Win32FileAttributeData)
if !ok {
Expand Down
7 changes: 4 additions & 3 deletions rotating/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (f *File) Write(p []byte) (n int, err error) {

if f.w == nil || f.size != 0 &&
(f.MaxFileSize != 0 && f.size+int64(len(p)) > f.MaxFileSize ||
f.MaxFileAge != 0 && time.Since(f.start) > f.MaxFileAge) {
f.MaxFileAge != 0 && time.Since(f.start) > f.MaxFileAge) { //nolint:whitespace

err = f.rotate()
if err != nil {
Expand Down Expand Up @@ -163,7 +163,6 @@ func (f *File) rotate() (err error) {
f.current = fname
f.size = 0
f.start = now
// f.start = fileCtime(f.fstat, fname, now)

if f.symlink != nil {
link := filepath.Join(f.dir, f.pref+"LATEST"+f.suff)
Expand All @@ -173,7 +172,9 @@ func (f *File) rotate() (err error) {
}

if f.MaxTotalSize != 0 || f.MaxTotalAge != 0 || f.MaxTotalFiles != 0 {
go f.removeOld(f.dir, base, f.pref, f.suff, f.format, f.start)
go func(start time.Time) {
_ = f.removeOld(f.dir, base, f.pref, f.suff, f.format, start)
}(f.start)
}

return
Expand Down
2 changes: 1 addition & 1 deletion tlio/writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type (

WriterFunc func(p []byte) (int, error)

// base interfaces
// Base interfaces.

Flusher interface {
Flush() error
Expand Down
6 changes: 3 additions & 3 deletions tlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func (s Span) IOWriter(d int) io.Writer {
}

func (l *Logger) DumpWriter(d int, msg, key string) io.Writer {
return dumpWrapper{
return &dumpWrapper{
Span: Span{
Logger: l,
},
Expand All @@ -453,7 +453,7 @@ func (l *Logger) DumpWriter(d int, msg, key string) io.Writer {
}

func (s Span) DumpWriter(d int, msg, key string) io.Writer {
return dumpWrapper{
return &dumpWrapper{
Span: s,

loc: loc.Caller(1 + d),
Expand All @@ -468,7 +468,7 @@ func (w writeWrapper) Write(p []byte) (int, error) {
return len(p), nil
}

func (w dumpWrapper) Write(p []byte) (int, error) {
func (w *dumpWrapper) Write(p []byte) (int, error) {
message(w.Logger, w.ID, -1, w.msg, []any{KeyCaller, w.loc, w.key, p})

return len(p), nil
Expand Down
Loading

0 comments on commit 1ceb87e

Please sign in to comment.