comparison rust/chg/src/main.rs @ 44854:9fc9526e283a

rust-chg: modernize entry function Finally the entire build passes. There's a bug that run() no longer waits for the spawned pager, which will be fixed by the next patch. Differential Revision: https://phab.mercurial-scm.org/D8448
author Yuya Nishihara <yuya@tcha.org>
date Sat, 11 Apr 2020 02:21:06 +0900
parents 5ac5c25ea97b
children 4b0185841058
comparison
equal deleted inserted replaced
44853:a347a329e48d 44854:9fc9526e283a
3 // This software may be used and distributed according to the terms of the 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. 4 // GNU General Public License version 2 or any later version.
5 5
6 use chg::locator::{self, Locator}; 6 use chg::locator::{self, Locator};
7 use chg::procutil; 7 use chg::procutil;
8 use chg::{ChgClientExt, ChgUiHandler}; 8 use chg::ChgUiHandler;
9 use futures::sync::oneshot;
10 use std::env; 9 use std::env;
11 use std::io; 10 use std::io;
11 use std::io::Write;
12 use std::process; 12 use std::process;
13 use std::time::Instant; 13 use std::time::Instant;
14 use tokio::prelude::*;
15 14
16 struct DebugLogger { 15 struct DebugLogger {
17 start: Instant, 16 start: Instant,
18 } 17 }
19 18
65 255 64 255
66 }); 65 });
67 process::exit(code); 66 process::exit(code);
68 } 67 }
69 68
70 fn run(umask: u32) -> io::Result<i32> { 69 #[tokio::main]
70 async fn run(umask: u32) -> io::Result<i32> {
71 let mut loc = Locator::prepare_from_env()?; 71 let mut loc = Locator::prepare_from_env()?;
72 loc.set_early_args(locator::collect_early_args(env::args_os().skip(1))); 72 loc.set_early_args(locator::collect_early_args(env::args_os().skip(1)));
73 let handler = ChgUiHandler::new(); 73 let mut handler = ChgUiHandler::new();
74 let (result_tx, result_rx) = oneshot::channel(); 74 let mut client = loc.connect().await?;
75 let fut = loc 75 client
76 .connect() 76 .attach_io(&io::stdin(), &io::stdout(), &io::stderr())
77 .and_then(|(_, client)| client.attach_io(io::stdin(), io::stdout(), io::stderr())) 77 .await?;
78 .and_then(move |client| client.set_umask(umask)) 78 client.set_umask(umask).await?;
79 .and_then(|client| { 79 let pid = client.server_spec().process_id.unwrap();
80 let pid = client.server_spec().process_id.unwrap(); 80 let pgid = client.server_spec().process_group_id;
81 let pgid = client.server_spec().process_group_id; 81 procutil::setup_signal_handler_once(pid, pgid)?;
82 procutil::setup_signal_handler_once(pid, pgid)?; 82 let code = client
83 Ok(client) 83 .run_command_chg(&mut handler, env::args_os().skip(1))
84 }) 84 .await?;
85 .and_then(|client| client.run_command_chg(handler, env::args_os().skip(1))) 85 procutil::restore_signal_handler_once()?;
86 .map(|(_client, _handler, code)| { 86 Ok(code)
87 procutil::restore_signal_handler_once()?;
88 Ok(code)
89 })
90 .or_else(|err| Ok(Err(err))) // pass back error to caller
91 .map(|res| result_tx.send(res).unwrap());
92 tokio::run(fut);
93 result_rx.wait().unwrap_or(Err(io::Error::new(
94 io::ErrorKind::Other,
95 "no exit code set",
96 )))
97 } 87 }