Skip to content

Commit

Permalink
use cobra print (#1313)
Browse files Browse the repository at this point in the history
  • Loading branch information
rleungx authored and nolouch committed Nov 8, 2018
1 parent 93d75e9 commit ae4fb7d
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 181 deletions.
5 changes: 2 additions & 3 deletions tools/pd-ctl/pdctl/command/cluster_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package command

import (
"fmt"
"net/http"

"github.com/spf13/cobra"
Expand All @@ -35,8 +34,8 @@ func NewClusterCommand() *cobra.Command {
func showClusterCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, clusterPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get the cluster information: %s\n", err)
cmd.Printf("Failed to get the cluster information: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}
49 changes: 24 additions & 25 deletions tools/pd-ctl/pdctl/command/config_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package command
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"path"
"strconv"
Expand Down Expand Up @@ -187,60 +186,60 @@ func NewDeleteLabelPropertyConfigCommand() *cobra.Command {
func showConfigCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, schedulePrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get config: %s\n", err)
cmd.Printf("Failed to get config: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}

func showReplicationConfigCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, replicationPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get config: %s\n", err)
cmd.Printf("Failed to get config: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}

func showLabelPropertyConfigCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, labelPropertyPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get config: %s\n", err)
cmd.Printf("Failed to get config: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}

func showAllConfigCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, configPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get config: %s\n", err)
cmd.Printf("Failed to get config: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}

func showNamespaceConfigCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println(cmd.UsageString())
cmd.Println(cmd.UsageString())
return
}
prefix := path.Join(namespacePrefix, args[0])
r, err := doRequest(cmd, prefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get config: %s\n", err)
cmd.Printf("Failed to get config: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}

func showClusterVersionCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, clusterVersionPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get cluster version: %s\n", err)
cmd.Printf("Failed to get cluster version: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}

func postConfigDataWithPath(cmd *cobra.Command, key, value, path string) error {
Expand Down Expand Up @@ -268,36 +267,36 @@ func postConfigDataWithPath(cmd *cobra.Command, key, value, path string) error {

func setConfigCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println(cmd.UsageString())
cmd.Println(cmd.UsageString())
return
}
opt, val := args[0], args[1]
err := postConfigDataWithPath(cmd, opt, val, configPrefix)
if err != nil {
fmt.Printf("Failed to set config: %s\n", err)
cmd.Printf("Failed to set config: %s\n", err)
return
}
fmt.Println("Success!")
cmd.Println("Success!")
}

func setNamespaceConfigCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 3 {
fmt.Println(cmd.UsageString())
cmd.Println(cmd.UsageString())
return
}
name, opt, val := args[0], args[1], args[2]
prefix := path.Join(namespacePrefix, name)
err := postConfigDataWithPath(cmd, opt, val, prefix)
if err != nil {
fmt.Printf("Failed to set namespace:%s config: %s\n", name, err)
cmd.Printf("Failed to set namespace:%s config: %s\n", name, err)
return
}
fmt.Println("Success!")
cmd.Println("Success!")
}

func deleteNamespaceConfigCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 && len(args) != 2 {
fmt.Println(cmd.UsageString())
cmd.Println(cmd.UsageString())
return
}
name, opt := args[0], args[1]
Expand All @@ -312,10 +311,10 @@ func deleteNamespaceConfigCommandFunc(cmd *cobra.Command, args []string) {
}

if err != nil {
fmt.Printf("Failed to delete namespace:%s config %s: %s\n", name, opt, err)
cmd.Printf("Failed to delete namespace:%s config %s: %s\n", name, opt, err)
return
}
fmt.Println("Success!")
cmd.Println("Success!")
}

func setLabelPropertyConfigCommandFunc(cmd *cobra.Command, args []string) {
Expand All @@ -328,7 +327,7 @@ func deleteLabelPropertyConfigCommandFunc(cmd *cobra.Command, args []string) {

func postLabelProperty(cmd *cobra.Command, action string, args []string) {
if len(args) != 3 {
fmt.Println(cmd.UsageString())
cmd.Println(cmd.UsageString())
return
}
input := map[string]interface{}{
Expand All @@ -343,7 +342,7 @@ func postLabelProperty(cmd *cobra.Command, action string, args []string) {

func setClusterVersionCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println(cmd.UsageString())
cmd.Println(cmd.UsageString())
return
}
input := map[string]interface{}{
Expand Down
8 changes: 4 additions & 4 deletions tools/pd-ctl/pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ func genResponseError(r *http.Response) error {
func getAddressFromCmd(cmd *cobra.Command, prefix string) string {
p, err := cmd.Flags().GetString("pd")
if err != nil {
fmt.Println("Get pd address error,should set flag with '-u'")
cmd.Println("Get pd address error,should set flag with '-u'")
os.Exit(1)
}

u, err := url.Parse(p)
if err != nil {
fmt.Println("address is wrong format,should like 'http://127.0.0.1:2379'")
cmd.Println("address is wrong format,should like 'http://127.0.0.1:2379'")
}
if u.Scheme == "" {
u.Scheme = "http"
Expand All @@ -124,14 +124,14 @@ func printResponseError(r *http.Response) {
func postJSON(cmd *cobra.Command, prefix string, input map[string]interface{}) {
data, err := json.Marshal(input)
if err != nil {
fmt.Println(err)
cmd.Println(err)
return
}

url := getAddressFromCmd(cmd, prefix)
r, err := dialClient.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
fmt.Println(err)
cmd.Println(err)
return
}
defer r.Body.Close()
Expand Down
5 changes: 2 additions & 3 deletions tools/pd-ctl/pdctl/command/health_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package command

import (
"fmt"
"net/http"

"github.com/spf13/cobra"
Expand All @@ -37,8 +36,8 @@ func NewHealthCommand() *cobra.Command {
func showHealthCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, healthPrefix, http.MethodGet)
if err != nil {
fmt.Println(err)
cmd.Println(err)
return
}
fmt.Println(r)
cmd.Println(r)
}
13 changes: 6 additions & 7 deletions tools/pd-ctl/pdctl/command/hot_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package command

import (
"fmt"
"net/http"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -51,10 +50,10 @@ func NewHotWriteRegionCommand() *cobra.Command {
func showHotWriteRegionsCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, hotWriteRegionsPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get hotspot: %s\n", err)
cmd.Printf("Failed to get hotspot: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}

// NewHotReadRegionCommand return a hot read regions subcommand of hotSpotCmd
Expand All @@ -70,10 +69,10 @@ func NewHotReadRegionCommand() *cobra.Command {
func showHotReadRegionsCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, hotReadRegionsPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get hotspot: %s\n", err)
cmd.Printf("Failed to get hotspot: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}

// NewHotStoreCommand return a hot stores subcommand of hotSpotCmd
Expand All @@ -89,8 +88,8 @@ func NewHotStoreCommand() *cobra.Command {
func showHotStoresCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, hotStoresPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get hotspot: %s\n", err)
cmd.Printf("Failed to get hotspot: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}
10 changes: 5 additions & 5 deletions tools/pd-ctl/pdctl/command/label_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func NewLabelListStoresCommand() *cobra.Command {
func showLabelsCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, labelsPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get labels: %s\n", err)
cmd.Printf("Failed to get labels: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}

func getValue(args []string, i int) string {
Expand All @@ -64,16 +64,16 @@ func getValue(args []string, i int) string {

func showLabelListStoresCommandFunc(cmd *cobra.Command, args []string) {
if len(args) > 2 {
fmt.Println("Usage: label store name [value]")
cmd.Println("Usage: label store name [value]")
return
}
namePrefix := fmt.Sprintf("name=%s", getValue(args, 0))
valuePrefix := fmt.Sprintf("value=%s", getValue(args, 1))
prefix := fmt.Sprintf("%s?%s&%s", labelsStorePrefix, namePrefix, valuePrefix)
r, err := doRequest(cmd, prefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get stores through label: %s\n", err)
cmd.Printf("Failed to get stores through label: %s\n", err)
return
}
fmt.Println(r)
cmd.Println(r)
}
11 changes: 5 additions & 6 deletions tools/pd-ctl/pdctl/command/log_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package command
import (
"bytes"
"encoding/json"
"fmt"
"net/http"

"github.com/spf13/cobra"
Expand All @@ -39,24 +38,24 @@ func NewLogCommand() *cobra.Command {
func logCommandFunc(cmd *cobra.Command, args []string) {
var err error
if len(args) != 1 {
fmt.Println(cmd.UsageString())
cmd.Println(cmd.UsageString())
return
}

data, err := json.Marshal(args[0])
if err != nil {
fmt.Printf("Failed to set log level: %s\n", err)
cmd.Printf("Failed to set log level: %s\n", err)
return
}
req, err := getRequest(cmd, logPrefix, http.MethodPost, "application/json", bytes.NewBuffer(data))
if err != nil {
fmt.Printf("Failed to set log level: %s\n", err)
cmd.Printf("Failed to set log level: %s\n", err)
return
}
_, err = dail(req)
if err != nil {
fmt.Printf("Failed to set log level: %s\n", err)
cmd.Printf("Failed to set log level: %s\n", err)
return
}
fmt.Println("Success!")
cmd.Println("Success!")
}
Loading

0 comments on commit ae4fb7d

Please sign in to comment.