-
Notifications
You must be signed in to change notification settings - Fork 5
/
global.go
58 lines (53 loc) · 1.14 KB
/
global.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
package command
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"github.com/spf13/cobra"
)
func getEndpoints(cmd *cobra.Command) []string {
addrs, err := cmd.Flags().GetString("pd")
if err != nil {
cmd.Println("get pd address failed, should set flag with '-u'")
os.Exit(1)
}
eps := strings.Split(addrs, ",")
for i, ep := range eps {
if j := strings.Index(ep, "//"); j == -1 {
eps[i] = "//" + ep
}
}
return eps
}
// GetStoresInfo
func GetStoresInfo(cmd *cobra.Command) (*StoresInfo, error) {
addrs := getEndpoints(cmd)
res, err := http.Get(addrs[0] + "/pd/api/v1/stores")
if err != nil {
return nil, err
}
defer res.Body.Close()
var stores StoresInfo
err = json.NewDecoder(res.Body).Decode(&stores)
if err != nil {
fmt.Println(err)
return nil, err
}
return &stores, nil
}
func GetRegionsInfo(cmd *cobra.Command) (*RegionsInfo, error) {
addrs := getEndpoints(cmd)
res, err := http.Get(addrs[0] + "/pd/api/v1/regions")
if err != nil {
return nil, err
}
defer res.Body.Close()
var regions RegionsInfo
err = json.NewDecoder(res.Body).Decode(®ions)
if err != nil {
return nil, err
}
return ®ions, nil
}