| @@ -3,8 +3,10 @@ package cmdline | |||||
| import ( | import ( | ||||
| "context" | "context" | ||||
| "fmt" | "fmt" | ||||
| "os" | |||||
| "github.com/spf13/cobra" | "github.com/spf13/cobra" | ||||
| "gitlink.org.cn/cloudream/common/utils/serder" | |||||
| "gitlink.org.cn/cloudream/storage/client/internal/config" | "gitlink.org.cn/cloudream/storage/client/internal/config" | ||||
| "gitlink.org.cn/cloudream/storage/common/pkgs/sysevent" | "gitlink.org.cn/cloudream/storage/common/pkgs/sysevent" | ||||
| ) | ) | ||||
| @@ -16,16 +18,17 @@ func init() { | |||||
| rootCmd.AddCommand(cmd) | rootCmd.AddCommand(cmd) | ||||
| outputJSON := rootCmd.Flags().BoolP("json", "j", false, "output in JSON format") | |||||
| cmd.AddCommand(&cobra.Command{ | cmd.AddCommand(&cobra.Command{ | ||||
| Use: "watch", | Use: "watch", | ||||
| Short: "Watch system events", | Short: "Watch system events", | ||||
| Run: func(cmd *cobra.Command, args []string) { | Run: func(cmd *cobra.Command, args []string) { | ||||
| watchSysEvent(GetCmdCtx(cmd)) | |||||
| watchSysEvent(*outputJSON) | |||||
| }, | }, | ||||
| }) | }) | ||||
| } | } | ||||
| func watchSysEvent(cmdCtx *CommandContext) { | |||||
| func watchSysEvent(outputJSON bool) { | |||||
| host, err := sysevent.NewWatcherHost(sysevent.ConfigFromMQConfig(config.Cfg().RabbitMQ)) | host, err := sysevent.NewWatcherHost(sysevent.ConfigFromMQConfig(config.Cfg().RabbitMQ)) | ||||
| if err != nil { | if err != nil { | ||||
| fmt.Println(err) | fmt.Println(err) | ||||
| @@ -34,7 +37,17 @@ func watchSysEvent(cmdCtx *CommandContext) { | |||||
| ch := host.Start() | ch := host.Start() | ||||
| host.AddWatcherFn(func(event sysevent.SysEvent) { | host.AddWatcherFn(func(event sysevent.SysEvent) { | ||||
| fmt.Println(event.String()) | |||||
| if outputJSON { | |||||
| data, err := serder.ObjectToJSON(event) | |||||
| if err != nil { | |||||
| fmt.Fprintf(os.Stderr, "serializing event: %v\n", err) | |||||
| return | |||||
| } | |||||
| fmt.Println(string(data)) | |||||
| } else { | |||||
| fmt.Println(event) | |||||
| } | |||||
| }) | }) | ||||
| for { | for { | ||||
| e, err := ch.Receive().Wait(context.Background()) | e, err := ch.Receive().Wait(context.Background()) | ||||
| @@ -45,16 +58,16 @@ func watchSysEvent(cmdCtx *CommandContext) { | |||||
| switch e := e.(type) { | switch e := e.(type) { | ||||
| case sysevent.PublishError: | case sysevent.PublishError: | ||||
| fmt.Printf("Publish error: %v\n", e.Err) | |||||
| fmt.Fprintf(os.Stderr, "Publish error: %v\n", e.Err) | |||||
| case sysevent.PublisherExited: | case sysevent.PublisherExited: | ||||
| if e.Err != nil { | if e.Err != nil { | ||||
| fmt.Printf("Publisher exited with error: %v\n", e.Err) | |||||
| fmt.Fprintf(os.Stderr, "Publisher exited with error: %v\n", e.Err) | |||||
| } | } | ||||
| return | return | ||||
| case sysevent.OtherError: | case sysevent.OtherError: | ||||
| fmt.Printf("Other error: %v\n", e.Err) | |||||
| fmt.Fprintf(os.Stderr, "Other error: %v\n", e.Err) | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||