|
123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package sysevent
-
- import (
- "bytes"
- "encoding/json"
- "fmt"
-
- "github.com/spf13/cobra"
- se "github.com/tmaxmax/go-sse"
- cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
- "gitlink.org.cn/cloudream/jcs-pub/jcsctl/cmd"
- )
-
- func init() {
- var opt watchOpt
- cmd := cobra.Command{
- Use: "watch",
- Args: cobra.ExactArgs(0),
- RunE: func(c *cobra.Command, args []string) error {
- ctx := cmd.GetCmdCtx(c)
- return watch(c, ctx, opt, args)
- },
- }
- SysEventCmd.AddCommand(&cmd)
- }
-
- type watchOpt struct {
- }
-
- func watch(c *cobra.Command, ctx *cmd.CommandContext, opt watchOpt, args []string) error {
- resp, err := ctx.Client.SysEvent().Watch(cliapi.SysEventWatch{})
- if err != nil {
- return fmt.Errorf("begin watch : %v", err)
- }
- defer resp.Stream.Close()
-
- se.Read(resp.Stream, nil)(func(e se.Event, err error) bool {
- buf := bytes.NewBuffer(nil)
- json.Indent(buf, []byte(e.Data), "", " ")
- fmt.Println(buf.String())
- return true
- })
-
- return nil
- }
|