From 22406dbde59980adcaf60f90f6bdad17b9524d32 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Fri, 24 Mar 2023 15:46:16 -0700 Subject: [PATCH] cli: only show `Ctrl+C to detach` when in a terminal (#178276) Fixes #178091 --- cli/src/tunnels/protocol.rs | 1 + cli/src/tunnels/singleton_client.rs | 28 ++++++++++++++++++++++++++++ cli/src/tunnels/singleton_server.rs | 16 ++-------------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/cli/src/tunnels/protocol.rs b/cli/src/tunnels/protocol.rs index 8a204e0fdbe26..528b2373fbad8 100644 --- a/cli/src/tunnels/protocol.rs +++ b/cli/src/tunnels/protocol.rs @@ -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> { diff --git a/cli/src/tunnels/singleton_client.rs b/cli/src/tunnels/singleton_client.rs index 8a814d9ea21c6..e805c73f1973b 100644 --- a/cli/src/tunnels/singleton_client.rs +++ b/cli/src/tunnels/singleton_client.rs @@ -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, @@ -34,6 +36,19 @@ struct SingletonServerContext { exit_entirely: Arc, } +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 { @@ -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| { diff --git a/cli/src/tunnels/singleton_server.rs b/cli/src/tunnels/singleton_server.rs index f78582806bc35..1a607c4ad1123 100644 --- a/cli/src/tunnels/singleton_server.rs +++ b/cli/src/tunnels/singleton_server.rs @@ -175,14 +175,6 @@ async fn serve_singleton_rpc( } } -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` @@ -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())