author | Yuya Nishihara <yuya@tcha.org> |
Sun, 07 Oct 2018 20:55:51 +0900 | |
changeset 40286 | af52181f71ff |
parent 40121 | 89742f1fa6cb |
child 40288 | 87c76e5f3427 |
permissions | -rw-r--r-- |
39968 | 1 |
// Copyright 2018 Yuya Nishihara <yuya@tcha.org> |
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 |
||
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
6 |
extern crate chg; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
7 |
extern crate futures; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
8 |
extern crate tokio; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
9 |
extern crate tokio_hglib; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
10 |
|
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
11 |
use chg::{ChgClientExt, ChgUiHandler}; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
12 |
use chg::locator; |
40121
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
13 |
use chg::procutil; |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
14 |
use futures::sync::oneshot; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
15 |
use std::env; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
16 |
use std::io; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
17 |
use std::process; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
18 |
use tokio::prelude::*; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
19 |
use tokio_hglib::UnixClient; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
20 |
|
39968 | 21 |
fn main() { |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
22 |
let code = run().unwrap_or_else(|err| { |
40286
af52181f71ff
rust-chg: suppress panic while writing chg error to stderr
Yuya Nishihara <yuya@tcha.org>
parents:
40121
diff
changeset
|
23 |
writeln!(io::stderr(), "chg: abort: {}", err).unwrap_or(()); |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
24 |
255 |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
25 |
}); |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
26 |
process::exit(code); |
39968 | 27 |
} |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
28 |
|
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
29 |
fn run() -> io::Result<i32> { |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
30 |
let current_dir = env::current_dir()?; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
31 |
let sock_path = locator::prepare_server_socket_path()?; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
32 |
let handler = ChgUiHandler::new(); |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
33 |
let (result_tx, result_rx) = oneshot::channel(); |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
34 |
let fut = UnixClient::connect(sock_path) |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
35 |
.and_then(|client| { |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
36 |
client.set_current_dir(current_dir) |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
37 |
}) |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
38 |
.and_then(|client| { |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
39 |
client.attach_io(io::stdin(), io::stdout(), io::stderr()) |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
40 |
}) |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
41 |
.and_then(|client| { |
40121
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
42 |
let pid = client.server_spec().process_id.unwrap(); |
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
43 |
let pgid = client.server_spec().process_group_id; |
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
44 |
procutil::setup_signal_handler_once(pid, pgid)?; |
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
45 |
Ok(client) |
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
46 |
}) |
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
47 |
.and_then(|client| { |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
48 |
client.run_command_chg(handler, env::args_os().skip(1)) |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
49 |
}) |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
50 |
.map(|(_client, _handler, code)| { |
40121
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
51 |
procutil::restore_signal_handler_once()?; |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
52 |
Ok(code) |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
53 |
}) |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
54 |
.or_else(|err| Ok(Err(err))) // pass back error to caller |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
55 |
.map(|res| result_tx.send(res).unwrap()); |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
56 |
tokio::run(fut); |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
57 |
result_rx.wait().unwrap_or(Err(io::Error::new(io::ErrorKind::Other, |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
58 |
"no exit code set"))) |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
59 |
} |