Skip to content

Commit

Permalink
Merge pull request #29 from cjphaha/feature/cjphaha_dubbogo_cli-v2
Browse files Browse the repository at this point in the history
Ftr: dubbogo-cli-v2 support get interfaces and methods form remote
  • Loading branch information
LaurenceLiZhixin authored Jan 27, 2022
2 parents 9b9059f + a56ed31 commit 90eb50d
Show file tree
Hide file tree
Showing 23 changed files with 1,465 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cmd/dubbogo-cli-v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# dubbogo-cli-v2

> dubbo-go integration tool
## How to use

1. Install
```bash
go get -u github.com/dubbogo/tools/cmd/dubbogo-cli-v2
```
## The main function

### Get a list of interfaces and methods

```bash
./dubbogo-cli-v2 show --r zookeeper --h 127.0.0.1:2181
```
The output is as follows

```bash
interface: org.apache.dubbo.game.basketballService
methods: []
interface: com.apache.dubbo.sample.basic.IGreeter
methods: []
interface: com.dubbogo.pixiu.UserService
methods: [CreateUser,GetUserByCode,GetUserByName,GetUserByNameAndAge,GetUserTimeout,UpdateUser,UpdateUserByName]
interface: org.apache.dubbo.gate.basketballService
methods: []
interface: org.apache.dubbo.game.basketballService
methods: []
interface: com.apache.dubbo.sample.basic.IGreeter
methods: []
interface: com.dubbogo.pixiu.UserService
methods: [CreateUser,GetUserByCode,GetUserByName,GetUserByNameAndAge,GetUserTimeout,UpdateUser,UpdateUserByName]
interface: org.apache.dubbo.gate.basketballService
methods: []

```

### Create demo

```bash
./dubbogo-cli-v2 new --path=./demo
```

This command will generate a dubbo-go example, you can refer to the example [HOWTO](https://github.com/apache/dubbo-go-samples/blob/master/HOWTO.md) to run.

![img.png](docs/demo/img.png)
48 changes: 48 additions & 0 deletions cmd/dubbogo-cli-v2/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# dubbogo-cli-v2

> dubbo-go 集成工具
## 使用方式

1. 安装
```bash
go get -u github.com/dubbogo/tools/cmd/dubbogo-cli-v2
```
## 主要功能

### 获取接口及方法列表

```bash
./dubbogo-cli-v2 show --r zookeeper --h 127.0.0.1:2181
```
输出如下

```bash
interface: org.apache.dubbo.game.basketballService
methods: []
interface: com.apache.dubbo.sample.basic.IGreeter
methods: []
interface: com.dubbogo.pixiu.UserService
methods: [CreateUser,GetUserByCode,GetUserByName,GetUserByNameAndAge,GetUserTimeout,UpdateUser,UpdateUserByName]
interface: org.apache.dubbo.gate.basketballService
methods: []
interface: org.apache.dubbo.game.basketballService
methods: []
interface: com.apache.dubbo.sample.basic.IGreeter
methods: []
interface: com.dubbogo.pixiu.UserService
methods: [CreateUser,GetUserByCode,GetUserByName,GetUserByNameAndAge,GetUserTimeout,UpdateUser,UpdateUserByName]
interface: org.apache.dubbo.gate.basketballService
methods: []

```

### 创建 demo

```bash
./dubbogo-cli-v2 new --path=./demo
```

该命令会生成一个 dubbo-go 的样例,该样例可以参考 [HOWTO](https://github.com/apache/dubbo-go-samples/blob/master/HOWTO.md) 运行:

![img.png](docs/demo/img.png)
94 changes: 94 additions & 0 deletions cmd/dubbogo-cli-v2/cmd/call.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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,
* 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 cmd

import (
"log"
)

import (
"github.com/spf13/cobra"
)

import (
"github.com/dubbogo/tools/internal/client"
"github.com/dubbogo/tools/internal/json_register"
)

// callCmd represents the call command
var (
callCmd = &cobra.Command{
Use: "call",
Short: "call a method",
Long: "",
Run: call,
}
)

var (
host string
port int
protocolName string
InterfaceID string
version string
group string
method string
sendObjFilePath string
recvObjFilePath string
timeout int
)

func init() {
rootCmd.AddCommand(callCmd)

callCmd.Flags().StringVarP(&host, "h", "", "localhost", "target server host")
callCmd.Flags().IntVarP(&port, "p", "", 8080, "target server port")
callCmd.Flags().StringVarP(&protocolName, "proto", "", "dubbo", "transfer protocol")
callCmd.Flags().StringVarP(&InterfaceID, "i", "", "com", "target service registered interface")
callCmd.Flags().StringVarP(&version, "v", "", "", "target service version")
callCmd.Flags().StringVarP(&group, "g", "", "", "target service group")
callCmd.Flags().StringVarP(&method, "method", "", "", "target method")
callCmd.Flags().StringVarP(&sendObjFilePath, "sendObj", "", "", "json file path to define transfer struct")
callCmd.Flags().StringVarP(&recvObjFilePath, "recvObj", "", "", "json file path to define receive struct")
callCmd.Flags().IntVarP(&timeout, "timeout", "", 3000, "request timeout (ms)")
}

func call(_ *cobra.Command, _ []string) {
checkParam()
reqPkg := json_register.RegisterStructFromFile(sendObjFilePath)
recvPkg := json_register.RegisterStructFromFile(recvObjFilePath)

t, err := client.NewTelnetClient(host, port, protocolName, InterfaceID, version, group, method, reqPkg, timeout)
if err != nil {
panic(err)
}
t.ProcessRequests(recvPkg)
t.Destroy()
}

func checkParam() {
if method == "" {
log.Fatalln("-method value not fond")
}
if sendObjFilePath == "" {
log.Fatalln("-sendObj value not found")
}
if recvObjFilePath == "" {
log.Fatalln("-recObj value not found")
}
}
48 changes: 48 additions & 0 deletions cmd/dubbogo-cli-v2/cmd/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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,
* 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 cmd

import (
"github.com/spf13/cobra"
)

import (
"github.com/dubbogo/tools/cmd/dubbogo-cli-v2/generator/sample"
)

// newCmd represents the new command
var newCmd = &cobra.Command{
Use: "new",
Short: "new a dubbo-go demo project",
Run: createDemo,
}

func init() {
rootCmd.AddCommand(newCmd)
newCmd.Flags().String("path", "rootPath", "")
}

func createDemo(cmd *cobra.Command, _ []string) {
path, err := cmd.Flags().GetString("path")
if err != nil {
panic(err)
}
if err := sample.Generate(path); err != nil {
panic(err)
}
}
74 changes: 74 additions & 0 deletions cmd/dubbogo-cli-v2/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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,
* 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 cmd

import (
"fmt"
"os"
)

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "dubbogo-cli-v2",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("hello")
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}

func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dubbogo-cli-v2.yaml)")
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
home, err := os.UserHomeDir()
cobra.CheckErr(err)

viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".dubbogo-cli-v2")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
70 changes: 70 additions & 0 deletions cmd/dubbogo-cli-v2/cmd/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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,
* 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 cmd

import (
"fmt"
"log"
)

import (
"github.com/spf13/cobra"
)

import (
"github.com/dubbogo/tools/cmd/dubbogo-cli-v2/metadata"
_ "github.com/dubbogo/tools/cmd/dubbogo-cli-v2/metadata/zookeeper"
)

// showCmd represents the show command
var showCmd = &cobra.Command{
Use: "show",
Short: "show interfaces and methods",
Long: ``,
Run: show,
}

func init() {
rootCmd.AddCommand(showCmd)
showCmd.Flags().String("r", "r", "")
showCmd.Flags().String("h", "h", "")
}

func show(cmd *cobra.Command, _ []string) {
registry, err := cmd.Flags().GetString("r")
if err != nil {
panic(err)
}
host, err := cmd.Flags().GetString("h")
if err != nil {
panic(err)
}
fact, ok := metadata.GetFactory(registry)
if !ok {
log.Print("registry not support")
return
}
methodsMap, err := fact("dubbogo-cli", []string{host}).ShowChildren()
if err != nil {
panic(err)
}
for k, v := range methodsMap {
fmt.Printf("interface: %s\n", k)
fmt.Printf("methods: %v\n", v)
}
}
Binary file added cmd/dubbogo-cli-v2/docs/demo/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 90eb50d

Please sign in to comment.