Skip to content

Commit

Permalink
VReplication: Add missing info to vtctldclient workflow SHOW output (#…
Browse files Browse the repository at this point in the history
…14225)

Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord authored Oct 11, 2023
1 parent e9de65c commit 8cc1839
Show file tree
Hide file tree
Showing 12 changed files with 3,239 additions and 2,181 deletions.
50 changes: 23 additions & 27 deletions go/cmd/vtctldclient/cli/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ const (
jsonPrefix = ""
)

var DefaultMarshalOptions = protojson.MarshalOptions{
Multiline: true,
Indent: jsonIndent,
UseEnumNumbers: false,
UseProtoNames: true,
EmitUnpopulated: true, // Can be set to false via the --compact flag
}

// MarshalJSON marshals obj to a JSON string. It uses the jsonpb marshaler for
// proto.Message types, with some sensible defaults, and falls back to the
// standard Go marshaler otherwise. In both cases, the marshaled JSON is
Expand All @@ -39,16 +47,19 @@ const (
// either by being a proto message type or by anonymously embedding one, so for
// other types that may have nested struct fields, we still use the standard Go
// marshaler, which will result in different formattings.
func MarshalJSON(obj any) ([]byte, error) {
func MarshalJSON(obj any, marshalOptions ...protojson.MarshalOptions) ([]byte, error) {
switch obj := obj.(type) {
case proto.Message:
m := protojson.MarshalOptions{
Multiline: true,
Indent: jsonIndent,
UseEnumNumbers: true,
UseProtoNames: true,
EmitUnpopulated: true,
m := DefaultMarshalOptions
switch len(marshalOptions) {
case 0: // Use default
case 1: // Use provided one
m = marshalOptions[0]
default:
return nil, fmt.Errorf("there should only be one optional MarshalOptions value but we had %d",
len(marshalOptions))
}

return m.Marshal(obj)
default:
data, err := json.MarshalIndent(obj, jsonPrefix, jsonIndent)
Expand All @@ -60,25 +71,10 @@ func MarshalJSON(obj any) ([]byte, error) {
}
}

// MarshalJSONPretty works the same as MarshalJSON but elides zero value
// elements and uses ENUM names instead of numbers.
// MarshalJSONPretty works the same as MarshalJSON but uses ENUM names
// instead of numbers.
func MarshalJSONPretty(obj any) ([]byte, error) {
switch obj := obj.(type) {
case proto.Message:
m := protojson.MarshalOptions{
Multiline: true,
Indent: jsonIndent,
UseEnumNumbers: false,
UseProtoNames: true,
EmitUnpopulated: false, // elide zero value elements
}
return m.Marshal(obj)
default:
data, err := json.MarshalIndent(obj, jsonPrefix, jsonIndent)
if err != nil {
return nil, fmt.Errorf("json.Marshal = %v", err)
}

return data, nil
}
marshalOptions := DefaultMarshalOptions
marshalOptions.UseEnumNumbers = false
return MarshalJSON(obj, marshalOptions)
}
10 changes: 8 additions & 2 deletions go/cmd/vtctldclient/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"vitess.io/vitess/go/vt/vtctl/vtctldclient"

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

server string
actionTimeout time.Duration
compactOutput bool

// Root is the main entrypoint to the vtctldclient CLI.
Root = &cobra.Command{
Expand All @@ -71,6 +73,9 @@ var (
ctx = context.Background()
}
commandCtx, commandCancel = context.WithTimeout(ctx, actionTimeout)
if compactOutput {
cli.DefaultMarshalOptions.EmitUnpopulated = false
}
vreplcommon.SetClient(client)
vreplcommon.SetCommandCtx(commandCtx)
return err
Expand Down Expand Up @@ -151,7 +156,8 @@ func getClientForCommand(cmd *cobra.Command) (vtctldclient.VtctldClient, error)
}

func init() {
Root.PersistentFlags().StringVar(&server, "server", "", "server to use for connection (required)")
Root.PersistentFlags().DurationVar(&actionTimeout, "action_timeout", time.Hour, "timeout for the total command")
Root.PersistentFlags().StringVar(&server, "server", "", "server to use for the connection (required)")
Root.PersistentFlags().DurationVar(&actionTimeout, "action_timeout", time.Hour, "timeout to use for the command")
Root.PersistentFlags().BoolVar(&compactOutput, "compact", false, "use compact format for otherwise verbose outputs")
vreplcommon.RegisterCommands(Root)
}
4 changes: 2 additions & 2 deletions go/cmd/vtctldclient/command/vreplication/workflow/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func commandShow(cmd *cobra.Command, args []string) error {

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

return nil
}
5 changes: 3 additions & 2 deletions go/flags/endtoend/vtctldclient.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ Available Commands:
help Help about any command

Flags:
--action_timeout duration timeout for the total command (default 1h0m0s)
--action_timeout duration timeout to use for the command (default 1h0m0s)
--alsologtostderr log to standard error as well as files
--compact use compact format for otherwise verbose outputs
--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.
--grpc_compression string Which protocol to use for compressing gRPC. Default: nothing. Supported: snappy
--grpc_enable_tracing Enable gRPC tracing.
Expand All @@ -121,7 +122,7 @@ Flags:
--mysql_server_version string MySQL server version to advertise. (default "8.0.30-Vitess")
--purge_logs_interval duration how often try to remove old logs (default 1h0m0s)
--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)
--server string server to use for connection (required)
--server string server to use for the connection (required)
--stderrthreshold severity logs at or above this threshold go to stderr (default 1)
-v, --v Level log level for V logs
--version version for vtctldclient
Expand Down
Loading

0 comments on commit 8cc1839

Please sign in to comment.