Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request vitessio#7302 from tinyspeck/am_vtctld_cell_getters
Browse files Browse the repository at this point in the history
[vtctld] Migrate cell getters
  • Loading branch information
rohit-nayak-ps authored and ajm188 committed Feb 11, 2021
1 parent 86c518c commit 726c306
Show file tree
Hide file tree
Showing 8 changed files with 582 additions and 10 deletions.
74 changes: 71 additions & 3 deletions go/cmd/vtctldclient/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
package main

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

"github.com/spf13/cobra"

Expand All @@ -32,6 +32,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 All @@ -56,7 +71,7 @@ func commandFindAllShardsInKeyspace(cmd *cobra.Command, args []string) error {
return err
}

data, err := json.Marshal(&resp)
data, err := MarshalJSON(resp)
if err != nil {
return err
}
Expand All @@ -65,6 +80,51 @@ func commandFindAllShardsInKeyspace(cmd *cobra.Command, args []string) error {
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
}

func commandGetKeyspace(cmd *cobra.Command, args []string) error {
ks := cmd.Flags().Arg(0)
resp, err := client.GetKeyspace(commandCtx, &vtctldatapb.GetKeyspaceRequest{
Expand All @@ -86,13 +146,21 @@ 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
}

func init() {
rootCmd.AddCommand(findAllShardsInKeyspaceCmd)
rootCmd.AddCommand(getCellInfoNamesCmd)
rootCmd.AddCommand(getCellInfoCmd)
rootCmd.AddCommand(getCellsAliasesCmd)
rootCmd.AddCommand(getKeyspaceCmd)
rootCmd.AddCommand(getKeyspacesCmd)
}
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

0 comments on commit 726c306

Please sign in to comment.