Skip to content

Commit 0c3eefb

Browse files
authored
Merge pull request #25 from zerok/feature/zerolog
refactor: Replace logrus with zerolog
2 parents 325e79c + c1e1346 commit 0c3eefb

16 files changed

+106
-75
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ This tool wouldn't be possible (or at least would have been a lot harder to
250250
write) without the great work of the Go community. The following libraries are
251251
used:
252252

253-
* https://github.com/sirupsen/logrus
253+
* https://github.com/rs/zerolog
254254
* https://github.com/fatih/structs
255255
* https://github.com/golang/snappy
256256
* https://github.com/hashicorp/errwrap

cmd/tpl/licenses_generated.go

+1-1
Large diffs are not rendered by default.

cmd/tpl/main.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ package main
33

44
import (
55
"bytes"
6+
"context"
67
"encoding/csv"
78
"fmt"
89
"io"
910
"os"
1011

1112
"github.com/pkg/errors"
12-
"github.com/sirupsen/logrus"
13+
"github.com/rs/zerolog"
1314
"github.com/spf13/pflag"
1415
"github.com/zerok/tpl/internal/world"
1516
)
1617

1718
var version, commit, date string
1819

1920
func main() {
20-
log := logrus.New()
21+
logger := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger().Level(zerolog.InfoLevel)
2122
var input string
2223
var vaultPrefix string
2324
var showVersion bool
@@ -52,8 +53,9 @@ func main() {
5253
pflag.Parse()
5354

5455
if verbose {
55-
log.SetLevel(logrus.DebugLevel)
56+
logger = logger.Level(zerolog.DebugLevel)
5657
}
58+
ctx := logger.WithContext(context.Background())
5759

5860
if showVersion {
5961
fmt.Printf("Version: %s\nCommit: %s\nBuild date: %s\n", version, commit, date)
@@ -73,7 +75,7 @@ func main() {
7375

7476
input = pflag.Arg(0)
7577
if input == "" {
76-
log.Error("No input file provided")
78+
logger.Error().Msg("No input file provided")
7779
pflag.Usage()
7880
os.Exit(1)
7981
}
@@ -84,14 +86,13 @@ func main() {
8486
} else {
8587
fp, err := os.Open(input)
8688
if err != nil {
87-
log.WithError(err).Fatalf("Failed to open template %s", input)
89+
logger.Fatal().Err(err).Msgf("Failed to open template %s", input)
8890
}
8991
defer fp.Close()
9092
rd = fp
9193
}
9294

93-
w := world.New(&world.Options{
94-
Logger: log,
95+
w := world.New(ctx, &world.Options{
9596
Insecure: insecure,
9697
LeftDelim: leftDelim,
9798
RightDelim: rightDelim,
@@ -103,39 +104,39 @@ func main() {
103104
if vaultMapping != "" {
104105
vaultMap, err := loadKeyMapping(vaultMapping)
105106
if err != nil {
106-
log.WithError(err).Fatalf("Failed to load vault mapping file")
107+
logger.Fatal().Err(err).Msgf("Failed to load vault mapping file")
107108
}
108109
azureMap, err := loadKeyMapping(azureMapping)
109110
if err != nil {
110-
log.WithError(err).Fatalf("Failed to load azure mapping file")
111+
logger.Fatal().Err(err).Msgf("Failed to load azure mapping file")
111112
}
112113
w.Vault().KeyMapping = vaultMap
113114
w.Azure().KeyMapping = azureMap
114115
}
115116
wd, err := os.Getwd()
116117
if err != nil {
117-
log.WithError(err).Fatal("Failed to determine current working directory.")
118+
logger.Fatal().Err(err).Msg("Failed to determine current working directory.")
118119
}
119-
d, err := world.LoadData(data, wd)
120+
d, err := world.LoadData(ctx, data, wd)
120121
if err != nil {
121-
log.WithError(err).Fatalf("Failed to load data")
122+
logger.Fatal().Err(err).Msgf("Failed to load data")
122123
}
123124
w.Data = d
124125

125126
output := bytes.Buffer{}
126127
if err := w.Render(&output, rd); err != nil {
127-
log.WithError(err).Fatal("Failed to render")
128+
logger.Fatal().Err(err).Msg("Failed to render")
128129
}
129130
if outputFile == "" {
130131
io.Copy(os.Stdout, &output)
131132
} else {
132133
fp, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
133134
if err != nil {
134-
log.WithError(err).Fatal("Failed to open output file")
135+
logger.Fatal().Err(err).Msg("Failed to open output file")
135136
}
136137
defer fp.Close()
137138
if _, err := io.Copy(fp, &output); err != nil {
138-
log.WithError(err).Fatal("Failed to write to output file")
139+
logger.Fatal().Err(err).Msg("Failed to write to output file")
139140
}
140141
}
141142
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/imdario/mergo v0.3.12 // indirect
1212
github.com/jmespath/go-jmespath v0.4.0
1313
github.com/pkg/errors v0.9.1
14-
github.com/sirupsen/logrus v1.8.1
14+
github.com/rs/zerolog v1.24.0
1515
github.com/spf13/pflag v1.0.5
1616
github.com/stretchr/testify v1.7.0
1717
gopkg.in/yaml.v2 v2.4.0

go.sum

+25-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
1515
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
1616
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
1717
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
18+
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
1819
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1920
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2021
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -26,6 +27,7 @@ github.com/go-ldap/ldap/v3 v3.1.3/go.mod h1:3rbOH3jRS2u6jg2rJnKAMLE/xQyCKIveG2Sa
2627
github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
2728
github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw=
2829
github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
30+
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
2931
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
3032
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
3133
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -118,13 +120,14 @@ github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4
118120
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
119121
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
120122
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
123+
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
124+
github.com/rs/zerolog v1.24.0 h1:76ivFxmVSRs1u2wUwJVg5VZDYQgeH1JpoS6ndgr9Wy8=
125+
github.com/rs/zerolog v1.24.0/go.mod h1:7KHcEGe0QZPOm2IE4Kpb5rTh6n1h2hIgS5OOnu1rUaI=
121126
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
122127
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
123128
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
124129
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
125130
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
126-
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
127-
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
128131
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
129132
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
130133
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
@@ -136,37 +139,53 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
136139
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
137140
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
138141
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
142+
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
139143
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
140144
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
145+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
141146
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904 h1:bXoxMPcSLOq08zI3/c5dEBT6lE4eh+jOh886GHrn6V8=
142147
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
143148
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
149+
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
144150
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
145151
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
146152
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
147153
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
148-
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM=
154+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
149155
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
156+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
157+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
150158
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
151159
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
152160
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
153161
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
162+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
154163
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
155164
golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
156165
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
157166
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
158167
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
159168
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
160169
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
161-
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
162-
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
163170
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
164-
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
171+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
172+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
173+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
174+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
175+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
165176
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
177+
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
178+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
166179
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI=
167180
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
181+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
168182
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
169183
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
184+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
185+
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
186+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
187+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
188+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
170189
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
171190
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
172191
google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=

internal/world/azure.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package world
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io/ioutil"
@@ -11,7 +12,7 @@ import (
1112

1213
"github.com/hashicorp/go-retryablehttp"
1314
"github.com/pkg/errors"
14-
"github.com/sirupsen/logrus"
15+
"github.com/rs/zerolog"
1516
)
1617

1718
const (
@@ -47,7 +48,7 @@ type AzureSecretVersions struct {
4748
}
4849

4950
type Azure struct {
50-
logger *logrus.Logger
51+
ctx context.Context
5152
Prefix string
5253
KeyMapping map[string]string
5354
keyVaultUrl string
@@ -58,14 +59,14 @@ type Azure struct {
5859
token string
5960
}
6061

61-
// LeveledLogrus implements the retryablehttp LeveledLogger interface
62-
// to use logrus as logging backend. See:
62+
// LeveledZerolog implements the retryablehttp LeveledLogger interface
63+
// to use zerolog as logging backend. See:
6364
// https://github.com/hashicorp/go-retryablehttp/pull/101#issuecomment-735206810
64-
type LeveledLogrus struct {
65-
*logrus.Logger
65+
type LeveledZerolog struct {
66+
logger *zerolog.Logger
6667
}
6768

68-
func (l *LeveledLogrus) fields(keysAndValues ...interface{}) map[string]interface{} {
69+
func (l *LeveledZerolog) fields(keysAndValues ...interface{}) map[string]interface{} {
6970
fields := make(map[string]interface{})
7071

7172
for i := 0; i < len(keysAndValues)-1; i += 2 {
@@ -75,25 +76,27 @@ func (l *LeveledLogrus) fields(keysAndValues ...interface{}) map[string]interfac
7576
return fields
7677
}
7778

78-
func (l *LeveledLogrus) Error(msg string, keysAndValues ...interface{}) {
79-
l.WithFields(l.fields(keysAndValues...)).Error(msg)
79+
func (l *LeveledZerolog) Error(msg string, keysAndValues ...interface{}) {
80+
l.logger.Error().Fields(l.fields(keysAndValues...)).Msg(msg)
8081
}
8182

82-
func (l *LeveledLogrus) Info(msg string, keysAndValues ...interface{}) {
83-
l.WithFields(l.fields(keysAndValues...)).Info(msg)
83+
func (l *LeveledZerolog) Info(msg string, keysAndValues ...interface{}) {
84+
l.logger.Info().Fields(l.fields(keysAndValues...)).Msg(msg)
8485
}
85-
func (l *LeveledLogrus) Debug(msg string, keysAndValues ...interface{}) {
86-
l.WithFields(l.fields(keysAndValues...)).Debug(msg)
86+
func (l *LeveledZerolog) Debug(msg string, keysAndValues ...interface{}) {
87+
l.logger.Debug().Fields(l.fields(keysAndValues...)).Msg(msg)
8788
}
8889

89-
func (l *LeveledLogrus) Warn(msg string, keysAndValues ...interface{}) {
90-
l.WithFields(l.fields(keysAndValues...)).Warn(msg)
90+
func (l *LeveledZerolog) Warn(msg string, keysAndValues ...interface{}) {
91+
l.logger.Warn().Fields(l.fields(keysAndValues...)).Msg(msg)
9192
}
9293

9394
func (w *World) Azure() *Azure {
9495
if w.azure != nil {
9596
return w.azure
9697
}
98+
logger := zerolog.Ctx(w.ctx).With().Str("component", "Azure").Logger()
99+
ctx := logger.WithContext(w.ctx)
97100
tenantId := os.Getenv(AzureTenantId)
98101
azureClientId := os.Getenv(AzureClientId)
99102
azureClientSecret := os.Getenv(AzureClientSecret)
@@ -106,15 +109,15 @@ func (w *World) Azure() *Azure {
106109
}
107110

108111
if azureKeyVaultUrl == "" {
109-
w.logger.Warnf("%v not set.", AzureKeyVaultUrl)
112+
logger.Warn().Msgf("%v not set.", AzureKeyVaultUrl)
110113
}
111114

112115
if azureToken == "" && (tenantId == "" || azureClientId == "" || azureClientSecret == "") {
113-
w.logger.Warnf("%s or %s, %s, %s needs to be set", AzureToken, AzureTenantId, AzureClientId, AzureClientSecret)
116+
logger.Warn().Msgf("%s or %s, %s, %s needs to be set", AzureToken, AzureTenantId, AzureClientId, AzureClientSecret)
114117
}
115118

116119
w.azure = &Azure{
117-
logger: w.logger,
120+
ctx: ctx,
118121
KeyMapping: make(map[string]string),
119122
tenantId: tenantId,
120123
clientId: azureClientId,
@@ -178,6 +181,7 @@ func (a *Azure) getLatestSecretVersion(path string) (string, error) {
178181
}
179182

180183
func (a *Azure) doVaultRequest(urlPath string) ([]byte, error) {
184+
logger := zerolog.Ctx(a.ctx)
181185
if a.token == "" {
182186
if err := a.getBearerToken(); err != nil {
183187
return nil, errors.Wrap(err, "failed to retrieve token")
@@ -199,7 +203,7 @@ func (a *Azure) doVaultRequest(urlPath string) ([]byte, error) {
199203
r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.token))
200204

201205
retryClient := retryablehttp.NewClient()
202-
retryClient.Logger = &LeveledLogrus{a.logger}
206+
retryClient.Logger = &LeveledZerolog{logger}
203207
client := retryClient.StandardClient()
204208

205209
resp, err := client.Do(r)
@@ -214,6 +218,7 @@ func (a *Azure) doVaultRequest(urlPath string) ([]byte, error) {
214218
}
215219

216220
func (a *Azure) getBearerToken() error {
221+
logger := zerolog.Ctx(a.ctx)
217222
params := url.Values{}
218223
params.Set("grant_type", AzureClientCredentialsGrant)
219224
params.Set("client_id", a.clientId)
@@ -231,7 +236,7 @@ func (a *Azure) getBearerToken() error {
231236
}
232237

233238
retryClient := retryablehttp.NewClient()
234-
retryClient.Logger = &LeveledLogrus{a.logger}
239+
retryClient.Logger = &LeveledZerolog{logger}
235240
client := retryClient.StandardClient()
236241

237242
resp, err := client.Do(r)

internal/world/azure_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package world_test
22

33
import (
44
"bytes"
5+
"context"
56
"testing"
67

78
"github.com/stretchr/testify/require"
@@ -10,15 +11,15 @@ import (
1011

1112
func TestAzureSecret(t *testing.T) {
1213
t.Run("ok", func(t *testing.T) {
13-
w := world.New(nil)
14+
w := world.New(context.Background(), nil)
1415
var out bytes.Buffer
1516
in := bytes.NewBufferString(`{{ azure "secret/path" "value" }}`)
1617
err := w.Render(&out, in)
1718
t.Logf("[[[ %s ]]]", out.String())
1819
require.Error(t, err)
1920
})
2021
t.Run("invalid-token", func(t *testing.T) {
21-
w := world.New(nil)
22+
w := world.New(context.Background(), nil)
2223
var out bytes.Buffer
2324
in := bytes.NewBufferString(`{{ .Azure.Secret "secrets--path" | jsonToMap | jmsepathValue "nested.json" }}`)
2425
err := w.Render(&out, in)

internal/world/data.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package world
22

33
import (
4+
"context"
45
"encoding/json"
56
"io"
67
"io/ioutil"
@@ -16,7 +17,7 @@ import (
1617
type Data map[string]interface{}
1718

1819
// LoadData fills a newly created Data object based on the given definitions.
19-
func LoadData(datadefs []string, cwd string) (Data, error) {
20+
func LoadData(ctx context.Context, datadefs []string, cwd string) (Data, error) {
2021
result := Data{}
2122
for _, datadef := range datadefs {
2223
elems := strings.SplitN(datadef, "=", 2)

0 commit comments

Comments
 (0)