add watch cmd

This commit is contained in:
lyyyuna 2021-06-24 17:28:44 +08:00
parent 205247b8ec
commit edad1f4914
3 changed files with 55 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package cmd
import (
"github.com/qiniu/goc/v2/pkg/build"
"github.com/qiniu/goc/v2/pkg/config"
"github.com/spf13/cobra"
)
@ -13,6 +14,8 @@ var installCmd = &cobra.Command{
}
func init() {
installCmd.Flags().StringVarP(&config.GocConfig.Mode, "mode", "", "count", "coverage mode: set, count, atomic, watch")
installCmd.Flags().StringVarP(&config.GocConfig.Host, "host", "", "127.0.0.1:7777", "specify the host of the goc sever")
rootCmd.AddCommand(installCmd)
}

25
cmd/watch.go Normal file
View File

@ -0,0 +1,25 @@
package cmd
import (
"github.com/qiniu/goc/v2/pkg/config"
cli "github.com/qiniu/goc/v2/pkg/watch"
"github.com/spf13/cobra"
)
var watchCmd = &cobra.Command{
Use: "watch",
Short: "watch for profile's real time update",
Long: "watch for profile's real time update",
Example: "",
Run: watch,
}
func init() {
watchCmd.Flags().StringVarP(&config.GocConfig.Host, "host", "", "127.0.0.1:7777", "specify the host of the goc server")
rootCmd.AddCommand(watchCmd)
}
func watch(cmd *cobra.Command, args []string) {
cli.Watch()
}

27
pkg/watch/watch.go Normal file
View File

@ -0,0 +1,27 @@
package watch
import (
"fmt"
"github.com/gorilla/websocket"
"github.com/qiniu/goc/v2/pkg/config"
"github.com/qiniu/goc/v2/pkg/log"
)
func Watch() {
watchUrl := fmt.Sprintf("ws://%v/v2/cover/ws/watch", config.GocConfig.Host)
c, _, err := websocket.DefaultDialer.Dial(watchUrl, nil)
if err != nil {
log.Fatalf("cannot connect to goc server: %v", err)
}
defer c.Close()
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Fatalf("cannot read message: %v", err)
}
log.Infof("profile update: %v", string(message))
}
}