##// END OF EJS Templates
rust-chg: suppress panic while writing chg error to stderr...
Yuya Nishihara -
r40322:af52181f default
parent child Browse files
Show More
@@ -1,59 +1,59 b''
1 // Copyright 2018 Yuya Nishihara <yuya@tcha.org>
1 // Copyright 2018 Yuya Nishihara <yuya@tcha.org>
2 //
2 //
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 extern crate chg;
6 extern crate chg;
7 extern crate futures;
7 extern crate futures;
8 extern crate tokio;
8 extern crate tokio;
9 extern crate tokio_hglib;
9 extern crate tokio_hglib;
10
10
11 use chg::{ChgClientExt, ChgUiHandler};
11 use chg::{ChgClientExt, ChgUiHandler};
12 use chg::locator;
12 use chg::locator;
13 use chg::procutil;
13 use chg::procutil;
14 use futures::sync::oneshot;
14 use futures::sync::oneshot;
15 use std::env;
15 use std::env;
16 use std::io;
16 use std::io;
17 use std::process;
17 use std::process;
18 use tokio::prelude::*;
18 use tokio::prelude::*;
19 use tokio_hglib::UnixClient;
19 use tokio_hglib::UnixClient;
20
20
21 fn main() {
21 fn main() {
22 let code = run().unwrap_or_else(|err| {
22 let code = run().unwrap_or_else(|err| {
23 eprintln!("chg: abort: {}", err);
23 writeln!(io::stderr(), "chg: abort: {}", err).unwrap_or(());
24 255
24 255
25 });
25 });
26 process::exit(code);
26 process::exit(code);
27 }
27 }
28
28
29 fn run() -> io::Result<i32> {
29 fn run() -> io::Result<i32> {
30 let current_dir = env::current_dir()?;
30 let current_dir = env::current_dir()?;
31 let sock_path = locator::prepare_server_socket_path()?;
31 let sock_path = locator::prepare_server_socket_path()?;
32 let handler = ChgUiHandler::new();
32 let handler = ChgUiHandler::new();
33 let (result_tx, result_rx) = oneshot::channel();
33 let (result_tx, result_rx) = oneshot::channel();
34 let fut = UnixClient::connect(sock_path)
34 let fut = UnixClient::connect(sock_path)
35 .and_then(|client| {
35 .and_then(|client| {
36 client.set_current_dir(current_dir)
36 client.set_current_dir(current_dir)
37 })
37 })
38 .and_then(|client| {
38 .and_then(|client| {
39 client.attach_io(io::stdin(), io::stdout(), io::stderr())
39 client.attach_io(io::stdin(), io::stdout(), io::stderr())
40 })
40 })
41 .and_then(|client| {
41 .and_then(|client| {
42 let pid = client.server_spec().process_id.unwrap();
42 let pid = client.server_spec().process_id.unwrap();
43 let pgid = client.server_spec().process_group_id;
43 let pgid = client.server_spec().process_group_id;
44 procutil::setup_signal_handler_once(pid, pgid)?;
44 procutil::setup_signal_handler_once(pid, pgid)?;
45 Ok(client)
45 Ok(client)
46 })
46 })
47 .and_then(|client| {
47 .and_then(|client| {
48 client.run_command_chg(handler, env::args_os().skip(1))
48 client.run_command_chg(handler, env::args_os().skip(1))
49 })
49 })
50 .map(|(_client, _handler, code)| {
50 .map(|(_client, _handler, code)| {
51 procutil::restore_signal_handler_once()?;
51 procutil::restore_signal_handler_once()?;
52 Ok(code)
52 Ok(code)
53 })
53 })
54 .or_else(|err| Ok(Err(err))) // pass back error to caller
54 .or_else(|err| Ok(Err(err))) // pass back error to caller
55 .map(|res| result_tx.send(res).unwrap());
55 .map(|res| result_tx.send(res).unwrap());
56 tokio::run(fut);
56 tokio::run(fut);
57 result_rx.wait().unwrap_or(Err(io::Error::new(io::ErrorKind::Other,
57 result_rx.wait().unwrap_or(Err(io::Error::new(io::ErrorKind::Other,
58 "no exit code set")))
58 "no exit code set")))
59 }
59 }
General Comments 0
You need to be logged in to leave comments. Login now