rust/chg/src/main.rs
changeset 39980 6bdee4bc181a
parent 39968 aab43d5861bb
child 40121 89742f1fa6cb
equal deleted inserted replaced
39979:045ea159418d 39980:6bdee4bc181a
     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;
       
     7 extern crate futures;
       
     8 extern crate tokio;
       
     9 extern crate tokio_hglib;
       
    10 
       
    11 use chg::{ChgClientExt, ChgUiHandler};
       
    12 use chg::locator;
       
    13 use futures::sync::oneshot;
       
    14 use std::env;
       
    15 use std::io;
       
    16 use std::process;
       
    17 use tokio::prelude::*;
       
    18 use tokio_hglib::UnixClient;
       
    19 
     6 fn main() {
    20 fn main() {
       
    21     let code = run().unwrap_or_else(|err| {
       
    22         eprintln!("chg: abort: {}", err);
       
    23         255
       
    24     });
       
    25     process::exit(code);
     7 }
    26 }
       
    27 
       
    28 fn run() -> io::Result<i32> {
       
    29     let current_dir = env::current_dir()?;
       
    30     let sock_path = locator::prepare_server_socket_path()?;
       
    31     let handler = ChgUiHandler::new();
       
    32     let (result_tx, result_rx) = oneshot::channel();
       
    33     let fut = UnixClient::connect(sock_path)
       
    34         .and_then(|client| {
       
    35             client.set_current_dir(current_dir)
       
    36         })
       
    37         .and_then(|client| {
       
    38             client.attach_io(io::stdin(), io::stdout(), io::stderr())
       
    39         })
       
    40         .and_then(|client| {
       
    41             client.run_command_chg(handler, env::args_os().skip(1))
       
    42         })
       
    43         .map(|(_client, _handler, code)| {
       
    44             Ok(code)
       
    45         })
       
    46         .or_else(|err| Ok(Err(err)))  // pass back error to caller
       
    47         .map(|res| result_tx.send(res).unwrap());
       
    48     tokio::run(fut);
       
    49     result_rx.wait().unwrap_or(Err(io::Error::new(io::ErrorKind::Other,
       
    50                                                   "no exit code set")))
       
    51 }