Skip to content

Commit

Permalink
cli: only show Ctrl+C to detach when in a terminal (microsoft#178276)
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 authored and qwq0 committed Mar 25, 2023
1 parent 1f96cc0 commit 22406db
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
1 change: 1 addition & 0 deletions cli/src/tunnels/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub mod singleton {
pub const METHOD_SHUTDOWN: &str = "shutdown";
pub const METHOD_STATUS: &str = "status";
pub const METHOD_LOG: &str = "log";
pub const METHOD_LOG_REPLY_DONE: &str = "log_done";

#[derive(Serialize)]
pub struct LogMessage<'a> {
Expand Down
28 changes: 28 additions & 0 deletions cli/src/tunnels/singleton_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use std::{
thread,
};

use const_format::concatcp;
use tokio::sync::mpsc;

use crate::{
async_pipe::{socket_stream_split, AsyncPipe},
constants::IS_INTERACTIVE_CLI,
json_rpc::{new_json_rpc, start_json_rpc},
log,
tunnels::protocol::EmptyObject,
Expand All @@ -34,6 +36,19 @@ struct SingletonServerContext {
exit_entirely: Arc<AtomicBool>,
}

const CONTROL_INSTRUCTIONS_COMMON: &str =
"Connected to an existing tunnel process running on this machine. You can press:
- \"x\" + Enter to stop the tunnel and exit
- \"r\" + Enter to restart the tunnel
";

const CONTROL_INSTRUCTIONS_INTERACTIVE: &str = concatcp!(
CONTROL_INSTRUCTIONS_COMMON,
"- Ctrl+C to detach
"
);

/// Serves a client singleton. Returns true if the process should exit after
/// this returns, instead of trying to start a tunnel.
pub async fn start_singleton_client(args: SingletonClientArgs) -> bool {
Expand Down Expand Up @@ -80,6 +95,19 @@ pub async fn start_singleton_client(args: SingletonClientArgs) -> bool {
Ok(())
});

rpc.register_sync(
protocol::singleton::METHOD_LOG_REPLY_DONE,
|_: EmptyObject, c| {
c.log.result(if *IS_INTERACTIVE_CLI {
CONTROL_INSTRUCTIONS_INTERACTIVE
} else {
CONTROL_INSTRUCTIONS_COMMON
});

Ok(())
},
);

rpc.register_sync(
protocol::singleton::METHOD_LOG,
|log: protocol::singleton::LogMessageOwned, c| {
Expand Down
16 changes: 2 additions & 14 deletions cli/src/tunnels/singleton_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,6 @@ async fn serve_singleton_rpc<C: Clone + Send + Sync + 'static>(
}
}

const CONTROL_INSTRUCTIONS: &str =
"Connected to an existing tunnel process running on this machine. You can press:
- Ctrl+C to detach
- \"x\" + Enter to stop the tunnel and exit
- \"r\" + Enter to restart the tunnel
";

/// Log sink that can broadcast and replay log events. Used for transmitting
/// logs from the singleton to all clients. This should be created and injected
/// into other services, like the tunnel, before `start_singleton_server`
Expand Down Expand Up @@ -223,12 +215,8 @@ impl BroadcastLogSink {

let _ = log_replay_tx.send(RpcCaller::serialize_notify(
&JsonRpcSerializer {},
protocol::singleton::METHOD_LOG,
protocol::singleton::LogMessage {
level: None,
prefix: "",
message: CONTROL_INSTRUCTIONS,
},
protocol::singleton::METHOD_LOG_REPLY_DONE,
protocol::EmptyObject {},
));

ConcatReceivable::new(log_replay_rx, self.tx.subscribe())
Expand Down

0 comments on commit 22406db

Please sign in to comment.