Skip to content

Commit 8cc1839

Browse files
authored
VReplication: Add missing info to vtctldclient workflow SHOW output (#14225)
Signed-off-by: Matt Lord <mattalord@gmail.com>
1 parent e9de65c commit 8cc1839

File tree

12 files changed

+3239
-2181
lines changed

12 files changed

+3239
-2181
lines changed

go/cmd/vtctldclient/cli/json.go

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ const (
3030
jsonPrefix = ""
3131
)
3232

33+
var DefaultMarshalOptions = protojson.MarshalOptions{
34+
Multiline: true,
35+
Indent: jsonIndent,
36+
UseEnumNumbers: false,
37+
UseProtoNames: true,
38+
EmitUnpopulated: true, // Can be set to false via the --compact flag
39+
}
40+
3341
// MarshalJSON marshals obj to a JSON string. It uses the jsonpb marshaler for
3442
// proto.Message types, with some sensible defaults, and falls back to the
3543
// standard Go marshaler otherwise. In both cases, the marshaled JSON is
@@ -39,16 +47,19 @@ const (
3947
// either by being a proto message type or by anonymously embedding one, so for
4048
// other types that may have nested struct fields, we still use the standard Go
4149
// marshaler, which will result in different formattings.
42-
func MarshalJSON(obj any) ([]byte, error) {
50+
func MarshalJSON(obj any, marshalOptions ...protojson.MarshalOptions) ([]byte, error) {
4351
switch obj := obj.(type) {
4452
case proto.Message:
45-
m := protojson.MarshalOptions{
46-
Multiline: true,
47-
Indent: jsonIndent,
48-
UseEnumNumbers: true,
49-
UseProtoNames: true,
50-
EmitUnpopulated: true,
53+
m := DefaultMarshalOptions
54+
switch len(marshalOptions) {
55+
case 0: // Use default
56+
case 1: // Use provided one
57+
m = marshalOptions[0]
58+
default:
59+
return nil, fmt.Errorf("there should only be one optional MarshalOptions value but we had %d",
60+
len(marshalOptions))
5161
}
62+
5263
return m.Marshal(obj)
5364
default:
5465
data, err := json.MarshalIndent(obj, jsonPrefix, jsonIndent)
@@ -60,25 +71,10 @@ func MarshalJSON(obj any) ([]byte, error) {
6071
}
6172
}
6273

63-
// MarshalJSONPretty works the same as MarshalJSON but elides zero value
64-
// elements and uses ENUM names instead of numbers.
74+
// MarshalJSONPretty works the same as MarshalJSON but uses ENUM names
75+
// instead of numbers.
6576
func MarshalJSONPretty(obj any) ([]byte, error) {
66-
switch obj := obj.(type) {
67-
case proto.Message:
68-
m := protojson.MarshalOptions{
69-
Multiline: true,
70-
Indent: jsonIndent,
71-
UseEnumNumbers: false,
72-
UseProtoNames: true,
73-
EmitUnpopulated: false, // elide zero value elements
74-
}
75-
return m.Marshal(obj)
76-
default:
77-
data, err := json.MarshalIndent(obj, jsonPrefix, jsonIndent)
78-
if err != nil {
79-
return nil, fmt.Errorf("json.Marshal = %v", err)
80-
}
81-
82-
return data, nil
83-
}
77+
marshalOptions := DefaultMarshalOptions
78+
marshalOptions.UseEnumNumbers = false
79+
return MarshalJSON(obj, marshalOptions)
8480
}

go/cmd/vtctldclient/command/root.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"vitess.io/vitess/go/vt/vtctl/vtctldclient"
3333

3434
// These imports ensure init()s within them get called and they register their commands/subcommands.
35+
"vitess.io/vitess/go/cmd/vtctldclient/cli"
3536
vreplcommon "vitess.io/vitess/go/cmd/vtctldclient/command/vreplication/common"
3637
_ "vitess.io/vitess/go/cmd/vtctldclient/command/vreplication/lookupvindex"
3738
_ "vitess.io/vitess/go/cmd/vtctldclient/command/vreplication/materialize"
@@ -55,6 +56,7 @@ var (
5556

5657
server string
5758
actionTimeout time.Duration
59+
compactOutput bool
5860

5961
// Root is the main entrypoint to the vtctldclient CLI.
6062
Root = &cobra.Command{
@@ -71,6 +73,9 @@ var (
7173
ctx = context.Background()
7274
}
7375
commandCtx, commandCancel = context.WithTimeout(ctx, actionTimeout)
76+
if compactOutput {
77+
cli.DefaultMarshalOptions.EmitUnpopulated = false
78+
}
7479
vreplcommon.SetClient(client)
7580
vreplcommon.SetCommandCtx(commandCtx)
7681
return err
@@ -151,7 +156,8 @@ func getClientForCommand(cmd *cobra.Command) (vtctldclient.VtctldClient, error)
151156
}
152157

153158
func init() {
154-
Root.PersistentFlags().StringVar(&server, "server", "", "server to use for connection (required)")
155-
Root.PersistentFlags().DurationVar(&actionTimeout, "action_timeout", time.Hour, "timeout for the total command")
159+
Root.PersistentFlags().StringVar(&server, "server", "", "server to use for the connection (required)")
160+
Root.PersistentFlags().DurationVar(&actionTimeout, "action_timeout", time.Hour, "timeout to use for the command")
161+
Root.PersistentFlags().BoolVar(&compactOutput, "compact", false, "use compact format for otherwise verbose outputs")
156162
vreplcommon.RegisterCommands(Root)
157163
}

go/cmd/vtctldclient/command/vreplication/workflow/show.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func commandShow(cmd *cobra.Command, args []string) error {
6767

6868
var data []byte
6969
if strings.ToLower(cmd.Name()) == "list" {
70-
// We only want the names
70+
// We only want the names.
7171
Names := make([]string, len(resp.Workflows))
7272
for i, wf := range resp.Workflows {
7373
Names[i] = wf.Name
@@ -79,7 +79,7 @@ func commandShow(cmd *cobra.Command, args []string) error {
7979
if err != nil {
8080
return err
8181
}
82-
fmt.Printf("%s\n", data)
82+
fmt.Println(string(data))
8383

8484
return nil
8585
}

go/flags/endtoend/vtctldclient.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ Available Commands:
100100
help Help about any command
101101

102102
Flags:
103-
--action_timeout duration timeout for the total command (default 1h0m0s)
103+
--action_timeout duration timeout to use for the command (default 1h0m0s)
104104
--alsologtostderr log to standard error as well as files
105+
--compact use compact format for otherwise verbose outputs
105106
--grpc_auth_static_client_creds string When using grpc_static_auth in the server, this file provides the credentials to use to authenticate with server.
106107
--grpc_compression string Which protocol to use for compressing gRPC. Default: nothing. Supported: snappy
107108
--grpc_enable_tracing Enable gRPC tracing.
@@ -121,7 +122,7 @@ Flags:
121122
--mysql_server_version string MySQL server version to advertise. (default "8.0.30-Vitess")
122123
--purge_logs_interval duration how often try to remove old logs (default 1h0m0s)
123124
--security_policy string the name of a registered security policy to use for controlling access to URLs - empty means allow all for anyone (built-in policies: deny-all, read-only)
124-
--server string server to use for connection (required)
125+
--server string server to use for the connection (required)
125126
--stderrthreshold severity logs at or above this threshold go to stderr (default 1)
126127
-v, --v Level log level for V logs
127128
--version version for vtctldclient

0 commit comments

Comments
 (0)