----- observe command -----
(c *apiClient, sessionID string)
| 859 | // ----- observe command ----- |
| 860 | |
| 861 | func cmdObserve(c *apiClient, sessionID string) { |
| 862 | sess, err := c.getSession(sessionID) |
| 863 | if err != nil { |
| 864 | fmt.Printf(" error: %v\n", err) |
| 865 | return |
| 866 | } |
| 867 | |
| 868 | wsURI := c.observeWsURL(sessionID) |
| 869 | header := http.Header{} |
| 870 | if c.secret != "" { |
| 871 | header.Set("Authorization", "Bearer "+c.secret) |
| 872 | } |
| 873 | conn, _, wsErr := websocket.DefaultDialer.Dial(wsURI, header) |
| 874 | if wsErr != nil { |
| 875 | fmt.Printf(" ws connect failed: %v\n", wsErr) |
| 876 | return |
| 877 | } |
| 878 | defer conn.Close() |
| 879 | |
| 880 | agentName, _ := formatAgent(sess.UserAgent) |
| 881 | fmt.Printf("\n \033[35mObserving\033[0m session \033[36m%s\033[0m (%s) — press Ctrl+C to stop\n\n", sessionID, agentName) |
| 882 | |
| 883 | // Use a signal channel so Ctrl+C stops observe without killing the process. |
| 884 | sigCh := make(chan os.Signal, 1) |
| 885 | signal.Notify(sigCh, os.Interrupt) |
| 886 | defer signal.Stop(sigCh) |
| 887 | |
| 888 | done := make(chan struct{}) |
| 889 | go func() { |
| 890 | defer close(done) |
| 891 | for { |
| 892 | var event ObserveEvent |
| 893 | if err := conn.ReadJSON(&event); err != nil { |
| 894 | return |
| 895 | } |
| 896 | printObserveEvent(&event) |
| 897 | } |
| 898 | }() |
| 899 | |
| 900 | select { |
| 901 | case <-sigCh: |
| 902 | fmt.Println("\n (stopped observing)") |
| 903 | case <-done: |
| 904 | fmt.Println("\n (observe connection closed)") |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | func printObserveEvent(event *ObserveEvent) { |
| 909 | ts := event.Timestamp.Format("15:04:05") |
no test coverage detected