Skip to content

Commit 22daf7b

Browse files
committed
feat: call service method by call command
1 parent 992de96 commit 22daf7b

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

cmd/dubbogo-cli-v2/cmd/call.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package cmd
19+
20+
import (
21+
"github.com/dubbogo/tools/internal/client"
22+
"github.com/dubbogo/tools/internal/json_register"
23+
"log"
24+
25+
"github.com/spf13/cobra"
26+
)
27+
28+
// callCmd represents the call command
29+
var (
30+
callCmd = &cobra.Command{
31+
Use: "call",
32+
Short: "call a method",
33+
Long: "",
34+
Run: call,
35+
}
36+
)
37+
38+
var (
39+
host string
40+
port int
41+
protocolName string
42+
InterfaceID string
43+
version string
44+
group string
45+
method string
46+
sendObjFilePath string
47+
recvObjFilePath string
48+
timeout int
49+
)
50+
51+
func init() {
52+
rootCmd.AddCommand(callCmd)
53+
showCmd.Flags().String("r", "localhost", "")
54+
showCmd.Flags().Int("h", 8080, "")
55+
56+
showCmd.Flags().StringVarP(&host, "h", "", "localhost", "target server host")
57+
showCmd.Flags().IntVarP(&port, "p", "", 8080, "target server port")
58+
showCmd.Flags().StringVarP(&protocolName, "proto", "", "dubbo", "transfer protocol")
59+
showCmd.Flags().StringVarP(&InterfaceID, "i", "", "com", "target service registered interface")
60+
showCmd.Flags().StringVarP(&version, "v", "", "", "target service version")
61+
showCmd.Flags().StringVarP(&group, "g", "", "", "target service group")
62+
showCmd.Flags().StringVarP(&method, "method", "", "", "target method")
63+
showCmd.Flags().StringVarP(&sendObjFilePath, "sendObj", "", "", "json file path to define transfer struct")
64+
showCmd.Flags().StringVarP(&recvObjFilePath, "recvObj", "", "", "json file path to define receive struct")
65+
showCmd.Flags().IntVarP(&timeout, "timeout", "", 3000, "request timeout (ms)")
66+
}
67+
68+
func call(cmd *cobra.Command, args []string) {
69+
checkParam()
70+
reqPkg := json_register.RegisterStructFromFile(sendObjFilePath)
71+
recvPkg := json_register.RegisterStructFromFile(recvObjFilePath)
72+
73+
t, err := client.NewTelnetClient(host, port, protocolName, InterfaceID, version, group, method, reqPkg, timeout)
74+
if err != nil {
75+
panic(err)
76+
}
77+
t.ProcessRequests(recvPkg)
78+
t.Destroy()
79+
}
80+
81+
func checkParam() {
82+
if method == "" {
83+
log.Fatalln("-method value not fond")
84+
}
85+
if sendObjFilePath == "" {
86+
log.Fatalln("-sendObj value not found")
87+
}
88+
if recvObjFilePath == "" {
89+
log.Fatalln("-recObj value not found")
90+
}
91+
}

0 commit comments

Comments
 (0)