From fe39e75e1050d4ad5b3f99b25a8b49fc0d2cad60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20D=C3=B6ll?= Date: Tue, 30 Jul 2024 20:46:24 +0000 Subject: [PATCH] feat: adding helm update, logx, errorx --- cast/doc.go | 2 + cmd/helm/update/main.go | 54 +++++++++ errorx/doc.go | 2 + errorx/ignore.go | 6 + errorx/must.go | 10 ++ go.mod | 23 ++-- go.sum | 36 ++++-- logx/facade.go | 71 ++++++++++++ logx/logger.go | 251 ++++++++++++++++++++++++++++++++++++++++ logx/logger_test.go | 25 ++++ slices/doc.go | 2 + 11 files changed, 462 insertions(+), 20 deletions(-) create mode 100644 cast/doc.go create mode 100644 cmd/helm/update/main.go create mode 100644 errorx/doc.go create mode 100644 errorx/ignore.go create mode 100644 errorx/must.go create mode 100644 logx/facade.go create mode 100644 logx/logger.go create mode 100644 logx/logger_test.go create mode 100644 slices/doc.go diff --git a/cast/doc.go b/cast/doc.go new file mode 100644 index 0000000..f70b32d --- /dev/null +++ b/cast/doc.go @@ -0,0 +1,2 @@ +// A simple package to cast between types. +package cast diff --git a/cmd/helm/update/main.go b/cmd/helm/update/main.go new file mode 100644 index 0000000..719540b --- /dev/null +++ b/cmd/helm/update/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "errors" + "log" + "os" + "strings" + + "github.com/zeiss/pkg/logx" + + "github.com/spf13/pflag" + "golang.org/x/mod/semver" + "helm.sh/helm/pkg/chartutil" +) + +type flags struct { + File string + Version string +} + +func main() { + log.SetFlags(0) + log.SetOutput(os.Stderr) + + logx.RedirectStdLog(logx.LogSink) + + f := &flags{} + + pflag.StringVar(&f.File, "file", f.File, "chart") + pflag.StringVar(&f.Version, "version", f.Version, "version") + pflag.Parse() + + f.Version = semver.Canonical(f.Version) + + ok := semver.IsValid(f.Version) + if !ok { + log.Fatal(errors.New("updater: no valid version")) + } + + f.Version = strings.TrimPrefix(f.Version, "v") + + meta, err := chartutil.LoadChartfile(f.File) + if err != nil { + log.Fatal(err) + } + + meta.AppVersion = f.Version + meta.Version = f.Version + + err = chartutil.SaveChartfile(f.File, meta) + if err != nil { + log.Fatal(err) + } +} diff --git a/errorx/doc.go b/errorx/doc.go new file mode 100644 index 0000000..5771e33 --- /dev/null +++ b/errorx/doc.go @@ -0,0 +1,2 @@ +// A package to help with errors. +package errorx diff --git a/errorx/ignore.go b/errorx/ignore.go new file mode 100644 index 0000000..79a0b1b --- /dev/null +++ b/errorx/ignore.go @@ -0,0 +1,6 @@ +package errorx + +// Ignore is a helper function to ignore errors. +func Ignore[T any](val T, err error) T { + return val +} diff --git a/errorx/must.go b/errorx/must.go new file mode 100644 index 0000000..68e4b7d --- /dev/null +++ b/errorx/must.go @@ -0,0 +1,10 @@ +package errorx + +// Must panics if the error is not nil. +func Must[T any](val T, err error) T { + if err != nil { + panic(err) + } + + return val +} diff --git a/go.mod b/go.mod index ae3b1fc..5e3ec97 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,17 @@ module github.com/zeiss/pkg -go 1.22 +go 1.22.0 + +toolchain go1.22.5 require ( github.com/golang/mock v1.6.0 github.com/golangci/golangci-lint v1.56.2 - github.com/stretchr/testify v1.8.4 + github.com/spf13/pflag v1.0.5 + github.com/stretchr/testify v1.9.0 + go.uber.org/zap v1.24.0 + golang.org/x/mod v0.15.0 + helm.sh/helm v2.17.0+incompatible mvdan.cc/gofumpt v0.6.0 ) @@ -42,6 +48,7 @@ require ( github.com/charithe/durationcheck v0.0.10 // indirect github.com/chavacava/garif v0.1.0 // indirect github.com/curioswitch/go-reassign v0.2.0 // indirect + github.com/cyphar/filepath-securejoin v0.3.1 // indirect github.com/daixiang0/gci v0.12.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/denis-tingaikin/go-header v0.4.3 // indirect @@ -52,6 +59,7 @@ require ( github.com/firefart/nonamedreturns v1.0.4 // indirect github.com/fsnotify/fsnotify v1.5.4 // indirect github.com/fzipp/gocyclo v0.6.0 // indirect + github.com/ghodss/yaml v1.0.0 // indirect github.com/ghostiam/protogetter v0.3.4 // indirect github.com/go-critic/go-critic v0.11.1 // indirect github.com/go-toolsmith/astcast v1.1.0 // indirect @@ -65,7 +73,7 @@ require ( github.com/go-xmlfmt/xmlfmt v1.1.2 // indirect github.com/gobwas/glob v0.2.3 // indirect github.com/gofrs/flock v0.8.1 // indirect - github.com/golang/protobuf v1.5.3 // indirect + github.com/golang/protobuf v1.5.4 // indirect github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 // indirect github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect github.com/golangci/go-misc v0.0.0-20220329215616-d24fe342adfe // indirect @@ -148,11 +156,10 @@ require ( github.com/spf13/cast v1.5.0 // indirect github.com/spf13/cobra v1.7.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.12.0 // indirect github.com/ssgreg/nlreturn/v2 v2.2.1 // indirect github.com/stbenjam/no-sprintf-host-port v0.1.1 // indirect - github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/objx v0.5.2 // indirect github.com/subosito/gotenv v1.4.1 // indirect github.com/t-yuki/gocover-cobertura v0.0.0-20180217150009-aaee18c8195c // indirect github.com/tdakkota/asciicheck v0.2.0 // indirect @@ -173,12 +180,10 @@ require ( go-simpler.org/sloglint v0.4.0 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect - go.uber.org/zap v1.24.0 // indirect golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect golang.org/x/exp/typeparams v0.0.0-20231219180239-dc181d75b848 // indirect - golang.org/x/mod v0.15.0 // indirect golang.org/x/sync v0.6.0 // indirect - golang.org/x/sys v0.17.0 // indirect + golang.org/x/sys v0.21.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/tools v0.18.0 // indirect google.golang.org/protobuf v1.33.0 // indirect @@ -186,6 +191,8 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect honnef.co/go/tools v0.4.6 // indirect + k8s.io/apimachinery v0.30.3 // indirect + k8s.io/helm v2.17.0+incompatible // indirect mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed // indirect mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect mvdan.cc/unparam v0.0.0-20240104100049-c549a3470d14 // indirect diff --git a/go.sum b/go.sum index 4ff6e71..0a9383c 100644 --- a/go.sum +++ b/go.sum @@ -118,6 +118,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/curioswitch/go-reassign v0.2.0 h1:G9UZyOcpk/d7Gd6mqYgd8XYWFMw/znxwGDUstnC9DIo= github.com/curioswitch/go-reassign v0.2.0/go.mod h1:x6OpXuWvgfQaMGks2BZybTngWjT84hqJfKoO8Tt/Roc= +github.com/cyphar/filepath-securejoin v0.3.1 h1:1V7cHiaW+C+39wEfpH6XlLBQo3j/PciWFrgfCLS8XrE= +github.com/cyphar/filepath-securejoin v0.3.1/go.mod h1:F7i41x/9cBF7lzCrVsYs9fuzwRZm4NQsGTBdpp6mETc= github.com/daixiang0/gci v0.12.1 h1:ugsG+KRYny1VK4oqrX4Vtj70bo4akYKa0tgT1DXMYiY= github.com/daixiang0/gci v0.12.1/go.mod h1:xtHP9N7AHdNvtRNfcx9gwTDfw7FRJx4bZUsiEfiNNAI= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -145,6 +147,8 @@ github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwV github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/fzipp/gocyclo v0.6.0 h1:lsblElZG7d3ALtGMx9fmxeTKZaLLpU8mET09yN4BBLo= github.com/fzipp/gocyclo v0.6.0/go.mod h1:rXPyn8fnlpa0R2csP/31uerbiVBugk5whMdlyaLkLoA= +github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/ghostiam/protogetter v0.3.4 h1:5SZ+lZSNmNkSbGVSF9hUHhv/b7ELF9Rwchoq7btYo6c= github.com/ghostiam/protogetter v0.3.4/go.mod h1:A0JgIhs0fgVnotGinjQiKaFVG3waItLJNwPmcMzDnvk= github.com/go-critic/go-critic v0.11.1 h1:/zBseUSUMytnRqxjlsYNbDDxpu3R2yH8oLXo/FOE8b8= @@ -158,8 +162,8 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= -github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= -github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +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-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= @@ -220,8 +224,8 @@ github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= -github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 h1:23T5iq8rbUYlhpt5DB4XJkc6BU31uODLD1o1gKvZmD0= github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2/go.mod h1:k9Qvh+8juN+UKMCS/3jFtGICgW8O96FVaZsaxdzDkR4= github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a h1:w8hkcTqaFpzKqonE9uMCefW1WDie15eSP/4MssdenaM= @@ -268,8 +272,8 @@ github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hf github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE= -github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec= +github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= @@ -503,8 +507,9 @@ github.com/stbenjam/no-sprintf-host-port v0.1.1/go.mod h1:TLhvtIvONRzdmkFiio4O8L github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -512,8 +517,9 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs= github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/t-yuki/gocover-cobertura v0.0.0-20180217150009-aaee18c8195c h1:+aPplBwWcHBo6q9xrfWdMrT9o4kltkmmvpemgIjep/8= @@ -668,8 +674,8 @@ golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= -golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= 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-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -745,8 +751,8 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= -golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -931,6 +937,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 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= +helm.sh/helm v2.17.0+incompatible h1:cSe3FaQOpRWLDXvTObQNj0P7WI98IG5yloU6tQVls2k= +helm.sh/helm v2.17.0+incompatible/go.mod h1:0Xbc6ErzwWH9qC55X1+hE3ZwhM3atbhCm/NbFZw5i+4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= @@ -940,6 +948,10 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9 honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.4.6 h1:oFEHCKeID7to/3autwsWfnuv69j3NsfcXbvJKuIcep8= honnef.co/go/tools v0.4.6/go.mod h1:+rnGS1THNh8zMwnd2oVOTL9QF6vmfyG6ZXBULae2uc0= +k8s.io/apimachinery v0.30.3 h1:q1laaWCmrszyQuSQCfNB8cFgCuDAoPszKY4ucAjDwHc= +k8s.io/apimachinery v0.30.3/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc= +k8s.io/helm v2.17.0+incompatible h1:Bpn6o1wKLYqKM3+Osh8e+1/K2g/GsQJ4F4yNF2+deao= +k8s.io/helm v2.17.0+incompatible/go.mod h1:LZzlS4LQBHfciFOurYBFkCMTaZ0D1l+p0teMg7TSULI= mvdan.cc/gofumpt v0.6.0 h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo= mvdan.cc/gofumpt v0.6.0/go.mod h1:4L0wf+kgIPZtcCWXynNS2e6bhmj73umwnuXSZarixzA= mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed h1:WX1yoOaKQfddO/mLzdV4wptyWgoH/6hwLs7QHTixo0I= diff --git a/logx/facade.go b/logx/facade.go new file mode 100644 index 0000000..7308c8c --- /dev/null +++ b/logx/facade.go @@ -0,0 +1,71 @@ +package logx + +// Printf .. +func Printf(format string, args ...interface{}) { + LogSink.Infof(format, args...) +} + +// Debugf ... +func Debugf(format string, args ...interface{}) { + LogSink.Debugf(format, args...) +} + +// Infof ... +func Infof(format string, args ...interface{}) { + LogSink.Infof(format, args...) +} + +// Errorf ... +func Errorf(format string, args ...interface{}) { + LogSink.Errorf(format, args...) +} + +// Warnf ... +func Warnf(format string, args ...interface{}) { + LogSink.Warnf(format, args...) +} + +// Panicf ... +func Panicf(format string, args ...interface{}) { + LogSink.Panicf(format, args...) +} + +// Fatalf ... +func Fatalf(format string, args ...interface{}) { + LogSink.Fatalf(format, args...) +} + +// Debugw ... +func Debugw(msg string, keysAndValues ...interface{}) { + LogSink.Debugw(msg, keysAndValues...) +} + +// Infow ... +func Infow(msg string, keysAndValues ...interface{}) { + LogSink.Infow(msg, keysAndValues...) +} + +// Warnw ... +func Warnw(msg string, keysAndValues ...interface{}) { + LogSink.Warnw(msg, keysAndValues...) +} + +// Errorw ... +func Errorw(msg string, keysAndValues ...interface{}) { + LogSink.Errorw(msg, keysAndValues...) +} + +// DPanicw ... +func DPanicw(msg string, keysAndValues ...interface{}) { + LogSink.DPanicw(msg, keysAndValues...) +} + +// Panicw ... +func Panicw(msg string, keysAndValues ...interface{}) { + LogSink.Panicw(msg, keysAndValues...) +} + +// Fatalw ... +func Fatalw(msg string, keysAndValues ...interface{}) { + LogSink.Fatalw(msg, keysAndValues...) +} diff --git a/logx/logger.go b/logx/logger.go new file mode 100644 index 0000000..4461d57 --- /dev/null +++ b/logx/logger.go @@ -0,0 +1,251 @@ +package logx + +import ( + "sync" + + "go.uber.org/zap" +) + +// LogSink is the logger sink. +var LogSink Logger + +func init() { + l, err := NewLogSink() + if err != nil { + panic(err) + } + + log := NewLogger(WithLogger(l)) + + LogSink = log +} + +// NewLogSink returns a new logger sink. +func NewLogSink() (*zap.Logger, error) { + return zap.NewProduction() +} + +// Logger represents a standard logging interface. +type Logger interface { + // Log a notice statement + Noticef(format string, v ...interface{}) + + // Infof is logging an info statement. + Infof(format string, v ...interface{}) + + // Log a warning statement + Warnf(format string, v ...interface{}) + + // Log a fatal error + Fatalf(format string, v ...interface{}) + + // Log an error + Errorf(format string, v ...interface{}) + + // Log a debug statement + Debugf(format string, v ...interface{}) + + // Log a trace statement + Tracef(format string, v ...interface{}) + + // Panicf is logging a panic statement. + Panicf(format string, v ...interface{}) + + // Printf is logging a printf statement. + Printf(format string, v ...interface{}) + + // Debugw is logging a debug statement with context. + Debugw(msg string, keysAndValues ...interface{}) + + // Infow is logging an info statement with context. + Infow(msg string, keysAndValues ...interface{}) + + // Warnw is logging a warning statement with context. + Warnw(msg string, keysAndValues ...interface{}) + + // Errorw is logging an error statement with context. + Errorw(msg string, keysAndValues ...interface{}) + + // DPanicw is logging a debug panic statement with context. + DPanicw(msg string, keysAndValues ...interface{}) + + // Panicw is logging a panic statement with context. + Panicw(msg string, keysAndValues ...interface{}) + + // Fatalw is logging a fatal statement with context. + Fatalw(msg string, keysAndValues ...interface{}) +} + +var _ Logger = (*logger)(nil) + +// LogFunc is a bridge between Logger and any third party logger. +type LogFunc func(string, ...interface{}) + +// Printf is a bridge between Logger and any third party logger. +func (f LogFunc) Printf(msg string, args ...interface{}) { f(msg, args...) } + +type logger struct { + opts *Opts + sync.RWMutex +} + +// Opt is a logger option. +type Opt func(*Opts) + +// Opts are the options for the logger. +type Opts struct { + Logger *zap.Logger +} + +// Configure is configuring the logger. +func (o *Opts) Configure(opts ...Opt) { + for _, opt := range opts { + opt(o) + } +} + +// WithLogger is setting the logger. +func WithLogger(l *zap.Logger) Opt { + return func(o *Opts) { + o.Logger = l + } +} + +// NewLogger is creating a new logger. +func NewLogger(o ...Opt) Logger { + options := new(Opts) + options.Configure(o...) + + l := new(logger) + l.opts = options + + return l +} + +// Errorf is logging an error. +func (l *logger) Errorf(format string, v ...interface{}) { + l.logFunc(func(log *zap.Logger, format string, v ...interface{}) { + log.Sugar().Errorf(format, v...) + }, format, v...) +} + +// Debugf is logging a debug statement. +func (l *logger) Debugf(format string, v ...interface{}) { + l.logFunc(func(log *zap.Logger, format string, v ...interface{}) { + log.Sugar().Debugf(format, v...) + }, format, v...) +} + +// Fatalf is logging a fatal error. +func (l *logger) Fatalf(format string, v ...interface{}) { + l.logFunc(func(log *zap.Logger, format string, v ...interface{}) { + log.Sugar().Fatalf(format, v...) + }, format, v...) +} + +// Noticef is logging a notice statement. +func (l *logger) Noticef(format string, v ...interface{}) { + l.logFunc(func(log *zap.Logger, format string, v ...interface{}) { + log.Sugar().Infof(format, v...) + }, format, v...) +} + +// Warnf is logging a warning statement. +func (l *logger) Warnf(format string, v ...interface{}) { + l.logFunc(func(log *zap.Logger, format string, v ...interface{}) { + log.Sugar().Warnf(format, v...) + }, format, v...) +} + +// Tracef is logging a trace statement. +func (l *logger) Tracef(format string, v ...interface{}) { + l.logFunc(func(log *zap.Logger, format string, v ...interface{}) { + log.Sugar().Debugf(format, v...) + }, format, v...) +} + +// Infof is logging an info statement. +func (l *logger) Infof(format string, v ...interface{}) { + l.logFunc(func(log *zap.Logger, format string, v ...interface{}) { + log.Sugar().Infof(format, v...) + }, format, v...) +} + +// Panicf is logging a panic statement. +func (l *logger) Panicf(format string, v ...interface{}) { + l.logFunc(func(log *zap.Logger, format string, v ...interface{}) { + log.Sugar().Panicf(format, v...) + }, format, v...) +} + +// Printf is logging a printf statement. +func (l *logger) Printf(format string, v ...interface{}) { + l.logFunc(func(log *zap.Logger, format string, v ...interface{}) { + log.Sugar().Infof(format, v...) + }, format, v...) +} + +// Debugw is logging a debug statement with context. +func (l *logger) Debugw(msg string, keysAndValues ...interface{}) { + l.logFunc(func(log *zap.Logger, msg string, keysAndValues ...interface{}) { + log.Sugar().Debugw(msg, keysAndValues...) + }, msg, keysAndValues...) +} + +// Infow is logging an info statement with context. +func (l *logger) Infow(msg string, keysAndValues ...interface{}) { + l.logFunc(func(log *zap.Logger, msg string, keysAndValues ...interface{}) { + log.Sugar().Infow(msg, keysAndValues...) + }, msg, keysAndValues...) +} + +// Warnw is logging a warning statement with context. +func (l *logger) Warnw(msg string, keysAndValues ...interface{}) { + l.logFunc(func(log *zap.Logger, msg string, keysAndValues ...interface{}) { + log.Sugar().Warnw(msg, keysAndValues...) + }, msg, keysAndValues...) +} + +// Errorw is logging an error statement with context. +func (l *logger) Errorw(msg string, keysAndValues ...interface{}) { + l.logFunc(func(log *zap.Logger, msg string, keysAndValues ...interface{}) { + log.Sugar().Errorw(msg, keysAndValues...) + }, msg, keysAndValues...) +} + +// DPanicw is logging a debug panic statement with context. +func (l *logger) DPanicw(msg string, keysAndValues ...interface{}) { + l.logFunc(func(log *zap.Logger, msg string, keysAndValues ...interface{}) { + log.Sugar().DPanicw(msg, keysAndValues...) + }, msg, keysAndValues...) +} + +// Panicw is logging a panic statement with context. +func (l *logger) Panicw(msg string, keysAndValues ...interface{}) { + l.logFunc(func(log *zap.Logger, msg string, keysAndValues ...interface{}) { + log.Sugar().Panicw(msg, keysAndValues...) + }, msg, keysAndValues...) +} + +// Fatalw is logging a fatal statement with context. +func (l *logger) Fatalw(msg string, keysAndValues ...interface{}) { + l.logFunc(func(log *zap.Logger, msg string, keysAndValues ...interface{}) { + log.Sugar().Fatalw(msg, keysAndValues...) + }, msg, keysAndValues...) +} + +func (l *logger) logFunc(f func(log *zap.Logger, format string, v ...interface{}), format string, args ...interface{}) { + l.Lock() + defer l.Unlock() + + if l.opts.Logger == nil { + return + } + + f(l.opts.Logger, format, args...) +} + +// RedirectStdLog is redirecting the standard logger to the logger. +func RedirectStdLog(l Logger) (func(), error) { + return zap.RedirectStdLogAt(l.(*logger).opts.Logger, zap.DebugLevel) +} diff --git a/logx/logger_test.go b/logx/logger_test.go new file mode 100644 index 0000000..595eed1 --- /dev/null +++ b/logx/logger_test.go @@ -0,0 +1,25 @@ +package logx_test + +import ( + "testing" + + "github.com/zeiss/pkg/logx" + + "github.com/stretchr/testify/assert" +) + +func TestFacade(t *testing.T) { + logx.Printf("test %q", "print") + + logx.Debugf("test %q", "debug") + logx.Infof("test %q", "info") + logx.Warnf("test %q", "warn") + assert.Panics(t, func() { logx.Panicf("test %q", "panic") }) + logx.Errorf("test %q", "error") + + logx.Debugw("test", "some", "debug") + logx.Infow("test", "some", "info") + logx.Warnw("test", "some", "warn") + assert.Panics(t, func() { logx.Panicw("test", "some", "panic") }) + logx.Errorw("test", "some", "error") +} diff --git a/slices/doc.go b/slices/doc.go new file mode 100644 index 0000000..8a50e81 --- /dev/null +++ b/slices/doc.go @@ -0,0 +1,2 @@ +// A package to operate on slices. +package slices