diff --git a/cmd/install.go b/cmd/install.go index 24715fa..d027474 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -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) } diff --git a/cmd/watch.go b/cmd/watch.go new file mode 100644 index 0000000..427e88b --- /dev/null +++ b/cmd/watch.go @@ -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() +} diff --git a/pkg/watch/watch.go b/pkg/watch/watch.go new file mode 100644 index 0000000..4177260 --- /dev/null +++ b/pkg/watch/watch.go @@ -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)) + } +}