streamAndPrint calls StreamCommand and prints each chunk as it arrives.
(ctx context.Context, client localrpc.CommandServiceClient, sessionID, command string)
| 138 | |
| 139 | // streamAndPrint calls StreamCommand and prints each chunk as it arrives. |
| 140 | func streamAndPrint(ctx context.Context, client localrpc.CommandServiceClient, sessionID, command string) error { |
| 141 | sc, err := client.StreamCommand(ctx, &localrpc.ExecuteCommandRequest{ |
| 142 | SessionId: sessionID, |
| 143 | Command: command, |
| 144 | }) |
| 145 | if err != nil { |
| 146 | return fmt.Errorf("StreamCommand: %w", err) |
| 147 | } |
| 148 | |
| 149 | for { |
| 150 | resp, err := sc.Recv() |
| 151 | if err == io.EOF { |
| 152 | return nil |
| 153 | } |
| 154 | if err != nil { |
| 155 | // Context cancelled is normal (Ctrl+C). |
| 156 | if ctx.Err() != nil { |
| 157 | return nil |
| 158 | } |
| 159 | return fmt.Errorf("recv: %w", err) |
| 160 | } |
| 161 | fmt.Print(resp.Output) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // isStreamingCommand returns true if the command name matches a known streaming command. |
| 166 | func isStreamingCommand(command string) bool { |