1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use crate::context::Context;
use crate::repl::{Error, Subcommand};
use reedline_repl_rs::clap::{ArgMatches, Command};
use reedline_repl_rs::Repl;

#[allow(clippy::unused_async)]
async fn run(_matches: ArgMatches, context: &mut Context) -> Result<Option<String>, Error> {
  let mut instance_state = context.instance_state.write().await;
  if let Some(instance_state) = instance_state.take() {
    instance_state.stop().expect("failed to stop");
  }
  std::process::exit(0);
}

impl Subcommand for ThisCommand {
  fn name(&self) -> &str {
    "quit"
  }

  fn build_command(&self, repl: Repl<Context, Error>) -> Repl<Context, Error> {
    repl.with_command_async(
      Command::new(self.name()).about("stop all running instances and quit"),
      |args, context| Box::pin(run(args, context)),
    )
  }
}

struct ThisCommand {}
pub fn build() -> Box<dyn Subcommand> {
  Box::new(ThisCommand {})
}