rust/chg/src/main.rs
author Gregory Szorc <gregory.szorc@gmail.com>
Tue, 22 Jan 2019 17:08:53 -0800
changeset 41342 4ad002b2584d
parent 40288 87c76e5f3427
child 43836 ce088b38f92b
permissions -rw-r--r--
config: reject str sections and keys on Python 3 Otherwise we could end up with a dict having both the str and bytes versions of a particular config item. This may cause some tests to regress. I haven't checked. But it is better behavior to fail fast. We could just as easily change this to normalize the values. But I like catching all non-compliant call sites first. Differential Revision: https://phab.mercurial-scm.org/D5649
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39968
aab43d5861bb rust-chg: add project skeleton
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     1
// Copyright 2018 Yuya Nishihara <yuya@tcha.org>
aab43d5861bb rust-chg: add project skeleton
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     2
//
aab43d5861bb rust-chg: add project skeleton
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     3
// This software may be used and distributed according to the terms of the
aab43d5861bb rust-chg: add project skeleton
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     4
// GNU General Public License version 2 or any later version.
aab43d5861bb rust-chg: add project skeleton
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     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;
40288
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
     8
extern crate log;
39980
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
     9
extern crate tokio;
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    10
extern crate tokio_hglib;
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    11
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    12
use chg::{ChgClientExt, ChgUiHandler};
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    13
use chg::locator;
40121
89742f1fa6cb rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents: 39980
diff changeset
    14
use chg::procutil;
39980
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    15
use futures::sync::oneshot;
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    16
use std::env;
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    17
use std::io;
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    18
use std::process;
40288
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    19
use std::time::Instant;
39980
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    20
use tokio::prelude::*;
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    21
use tokio_hglib::UnixClient;
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    22
40288
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    23
struct DebugLogger {
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    24
    start: Instant,
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    25
}
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    26
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    27
impl DebugLogger {
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    28
    pub fn new() -> DebugLogger {
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    29
        DebugLogger {
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    30
            start: Instant::now(),
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    31
        }
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    32
    }
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    33
}
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    34
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    35
impl log::Log for DebugLogger {
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    36
    fn enabled(&self, metadata: &log::Metadata) -> bool {
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    37
        metadata.target().starts_with("chg::")
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    38
    }
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    39
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    40
    fn log(&self, record: &log::Record) {
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    41
        if self.enabled(record.metadata()) {
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    42
            // just make the output looks similar to chg of C
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    43
            let l = format!("{}", record.level()).to_lowercase();
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    44
            let t = self.start.elapsed();
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    45
            writeln!(io::stderr(), "chg: {}: {}.{:06} {}",
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    46
                     l, t.as_secs(), t.subsec_micros(), record.args()).unwrap_or(());
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    47
        }
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    48
    }
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    49
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    50
    fn flush(&self) {
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    51
    }
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    52
}
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    53
39968
aab43d5861bb rust-chg: add project skeleton
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    54
fn main() {
40288
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    55
    if env::var_os("CHGDEBUG").is_some() {
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    56
        log::set_boxed_logger(Box::new(DebugLogger::new()))
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    57
            .expect("any logger should not be installed yet");
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    58
        log::set_max_level(log::LevelFilter::Debug);
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    59
    }
87c76e5f3427 rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents: 40286
diff changeset
    60
39980
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    61
    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
    62
        writeln!(io::stderr(), "chg: abort: {}", err).unwrap_or(());
39980
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    63
        255
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    64
    });
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    65
    process::exit(code);
39968
aab43d5861bb rust-chg: add project skeleton
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    66
}
39980
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    67
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    68
fn run() -> io::Result<i32> {
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    69
    let current_dir = env::current_dir()?;
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    70
    let sock_path = locator::prepare_server_socket_path()?;
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    71
    let handler = ChgUiHandler::new();
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    72
    let (result_tx, result_rx) = oneshot::channel();
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    73
    let fut = UnixClient::connect(sock_path)
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    74
        .and_then(|client| {
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    75
            client.set_current_dir(current_dir)
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    76
        })
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    77
        .and_then(|client| {
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    78
            client.attach_io(io::stdin(), io::stdout(), io::stderr())
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    79
        })
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    80
        .and_then(|client| {
40121
89742f1fa6cb rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents: 39980
diff changeset
    81
            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
    82
            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
    83
            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
    84
            Ok(client)
89742f1fa6cb rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents: 39980
diff changeset
    85
        })
89742f1fa6cb rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents: 39980
diff changeset
    86
        .and_then(|client| {
39980
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    87
            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
    88
        })
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    89
        .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
    90
            procutil::restore_signal_handler_once()?;
39980
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    91
            Ok(code)
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    92
        })
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    93
        .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
    94
        .map(|res| result_tx.send(res).unwrap());
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    95
    tokio::run(fut);
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    96
    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
    97
                                                  "no exit code set")))
6bdee4bc181a rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents: 39968
diff changeset
    98
}