| 1 |
// Copyright 2018 Yuya Nishihara <[email protected]> |
| 2 |
// |
| 3 |
// This software may be used and distributed according to the terms of the |
| 4 |
// GNU General Public License version 2 or any later version. |
| 5 |
|
| 6 |
//! Functions to run Mercurial command in cHg-aware command server. |
| 7 |
|
| 8 |
use std::io; |
| 9 |
use std::os::unix::io::AsRawFd; |
| 10 |
|
| 11 |
use bytes::Bytes; |
| 12 |
use tokio_hglib::codec::ChannelMessage; |
| 13 |
use tokio_hglib::Connection; |
| 14 |
use tokio_hglib::Protocol; |
| 15 |
|
| 16 |
use crate::attachio; |
| 17 |
use crate::message::CommandType; |
| 18 |
use crate::message::{self}; |
| 19 |
use crate::uihandler::SystemHandler; |
| 20 |
|
| 21 |
/// Runs the given Mercurial command in cHg-aware command server, and |
| 22 |
/// fetches the result code. |
| 23 |
/// |
| 24 |
/// This is a subset of tokio-hglib's `run_command()` with the additional |
| 25 |
/// SystemRequest support. |
| 26 |
pub async fn run_command( |
| 27 |
proto: &mut Protocol<impl Connection + AsRawFd>, |
| 28 |
handler: &mut impl SystemHandler, |
| 29 |
packed_args: impl Into<Bytes>, |
| 30 |
) -> io::Result<i32> { |
| 31 |
proto.send_command_with_args("runcommand", packed_args).await?; |
| 32 |
loop { |
| 33 |
match proto.fetch_response().await? { |
| 34 |
ChannelMessage::Data(b'r', data) => { |
| 35 |
return message::parse_result_code(data); |
| 36 |
} |
| 37 |
ChannelMessage::Data(..) => { |
| 38 |
// just ignores data sent to optional channel |
| 39 |
} |
| 40 |
ChannelMessage::InputRequest(..) |
| 41 |
| ChannelMessage::LineRequest(..) => { |
| 42 |
return Err(io::Error::new( |
| 43 |
io::ErrorKind::InvalidData, |
| 44 |
"unsupported request", |
| 45 |
)); |
| 46 |
} |
| 47 |
ChannelMessage::SystemRequest(data) => { |
| 48 |
let (cmd_type, cmd_spec) = message::parse_command_spec(data)?; |
| 49 |
match cmd_type { |
| 50 |
CommandType::Pager => { |
| 51 |
// server spins new command loop while pager request is |
| 52 |
// in progress, which can be terminated by "" command. |
| 53 |
let pin = handler.spawn_pager(&cmd_spec).await?; |
| 54 |
attachio::attach_io(proto, &io::stdin(), &pin, &pin) |
| 55 |
.await?; |
| 56 |
proto.send_command("").await?; // terminator |
| 57 |
} |
| 58 |
CommandType::System => { |
| 59 |
let code = handler.run_system(&cmd_spec).await?; |
| 60 |
let data = message::pack_result_code(code); |
| 61 |
proto.send_data(data).await?; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |