Skip to content

Commit 06c3515

Browse files
author
Andrew Mason
authored
[cli] Migrate vterrors to pflag (vitessio#10957)
* Tidy imports, make logErrStacks private Signed-off-by: Andrew Mason <[email protected]> * Migrate vterrors to `pflag` Since every binary will involve errors in some way, shape, or form, I decided to just register the vterrors flags to all binaries, rather than naming each binary explicitly. This notably does the _inverse_ of the code samples in the [Mover's Guide][1], because `servenv` has `vterrors` in its dependency chain, so trying to import `servenv` from `vterrors` would cause an import cycle. This is the approach we should continue to use for any vitess package imported by `servenv`. [1]: see vitessio#10697 (comment)) Signed-off-by: Andrew Mason <[email protected]>
1 parent 7f25195 commit 06c3515

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

go/vt/servenv/servenv.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import (
4747
"vitess.io/vitess/go/netutil"
4848
"vitess.io/vitess/go/stats"
4949
"vitess.io/vitess/go/vt/log"
50+
"vitess.io/vitess/go/vt/vterrors"
5051

5152
// register the proper init and shutdown hooks for logging
5253
_ "vitess.io/vitess/go/vt/logutil"
@@ -232,8 +233,10 @@ func RunDefault() {
232233
}
233234

234235
var (
235-
flagHooksM sync.Mutex
236-
globalFlagHooks []func(*pflag.FlagSet)
236+
flagHooksM sync.Mutex
237+
globalFlagHooks = []func(*pflag.FlagSet){
238+
vterrors.RegisterFlags,
239+
}
237240
commandFlagHooks = map[string][]func(*pflag.FlagSet){}
238241
)
239242

go/vt/vterrors/errors_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ limitations under the License.
1717
package vterrors
1818

1919
import (
20+
"context"
2021
"errors"
2122
"fmt"
2223
"io"
2324
"reflect"
2425
"strings"
2526
"testing"
2627

27-
"context"
28-
2928
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
3029
)
3130

@@ -193,8 +192,8 @@ func TestStackFormat(t *testing.T) {
193192
assertContains(t, got, "middle", false)
194193
assertContains(t, got, "outer", false)
195194

196-
LogErrStacks = true
197-
defer func() { LogErrStacks = false }()
195+
logErrStacks = true
196+
defer func() { logErrStacks = false }()
198197
got = fmt.Sprintf("%v", err)
199198
assertContains(t, got, "innerMost", true)
200199
assertContains(t, got, "middle", true)
@@ -276,9 +275,9 @@ func TestWrapping(t *testing.T) {
276275
err3 := Wrapf(err2, "baz")
277276
errorWithoutStack := fmt.Sprintf("%v", err3)
278277

279-
LogErrStacks = true
278+
logErrStacks = true
280279
errorWithStack := fmt.Sprintf("%v", err3)
281-
LogErrStacks = false
280+
logErrStacks = false
282281

283282
assertEquals(t, err3.Error(), "baz: bar: foo")
284283
assertContains(t, errorWithoutStack, "foo", true)

go/vt/vterrors/vterrors.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,23 @@ limitations under the License.
8686
package vterrors
8787

8888
import (
89-
"flag"
89+
"context"
9090
"fmt"
9191
"io"
9292

93-
"context"
93+
"github.com/spf13/pflag"
9494

9595
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
9696
)
9797

98-
// LogErrStacks controls whether or not printing errors includes the
98+
// logErrStacks controls whether or not printing errors includes the
9999
// embedded stack trace in the output.
100-
var LogErrStacks bool
100+
var logErrStacks bool
101101

102-
func init() {
103-
flag.BoolVar(&LogErrStacks, "log_err_stacks", false, "log stack traces for errors")
102+
// RegisterFlags registers the command-line options that control vterror
103+
// behavior on the provided FlagSet.
104+
func RegisterFlags(fs *pflag.FlagSet) {
105+
fs.BoolVar(&logErrStacks, "log_err_stacks", false, "log stack traces for errors")
104106
}
105107

106108
// New returns an error with the supplied message.
@@ -153,7 +155,7 @@ func (f *fundamental) Format(s fmt.State, verb rune) {
153155
case 'v':
154156
panicIfError(io.WriteString(s, "Code: "+f.code.String()+"\n"))
155157
panicIfError(io.WriteString(s, f.msg+"\n"))
156-
if LogErrStacks {
158+
if logErrStacks {
157159
f.stack.Format(s, verb)
158160
}
159161
return
@@ -249,7 +251,7 @@ func (w *wrapping) Format(s fmt.State, verb rune) {
249251
if rune('v') == verb {
250252
panicIfError(fmt.Fprintf(s, "%v\n", w.Cause()))
251253
panicIfError(io.WriteString(s, w.msg))
252-
if LogErrStacks {
254+
if logErrStacks {
253255
w.stack.Format(s, verb)
254256
}
255257
return

0 commit comments

Comments
 (0)