cli: only show Ctrl+C to detach when in a terminal (#178276)

Fixes #178091
This commit is contained in:
Connor Peet 2023-03-24 15:46:16 -07:00 committed by GitHub
parent 0dcdab5b32
commit 97af9cd323
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 14 deletions

View file

@ -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> {

View file

@ -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<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 {
@ -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| {

View file

@ -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`
@ -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())