From 8a5a786954b8c0134a19819ff39a364e5c40d555 Mon Sep 17 00:00:00 2001 From: elij Date: Tue, 21 Jan 2025 11:47:32 -0800 Subject: [PATCH] some clean ups --- .errcheck-excludes.txt | 13 ++++ .gitignore | 1 + Makefile | 148 +++++++++++++++++++++++++++++++++++++ formatwriter_json.go | 8 +- formatwriter_plain.go | 8 +- formatwriter_structured.go | 8 +- go.mod | 2 +- go.sum | 4 +- slicebuffer.go | 13 +++- time.go | 9 ++- time_test.go | 6 +- tlogger.go | 2 +- 12 files changed, 197 insertions(+), 25 deletions(-) create mode 100644 .errcheck-excludes.txt create mode 100644 Makefile diff --git a/.errcheck-excludes.txt b/.errcheck-excludes.txt new file mode 100644 index 0000000..eecaec0 --- /dev/null +++ b/.errcheck-excludes.txt @@ -0,0 +1,13 @@ +fmt.Fprint +fmt.Fprintf +(*os.File).Close +(*bufio.Writer).Flush +(*sliceBuffer).WriteByte +(github.com/cactus/mlog.byteSliceWriter).Write +(github.com/cactus/mlog.byteSliceWriter).WriteByte +(github.com/cactus/mlog.byteSliceWriter).WriteString +(*github.com/cactus/mlog.sliceBuffer).Write +(*github.com/cactus/mlog.sliceBuffer).WriteByte +(*github.com/cactus/mlog.sliceBuffer).WriteString +(*github.com/cactus/mlog.sliceBuffer).WriteTo +(*github.com/cactus/mlog.discardSliceWriter).WriteByte diff --git a/.gitignore b/.gitignore index 9e13edb..23a6926 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /bin /pkg /vendor/src +/.tools *.py[co] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..aae2add --- /dev/null +++ b/Makefile @@ -0,0 +1,148 @@ +# environment +BUILDDIR := ${CURDIR}/build +ARCH := $(shell go env GOHOSTARCH) +OS := $(shell go env GOHOSTOS) +GOVER := $(shell go version | awk '{print $$3}' | tr -d '.') + +# app specific info +APP_VER := v$(shell git describe --always --tags|sed 's/^v//') +GITHASH := $(shell git rev-parse --short HEAD) +GOPATH := $(shell go env GOPATH) +GOBIN := ${CURDIR}/.tools +VERSION_VAR := main.Version + +# flags and build configuration +GOBUILD_OPTIONS := -trimpath +GOTEST_FLAGS := +GOTEST_BENCHFLAGS := +GOBUILD_DEPFLAGS := -tags netgo,production +GOBUILD_LDFLAGS ?= -s -w +GOBUILD_FLAGS := ${GOBUILD_DEPFLAGS} ${GOBUILD_OPTIONS} -ldflags "${GOBUILD_LDFLAGS} -X ${VERSION_VAR}=${APP_VER}" + +# cross compile defs +CC_BUILD_TARGETS = +CC_BUILD_ARCHES = darwin/amd64 darwin/arm64 freebsd/amd64 linux/amd64 linux/arm64 windows/amd64 +CC_OUTPUT_TPL := ${BUILDDIR}/bin/{{.Dir}}.{{.OS}}-{{.Arch}} + +# misc +DOCKER_PREBUILD ?= + +# some exported vars (pre-configure go build behavior) +export GO111MODULE=on +#export CGO_ENABLED=0 +## enable go 1.21 loopvar "experiment" +export GOEXPERIMENT=loopvar +export GOBIN +export PATH := ${GOBIN}:${PATH} + +define HELP_OUTPUT +Available targets: + help this help + clean clean up + all build binaries and man pages + check run checks and validators + test run tests + cover run tests with cover output + bench run benchmarks + generate run go:generate + build build all binaries + update-go-deps updates go.mod and go.sum files +endef +export HELP_OUTPUT + + +.PHONY: help +help: + @echo "$$HELP_OUTPUT" + +.PHONY: clean +clean: + @rm -rf "${BUILDDIR}" + +${GOBIN}/stringer: + go install golang.org/x/tools/cmd/stringer@latest + +${GOBIN}/staticcheck: + go install honnef.co/go/tools/cmd/staticcheck@latest + +${GOBIN}/gosec: + go install github.com/securego/gosec/v2/cmd/gosec@latest + +${GOBIN}/govulncheck: + go install golang.org/x/vuln/cmd/govulncheck@latest + +${GOBIN}/errcheck: + go install github.com/kisielk/errcheck@latest + +${GOBIN}/ineffassign: + go install github.com/gordonklaus/ineffassign@latest + +${GOBIN}/nilaway: + go install go.uber.org/nilaway/cmd/nilaway@latest + +BUILD_TOOLS := ${GOBIN}/stringer +CHECK_TOOLS := ${GOBIN}/staticcheck ${GOBIN}/gosec ${GOBIN}/govulncheck +CHECK_TOOLS += ${GOBIN}/errcheck ${GOBIN}/ineffassign ${GOBIN}/nilaway + +.PHONY: setup +setup: + +.PHONY: setup-build +setup-build: setup ${BUILD_TOOLS} + +.PHONY: setup-check +setup-check: setup ${CHECK_TOOLS} + +.PHONY: generate +generate: setup-build + @echo ">> Generating..." + @PATH="${PATH}" go generate ./... + +.PHONY: build +build: setup-build + @echo ">> Building..." + @[ -d "${BUILDDIR}/bin" ] || mkdir -p "${BUILDDIR}/bin" + @(for x in ${CC_BUILD_TARGETS}; do \ + echo "...$${x}..."; \ + go build ${GOBUILD_FLAGS} -o "${BUILDDIR}/bin/$${x}" ./cmd/$${x}; \ + done) + @echo "done!" + +.PHONY: test +test: setup + @echo ">> Running tests..." + @go test -count=1 -vet=off ${GOTEST_FLAGS} ./... + +.PHONY: bench +bench: setup + @echo ">> Running benchmarks..." + @go test -bench="." -run="^$$" -test.benchmem=true ${GOTEST_BENCHFLAGS} ./... + +.PHONY: cover +cover: setup + @echo ">> Running tests with coverage..." + @go test -vet=off -cover ${GOTEST_FLAGS} ./... + +.PHONY: check +check: setup-check + @echo ">> Running checks and validators..." + @echo "... staticcheck ..." + @${GOBIN}/staticcheck ./... + @echo "... errcheck ..." + @${GOBIN}/errcheck -ignoretests -exclude .errcheck-excludes.txt ./... + @echo "... go-vet ..." + @go vet ./... + @echo "... gosec ..." + @${GOBIN}/gosec -quiet -exclude-dir=tool -exclude G104 ./... + @echo "... ineffassign ..." + @${GOBIN}/ineffassign ./... + @echo "... govulncheck ..." + +.PHONY: update-go-deps +update-go-deps: setup + @echo ">> updating Go dependencies..." + @go get -u all + @go mod tidy + +.PHONY: all +all: build diff --git a/formatwriter_json.go b/formatwriter_json.go index 17c87d5..6242a70 100644 --- a/formatwriter_json.go +++ b/formatwriter_json.go @@ -28,9 +28,9 @@ func (j *FormatWriterJSON) EmitAttrs(logger *Logger, level int, message string, t := time.Now() sb.WriteString(`"time": "`) if flags&Ltai64n != 0 { - writeTimeTAI64N(sb, &t, flags) + writeTimeTAI64N(sb, &t) } else { - writeTime(sb, &t, flags) + writeTime(sb, &t) } sb.WriteString(`", `) } @@ -103,9 +103,9 @@ func (j *FormatWriterJSON) Emit(logger *Logger, level int, message string, extra t := time.Now() sb.WriteString(`"time": "`) if flags&Ltai64n != 0 { - writeTimeTAI64N(sb, &t, flags) + writeTimeTAI64N(sb, &t) } else { - writeTime(sb, &t, flags) + writeTime(sb, &t) } sb.WriteString(`", `) } diff --git a/formatwriter_plain.go b/formatwriter_plain.go index 653f675..68de04c 100644 --- a/formatwriter_plain.go +++ b/formatwriter_plain.go @@ -23,9 +23,9 @@ func (l *FormatWriterPlain) EmitAttrs(logger *Logger, level int, message string, if flags&(Ltimestamp|Ltai64n) != 0 { t := time.Now() if flags&Ltai64n != 0 { - writeTimeTAI64N(sb, &t, flags) + writeTimeTAI64N(sb, &t) } else { - writeTime(sb, &t, flags) + writeTime(sb, &t) } sb.WriteByte(' ') } @@ -87,9 +87,9 @@ func (l *FormatWriterPlain) Emit(logger *Logger, level int, message string, extr if flags&(Ltimestamp|Ltai64n) != 0 { t := time.Now() if flags&Ltai64n != 0 { - writeTimeTAI64N(sb, &t, flags) + writeTimeTAI64N(sb, &t) } else { - writeTime(sb, &t, flags) + writeTime(sb, &t) } sb.WriteByte(' ') } diff --git a/formatwriter_structured.go b/formatwriter_structured.go index 0d8cb0d..7453012 100644 --- a/formatwriter_structured.go +++ b/formatwriter_structured.go @@ -24,9 +24,9 @@ func (l *FormatWriterStructured) EmitAttrs(logger *Logger, level int, message st t := time.Now() sb.WriteString(`time="`) if flags&Ltai64n != 0 { - writeTimeTAI64N(sb, &t, flags) + writeTimeTAI64N(sb, &t) } else { - writeTime(sb, &t, flags) + writeTime(sb, &t) } sb.WriteString(`" `) } @@ -94,9 +94,9 @@ func (l *FormatWriterStructured) Emit(logger *Logger, level int, message string, t := time.Now() sb.WriteString(`time="`) if flags&Ltai64n != 0 { - writeTimeTAI64N(sb, &t, flags) + writeTimeTAI64N(sb, &t) } else { - writeTime(sb, &t, flags) + writeTime(sb, &t) } sb.WriteString(`" `) } diff --git a/go.mod b/go.mod index 91c06fa..b1df08a 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/cactus/mlog go 1.16 require ( - github.com/cactus/tai64 v1.0.2 + github.com/cactus/tai64 v1.0.3 github.com/google/go-cmp v0.6.0 // indirect gotest.tools/v3 v3.5.1 ) diff --git a/go.sum b/go.sum index 4d9abdb..79030a1 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/cactus/tai64 v1.0.2 h1:c5rm3aQ9z3b6Vva2LXRSICx/Rpu9rj4MHEzRG1g7dK0= -github.com/cactus/tai64 v1.0.2/go.mod h1:gu5LAXd6eWwrRD/HPw+aTrJF5WkieYswRVLSNslKGg4= +github.com/cactus/tai64 v1.0.3 h1:GKdl8U1VprATgD16ob7Vjn7k+0G+rodh9roOcobjb3I= +github.com/cactus/tai64 v1.0.3/go.mod h1:Ciis5iTJ0/vRkbGsPqBvWTOtXbJdYobxVG/C4tSP9BY= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/slicebuffer.go b/slicebuffer.go index be187b9..27212ea 100644 --- a/slicebuffer.go +++ b/slicebuffer.go @@ -46,8 +46,11 @@ type sliceBuffer struct { } func (sb *sliceBuffer) AppendIntWidth(i int, wid int) { - digits := 0 // write digits backwards (easier/faster) + if i < 0 { + sb.data = append(sb.data, '-') + } + digits := 0 for i >= 10 { q := i / 10 sb.data = append(sb.data, byte('0'+i-q*10)) @@ -72,7 +75,13 @@ func (sb *sliceBuffer) AppendIntWidth(i int, wid int) { const hexdigits = "0123456789abcdefghijklmnopqrstuvwxyz" func (sb *sliceBuffer) AppendIntWidthHex(i int64, wid int) { - u := uint64(i) + var u uint64 + if i < 0 { + sb.data = append(sb.data, '-') + u = uint64(-i) + } else { + u = uint64(i) + } digits := 0 b := uint64(16) diff --git a/time.go b/time.go index db936c2..2d162c8 100644 --- a/time.go +++ b/time.go @@ -6,7 +6,7 @@ import ( "github.com/cactus/tai64" ) -func writeTime(sb intSliceWriter, t *time.Time, flags FlagSet) { +func writeTime(sb intSliceWriter, t *time.Time) { year, month, day := t.Date() sb.AppendIntWidth(year, 4) sb.WriteByte('-') @@ -36,13 +36,14 @@ func writeTime(sb intSliceWriter, t *time.Time, flags FlagSet) { } else { sb.WriteByte('+') } - sb.AppendIntWidth(offset/3600, 2) + offset := offset / 60 + sb.AppendIntWidth(offset/60, 2) sb.WriteByte(':') - sb.AppendIntWidth(offset%3600, 2) + sb.AppendIntWidth(offset%60, 2) } } -func writeTimeTAI64N(sb intSliceWriter, t *time.Time, flags FlagSet) { +func writeTimeTAI64N(sb intSliceWriter, t *time.Time) { tu := t.UTC() tux := tu.Unix() offset := tai64.GetOffsetUnix(tux) diff --git a/time_test.go b/time_test.go index afd3869..4c28ab3 100644 --- a/time_test.go +++ b/time_test.go @@ -40,8 +40,8 @@ func TestTime(t *testing.T) { b := &sliceBuffer{make([]byte, 0, 1024)} for _, tc := range cases { b.Truncate(0) - writeTime(b, &(tc.T), tc.F) - assert.Check(t, is.Equal(tc.R, b.String()), "time written incorrectly") + writeTime(b, &tc.T) + assert.Check(t, is.Equal(b.String(), tc.R), "time written incorrectly") } } @@ -77,7 +77,7 @@ func TestTimeTAI64N(t *testing.T) { b := &sliceBuffer{make([]byte, 0, 1024)} for _, tc := range cases { b.Truncate(0) - writeTimeTAI64N(b, &(tc.T), tc.F) + writeTimeTAI64N(b, &(tc.T)) assert.Check(t, is.Equal(tc.R, b.String()), "time written incorrectly") } } diff --git a/tlogger.go b/tlogger.go index 9eb6cc0..fdda265 100644 --- a/tlogger.go +++ b/tlogger.go @@ -36,6 +36,6 @@ func (lw *TestingLogWriter) Write(p []byte) (n int, err error) { return 0, fmt.Errorf("got a nil testing.TBf") } lw.tb.Helper() - lw.tb.Logf(string(p)) + lw.tb.Log(string(p)) return len(p), nil }