-
Notifications
You must be signed in to change notification settings - Fork 499
/
common.go
172 lines (135 loc) · 4.66 KB
/
common.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
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* 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
*
* https://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 command
import (
"bytes"
"fmt"
"io"
"net/http"
yamljsontool "github.com/ghodss/yaml"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
type (
// GlobalFlags is the global flags for the whole client.
GlobalFlags struct {
Server string
OutputFormat string
}
// APIErr is the standard return of error.
APIErr struct {
Code int `yaml:"code"`
Message string `yaml:"message"`
}
)
// CommandlineGlobalFlags is the singleton of GlobalFlags.
var CommandlineGlobalFlags GlobalFlags
const (
apiURL = "/apis/v1"
healthURL = apiURL + "/healthz"
membersURL = apiURL + "/status/members"
memberURL = apiURL + "/status/members/%s"
objectKindsURL = apiURL + "/object-kinds"
objectsURL = apiURL + "/objects"
objectURL = apiURL + "/objects/%s"
statusObjectURL = apiURL + "/status/objects/%s"
statusObjectsURL = apiURL + "/status/objects"
wasmCodeURL = apiURL + "/wasm/code"
wasmDataURL = apiURL + "/wasm/data/%s/%s"
customDataKindURL = apiURL + "/customdatakinds"
customDataKindItemURL = apiURL + "/customdatakinds/%s"
customDataURL = apiURL + "/customdata/%s"
customDataItemURL = apiURL + "/customdata/%s/%s"
profileURL = apiURL + "/profile"
profileStartURL = apiURL + "/profile/start/%s"
profileStopURL = apiURL + "/profile/stop"
// MeshTenantsURL is the mesh tenant prefix.
MeshTenantsURL = apiURL + "/mesh/tenants"
// MeshTenantURL is the mesh tenant path.
MeshTenantURL = apiURL + "/mesh/tenants/%s"
// MeshServicesURL is mesh service prefix.
MeshServicesURL = apiURL + "/mesh/services"
// MeshServiceURL is the mesh service path.
MeshServiceURL = apiURL + "/mesh/services/%s"
// MeshServiceCanaryURL is the mesh service canary path.
MeshServiceCanaryURL = apiURL + "/mesh/services/%s/canary"
// MeshServiceResilienceURL is the mesh service resilience path.
MeshServiceResilienceURL = apiURL + "/mesh/services/%s/resilience"
// MeshServiceLoadBalanceURL is the mesh service load balance path.
MeshServiceLoadBalanceURL = apiURL + "/mesh/services/%s/loadbalance"
// MeshServiceOutputServerURL is the mesh service output server path.
MeshServiceOutputServerURL = apiURL + "/mesh/services/%s/outputserver"
// MeshServiceTracingsURL is the mesh service tracings path.
MeshServiceTracingsURL = apiURL + "/mesh/services/%s/tracings"
// MeshServiceMetricsURL is the mesh service metrics path.
MeshServiceMetricsURL = apiURL + "/mesh/services/%s/metrics"
// MeshServiceInstancesURL is the mesh service prefix.
MeshServiceInstancesURL = apiURL + "/mesh/serviceinstances"
// MeshServiceInstanceURL is the mesh service path.
MeshServiceInstanceURL = apiURL + "/mesh/serviceinstances/%s/%s"
// MeshIngressesURL is the mesh ingress prefix.
MeshIngressesURL = apiURL + "/mesh/ingresses"
// MeshIngressURL is the mesh ingress path.
MeshIngressURL = apiURL + "/mesh/ingresses/%s"
)
func makeURL(urlTemplate string, a ...interface{}) string {
return "https://" + CommandlineGlobalFlags.Server + fmt.Sprintf(urlTemplate, a...)
}
func successfulStatusCode(code int) bool {
return code >= 200 && code < 300
}
func handleRequest(httpMethod string, url string, reqBody []byte, cmd *cobra.Command) {
req, err := http.NewRequest(httpMethod, url, bytes.NewReader(reqBody))
if err != nil {
ExitWithError(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
ExitWithErrorf("%s failed: %v", cmd.Short, err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
ExitWithErrorf("%s failed: %v", cmd.Short, err)
}
if !successfulStatusCode(resp.StatusCode) {
msg := string(body)
apiErr := &APIErr{}
err = yaml.Unmarshal(body, apiErr)
if err == nil {
msg = apiErr.Message
}
ExitWithErrorf("%d: %s", apiErr.Code, msg)
}
if len(body) != 0 {
printBody(body)
}
}
func printBody(body []byte) {
var output []byte
switch CommandlineGlobalFlags.OutputFormat {
case "yaml":
output = body
case "json":
var err error
output, err = yamljsontool.YAMLToJSON(body)
if err != nil {
ExitWithErrorf("yaml %s to json failed: %v", body, err)
}
}
fmt.Printf("%s", output)
}