Patchwork [13,of,13] rust-chg: add main program

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 2, 2018, 2:25 p.m.
Message ID <0c503d7b2397ff647786.1538490352@mimosa>
Download mbox | patch
Permalink /patch/35319/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 2, 2018, 2:25 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1537784630 -32400
#      Mon Sep 24 19:23:50 2018 +0900
# Node ID 0c503d7b2397ff6477864a255ab461fa649d7eee
# Parent  a64ea1d912b6aa3328c020f3cd1a9b8239bfcf25
rust-chg: add main program

It can at least connect to a *running* command server, and execute Mercurial
command. No signal handling nor daemon management is implemented yet.

Patch

diff --git a/rust/chg/src/main.rs b/rust/chg/src/main.rs
--- a/rust/chg/src/main.rs
+++ b/rust/chg/src/main.rs
@@ -3,5 +3,49 @@ 
 // This software may be used and distributed according to the terms of the
 // GNU General Public License version 2 or any later version.
 
+extern crate chg;
+extern crate futures;
+extern crate tokio;
+extern crate tokio_hglib;
+
+use chg::{ChgClientExt, ChgUiHandler};
+use chg::locator;
+use futures::sync::oneshot;
+use std::env;
+use std::io;
+use std::process;
+use tokio::prelude::*;
+use tokio_hglib::UnixClient;
+
 fn main() {
+    let code = run().unwrap_or_else(|err| {
+        eprintln!("chg: abort: {}", err);
+        255
+    });
+    process::exit(code);
 }
+
+fn run() -> io::Result<i32> {
+    let current_dir = env::current_dir()?;
+    let sock_path = locator::prepare_server_socket_path()?;
+    let handler = ChgUiHandler::new();
+    let (result_tx, result_rx) = oneshot::channel();
+    let fut = UnixClient::connect(sock_path)
+        .and_then(|client| {
+            client.set_current_dir(current_dir)
+        })
+        .and_then(|client| {
+            client.attach_io(io::stdin(), io::stdout(), io::stderr())
+        })
+        .and_then(|client| {
+            client.run_command_chg(handler, env::args_os().skip(1))
+        })
+        .map(|(_client, _handler, code)| {
+            Ok(code)
+        })
+        .or_else(|err| Ok(Err(err)))  // pass back error to caller
+        .map(|res| result_tx.send(res).unwrap());
+    tokio::run(fut);
+    result_rx.wait().unwrap_or(Err(io::Error::new(io::ErrorKind::Other,
+                                                  "no exit code set")))
+}