Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Use a channel to signal that the main fn is done #4860

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,21 @@ pub(crate) trait AsyncCliCommand: Send + Sync {

async fn run_async(self) -> Result<Self::Output, anyhow::Error>;

fn setup(&self) -> Option<JoinHandle<anyhow::Result<()>>> {
fn setup(
&self,
done: tokio::sync::oneshot::Receiver<()>,
) -> Option<JoinHandle<anyhow::Result<()>>> {
if is_terminal::IsTerminal::is_terminal(&std::io::stdin()) {
return Some(tokio::task::spawn(async move {
tokio::signal::ctrl_c().await?;
let term = console::Term::stdout();
let _ = term.show_cursor();
tokio::select! {
_ = done => {}

_ = tokio::signal::ctrl_c() => {
let term = console::Term::stdout();
let _ = term.show_cursor();
}
}

Ok::<(), anyhow::Error>(())
}));
}
Expand All @@ -91,10 +100,15 @@ impl<O: Send + Sync, C: AsyncCliCommand<Output = O>> CliCommand for C {

fn run(self) -> Result<(), anyhow::Error> {
tokio::runtime::Runtime::new()?.block_on(async {
let handle = self.setup();
let (snd, rcv) = tokio::sync::oneshot::channel();
let handle = self.setup(rcv);

AsyncCliCommand::run_async(self).await?;
theduke marked this conversation as resolved.
Show resolved Hide resolved

if snd.send(()).is_err() {
tracing::warn!("Failed to send 'done' signal to setup thread!");
}

if let Some(handle) = handle {
handle.await??;
}
Expand Down
Loading