-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_command.go
174 lines (159 loc) · 4.68 KB
/
store_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2016 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package command
import (
"fmt"
"net/http"
"path"
"strconv"
"github.com/spf13/cobra"
)
var (
storesPrefix = "pd/api/v1/stores"
storePrefix = "pd/api/v1/store/%s"
)
// NewStoreCommand return a stores subcommand of rootCmd
func NewStoreCommand() *cobra.Command {
s := &cobra.Command{
Use: `store [delete|label|weight] <store_id> [--jq="<query string>"]`,
Short: "show the store status",
Run: showStoreCommandFunc,
}
s.AddCommand(NewDeleteStoreCommand())
s.AddCommand(NewLabelStoreCommand())
s.AddCommand(NewSetStoreWeightCommand())
s.Flags().String("jq", "", "jq query")
return s
}
// NewDeleteStoreCommand return a delete subcommand of storeCmd
func NewDeleteStoreCommand() *cobra.Command {
d := &cobra.Command{
Use: "delete <store_id>",
Short: "delete the store",
Run: deleteStoreCommandFunc,
}
return d
}
// NewLabelStoreCommand returns a label subcommand of storeCmd.
func NewLabelStoreCommand() *cobra.Command {
l := &cobra.Command{
Use: "label <store_id> <key> <value>",
Short: "set a store's label value",
Run: labelStoreCommandFunc,
}
return l
}
// NewSetStoreWeightCommand returns a weight subcommand of storeCmd.
func NewSetStoreWeightCommand() *cobra.Command {
return &cobra.Command{
Use: "weight <store_id> <leader_weight> <region_weight>",
Short: "set a store's leader and region balance weight",
Run: setStoreWeightCommandFunc,
}
}
// NewStoresCommand returns a store subcommand of rootCmd
func NewStoresCommand() *cobra.Command {
s := &cobra.Command{
Use: `stores [remove-tombstone]`,
Short: "show the store status",
}
s.AddCommand(NewRemoveTombStoneCommand())
return s
}
// NewRemoveTombStoneCommand returns a tombstone subcommand of storesCmd.
func NewRemoveTombStoneCommand() *cobra.Command {
return &cobra.Command{
Use: "remove-tombstone",
Short: "remove tombstone record if only safe",
Run: removeTombStoneCommandFunc,
}
}
func showStoreCommandFunc(cmd *cobra.Command, args []string) {
prefix := storesPrefix
if len(args) == 1 {
if _, err := strconv.Atoi(args[0]); err != nil {
fmt.Println("store_id should be a number")
return
}
prefix = fmt.Sprintf(storePrefix, args[0])
}
r, err := doRequest(cmd, prefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get store: %s\n", err)
return
}
if flag := cmd.Flag("jq"); flag != nil && flag.Value.String() != "" {
printWithJQFilter(r, flag.Value.String())
return
}
fmt.Println(r)
}
func deleteStoreCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("Usage: store delete <store_id>")
return
}
if _, err := strconv.Atoi(args[0]); err != nil {
fmt.Println("store_id should be a number")
return
}
prefix := fmt.Sprintf(storePrefix, args[0])
_, err := doRequest(cmd, prefix, http.MethodDelete)
if err != nil {
fmt.Printf("Failed to delete store %s: %s\n", args[0], err)
return
}
fmt.Println("Success!")
}
func labelStoreCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 3 {
fmt.Println("Usage: store label <store_id> <key> <value>")
return
}
if _, err := strconv.Atoi(args[0]); err != nil {
fmt.Println("store_id should be a number")
return
}
prefix := fmt.Sprintf(path.Join(storePrefix, "label"), args[0])
postJSON(cmd, prefix, map[string]interface{}{args[1]: args[2]})
}
func setStoreWeightCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 3 {
fmt.Println("Usage: store weight <store_id> <leader_weight> <region_weight>")
return
}
leader, err := strconv.ParseFloat(args[1], 64)
if err != nil || leader < 0 {
fmt.Println("leader_weight should be a number that >= 0.")
return
}
region, err := strconv.ParseFloat(args[2], 64)
if err != nil || region < 0 {
fmt.Println("region_weight should be a number that >= 0")
return
}
prefix := fmt.Sprintf(path.Join(storePrefix, "weight"), args[0])
postJSON(cmd, prefix, map[string]interface{}{
"leader": leader,
"region": region,
})
}
func removeTombStoneCommandFunc(cmd *cobra.Command, args []string) {
prefix := path.Join(storesPrefix, "remove-tombstone")
_, err := doRequest(cmd, prefix, http.MethodDelete)
if err != nil {
cmd.Printf("Failed to remove tombstone store %s \n", err)
return
}
cmd.Println("Success!")
}