Skip to content

Commit

Permalink
Merge pull request #2 from jortel/go-1.20
Browse files Browse the repository at this point in the history
Go 1.20. Fix CVEs.
  • Loading branch information
jortel authored Feb 16, 2024
2 parents 3faf372 + bd9744c commit 59b5c1a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 62 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
GOPATH ?= $(HOME)/go
GOBIN ?= $(GOPATH)/bin
GOIMPORTS = $(GOBIN)/goimports

PKG = ./error/... \
./logr/... \
./cmd/...

PKGDIR = $(subst /...,,$(PKG))

bin: fmt vet
go build -o bin/cmd github.com/jortel/go-utils/cmd

fmt:
go fmt ${PKG}
fmt: $(GOIMPORTS)
$(GOIMPORTS) -w $(PKGDIR)

vet:
go vet ${PKG}

$(GOIMPORTS):
go install golang.org/x/tools/cmd/goimports@latest
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"

liberr "github.com/jortel/go-utils/error"
"github.com/jortel/go-utils/logr"
)
Expand Down
21 changes: 6 additions & 15 deletions error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (
"strings"
)

//
// New returns a new wrapped error.
func New(m string, kvpair ...interface{}) error {
return Wrap(
errors.New(m),
kvpair...)
}

//
// Wrap an error.
// Returns `err` when err is `nil` or error.
func Wrap(err error, kvpair ...interface{}) error {
Expand Down Expand Up @@ -52,7 +50,6 @@ func Wrap(err error, kvpair ...interface{}) error {
return newError
}

//
// Unwrap an error.
// Returns: the original error when not wrapped.
func Unwrap(err error) (out error) {
Expand All @@ -71,7 +68,6 @@ func Unwrap(err error) (out error) {
return
}

//
// Error provides an error with context and stack.
type Error struct {
// Original error.
Expand All @@ -84,7 +80,6 @@ type Error struct {
stack []string
}

//
// Error description.
func (e Error) Error() string {
if len(e.description) > 0 {
Expand All @@ -94,31 +89,28 @@ func (e Error) Error() string {
}
}

//
// Stack returns the stack trace.
// Format:
// package.Function()
// file:line
// package.Function()
// file:line
// ...
//
// package.Function()
// file:line
// package.Function()
// file:line
// ...
func (e Error) Stack() string {
return strings.Join(e.stack, "\n")
}

//
// Context returns the`context` key/value pairs.
func (e Error) Context() []interface{} {
return e.context
}

//
// Unwrap the error.
func (e Error) Unwrap() error {
return Unwrap(e.wrapped)
}

//
// append the context.
// And odd number of context is interpreted as:
// a description followed by an even number of key value pairs.
Expand All @@ -139,7 +131,6 @@ func (e *Error) append(kvpair []interface{}) {
e.context = append(e.context, kvpair...)
}

//
// Build caused-by.
func (e *Error) causedBy(error, caused string) string {
return fmt.Sprintf(
Expand Down
3 changes: 2 additions & 1 deletion error/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package error

import (
"errors"
"github.com/onsi/gomega"
"testing"

"github.com/onsi/gomega"
)

func TestError(t *testing.T) {
Expand Down
18 changes: 8 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
module github.com/jortel/go-utils

go 1.18
go 1.20

require (
github.com/onsi/gomega v1.27.6
github.com/sirupsen/logrus v1.9.0
github.com/go-logr/logr v1.4.1
github.com/onsi/gomega v1.31.1
github.com/sirupsen/logrus v1.9.3
)

require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/kr/pretty v0.1.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
github.com/google/go-cmp v0.6.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
40 changes: 17 additions & 23 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
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=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
github.com/onsi/gomega v1.31.1/go.mod h1:y40C95dwAD1Nz36SsEnxvfFe8FFfNxzI5eJ0EYGyAy0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14 changes: 3 additions & 11 deletions logr/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package logr

import (
"fmt"
"os"
"strconv"

"github.com/go-logr/logr"
liberr "github.com/jortel/go-utils/error"
log "github.com/sirupsen/logrus"
"os"
"strconv"
)

const (
Expand All @@ -15,7 +16,6 @@ const (
EnvLevel = "LOG_LEVEL"
)

//
// Sink -.
type Sink struct {
delegate *log.Logger
Expand All @@ -26,7 +26,6 @@ type Sink struct {
level int
}

//
// WithName returns a named logger.
func WithName(name string, kvpair ...interface{}) logr.Logger {
return logr.New(
Expand All @@ -36,7 +35,6 @@ func WithName(name string, kvpair ...interface{}) logr.Logger {
})
}

//
// Init builds the delegate logger.
func (s *Sink) Init(_ logr.RuntimeInfo) {
s.delegate = log.New()
Expand Down Expand Up @@ -67,7 +65,6 @@ func (s *Sink) Init(_ logr.RuntimeInfo) {
s.level = n
}

//
// Info logs at info.
func (s *Sink) Info(_ int, message string, kvpair ...interface{}) {
fields := fields(kvpair)
Expand All @@ -76,7 +73,6 @@ func (s *Sink) Info(_ int, message string, kvpair ...interface{}) {
entry.Info(s.named(message))
}

//
// Error logs an error.
func (s *Sink) Error(err error, message string, kvpair ...interface{}) {
if err == nil {
Expand Down Expand Up @@ -120,19 +116,16 @@ func (s *Sink) Error(err error, message string, kvpair ...interface{}) {
}
}

//
// Enabled returns whether logger is enabled.
func (s *Sink) Enabled(level int) bool {
return s.level >= level
}

//
// WithName returns a logger with name.
func (s *Sink) WithName(name string) logr.LogSink {
return &Sink{name: name}
}

//
// WithValues returns a logger with values.
func (s *Sink) WithValues(kvpair ...interface{}) logr.LogSink {
return &Sink{
Expand All @@ -149,7 +142,6 @@ func (s *Sink) named(message string) (m string) {
return
}

//
// fields returns fields for kvpair.
func fields(kvpair []interface{}) log.Fields {
fields := log.Fields{}
Expand Down

0 comments on commit 59b5c1a

Please sign in to comment.