Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vtctld] Migrate cell getters #7302

Merged
merged 3 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 73 additions & 4 deletions go/cmd/vtctldclient/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ limitations under the License.
package main

import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/spf13/cobra"

"vitess.io/vitess/go/vt/log"
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
"vitess.io/vitess/go/vt/topo/topoproto"

vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
)

var (
Expand All @@ -36,6 +37,21 @@ var (
Args: cobra.ExactArgs(1),
RunE: commandFindAllShardsInKeyspace,
}
getCellInfoNamesCmd = &cobra.Command{
Use: "GetCellInfoNames",
Args: cobra.NoArgs,
RunE: commandGetCellInfoNames,
}
getCellInfoCmd = &cobra.Command{
Use: "GetCellInfo cell",
Args: cobra.ExactArgs(1),
RunE: commandGetCellInfo,
}
getCellsAliasesCmd = &cobra.Command{
Use: "GetCellsAliases",
Args: cobra.NoArgs,
RunE: commandGetCellsAliases,
}
getKeyspaceCmd = &cobra.Command{
Use: "GetKeyspace keyspace",
Aliases: []string{"getkeyspace"},
Expand Down Expand Up @@ -65,12 +81,57 @@ func commandFindAllShardsInKeyspace(cmd *cobra.Command, args []string) error {
return err
}

data, err := json.Marshal(&resp)
data, err := MarshalJSON(resp)
if err != nil {
return err
}

fmt.Printf("%s\n", data)
return nil
}

func commandGetCellInfoNames(cmd *cobra.Command, args []string) error {
resp, err := client.GetCellInfoNames(commandCtx, &vtctldatapb.GetCellInfoNamesRequest{})
if err != nil {
return err
}

fmt.Printf("%s\n", strings.Join(resp.Names, "\n"))

return nil
}

func commandGetCellInfo(cmd *cobra.Command, args []string) error {
cell := cmd.Flags().Arg(0)
resp, err := client.GetCellInfo(commandCtx, &vtctldatapb.GetCellInfoRequest{Cell: cell})

if err != nil {
return err
}

data, err := MarshalJSON(resp.CellInfo)
if err != nil {
return err
}

fmt.Printf("%s\n", data)

return nil
}

func commandGetCellsAliases(cmd *cobra.Command, args []string) error {
resp, err := client.GetCellsAliases(commandCtx, &vtctldatapb.GetCellsAliasesRequest{})
if err != nil {
return err
}

data, err := MarshalJSON(resp.Aliases)
if err != nil {
return err
}

fmt.Printf("%s\n", data)

return nil
}

Expand All @@ -95,7 +156,12 @@ func commandGetKeyspaces(cmd *cobra.Command, args []string) error {
return err
}

fmt.Printf("%+v\n", resp.Keyspaces)
data, err := MarshalJSON(resp.Keyspaces)
if err != nil {
return err
}

fmt.Printf("%s\n", data)

return nil
}
Expand Down Expand Up @@ -133,6 +199,9 @@ func commandInitShardPrimary(cmd *cobra.Command, args []string) error {

func init() {
rootCmd.AddCommand(findAllShardsInKeyspaceCmd)
rootCmd.AddCommand(getCellInfoNamesCmd)
rootCmd.AddCommand(getCellInfoCmd)
rootCmd.AddCommand(getCellsAliasesCmd)
rootCmd.AddCommand(getKeyspaceCmd)
rootCmd.AddCommand(getKeyspacesCmd)

Expand Down
61 changes: 61 additions & 0 deletions go/cmd/vtctldclient/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2021 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"bytes"
"encoding/json"
"fmt"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)

// 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
// indented with two spaces for readability.
//
// Unfortunately jsonpb only works for types that implement proto.Message,
// 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 interface{}) ([]byte, error) {
switch obj := obj.(type) {
case proto.Message:
b := bytes.NewBuffer(nil)
m := jsonpb.Marshaler{
EnumsAsInts: false,
EmitDefaults: true,
Indent: " ",
OrigName: true,
}

if err := m.Marshal(b, obj); err != nil {
return nil, fmt.Errorf("jsonpb.Marshal = %v", err)
}

return b.Bytes(), nil
default:
data, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return nil, fmt.Errorf("json.Marshal = %v", err)
}

return data, nil
}
}
Loading