From f47e87fdf9624f647c09e6575017615106135958 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Thu, 7 Nov 2024 17:10:05 +0100 Subject: [PATCH] Factorize logic to wait for exit status after applet install --- crates/cli-tools/CHANGELOG.md | 1 + crates/cli-tools/src/action.rs | 24 ++++++++++++++++++++++-- crates/cli/CHANGELOG.md | 2 +- crates/cli/src/main.rs | 24 ++---------------------- crates/xtask/src/main.rs | 30 +++++++----------------------- 5 files changed, 33 insertions(+), 48 deletions(-) diff --git a/crates/cli-tools/CHANGELOG.md b/crates/cli-tools/CHANGELOG.md index 56567143..61782c56 100644 --- a/crates/cli-tools/CHANGELOG.md +++ b/crates/cli-tools/CHANGELOG.md @@ -16,6 +16,7 @@ ### Minor +- Add `action::AppletInstall::wait` to wait for exit status - Add `action::RustApplet{Build,Test}::crate_dir` - Add `action::PlatformInfo` to print platform serial and version - Add `cmd::spawn()` for more control on command execution diff --git a/crates/cli-tools/src/action.rs b/crates/cli-tools/src/action.rs index 2945dde9..a32bf091 100644 --- a/crates/cli-tools/src/action.rs +++ b/crates/cli-tools/src/action.rs @@ -75,14 +75,34 @@ pub struct AppletInstall { #[clap(flatten)] pub transfer: Transfer, + + #[command(subcommand)] + pub wait: Option, +} + +#[derive(clap::Subcommand)] +pub enum AppletInstallWait { + /// Waits until the applet exits. + #[group(id = "AppletInstallWait::Wait")] + Wait { + #[command(flatten)] + action: AppletExitStatus, + }, } impl AppletInstall { pub async fn run(self, connection: &mut dyn Connection) -> Result<()> { - let AppletInstall { applet, transfer } = self; + let AppletInstall { applet, transfer, wait } = self; transfer .run::(connection, applet, "Installed", None:: _>) - .await + .await?; + match wait { + Some(AppletInstallWait::Wait { mut action }) => { + action.wait.ensure_wait(); + action.run(connection).await + } + None => Ok(()), + } } } diff --git a/crates/cli/CHANGELOG.md b/crates/cli/CHANGELOG.md index 6e6ce604..85dbb377 100644 --- a/crates/cli/CHANGELOG.md +++ b/crates/cli/CHANGELOG.md @@ -49,4 +49,4 @@ ## 0.1.0 - + diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index a6dc6256..7c20ee75 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -56,8 +56,6 @@ enum Action { options: action::ConnectionOptions, #[command(flatten)] action: action::AppletInstall, - #[command(subcommand)] - command: Option, }, /// Updates an applet on a platform. @@ -147,16 +145,6 @@ enum Action { Completion(Completion), } -#[derive(clap::Subcommand)] -enum AppletInstallCommand { - /// Waits until the applet exits. - #[group(id = "AppletInstallCommand::Wait")] - Wait { - #[command(flatten)] - action: action::AppletExitStatus, - }, -} - #[derive(clap::Args)] struct Host { /// Path of the directory containing the host platform files. @@ -247,16 +235,8 @@ async fn main() -> Result<()> { let flags = Flags::parse(); match flags.action { Action::AppletList => bail!("not implemented yet"), - Action::AppletInstall { options, action, command } => { - let mut connection = options.connect().await?; - action.run(&mut connection).await?; - match command { - None => Ok(()), - Some(AppletInstallCommand::Wait { mut action }) => { - action.wait.ensure_wait(); - action.run(&mut connection).await - } - } + Action::AppletInstall { options, action } => { + action.run(&mut options.connect().await?).await } Action::AppletUpdate => bail!("not implemented yet"), Action::AppletUninstall { options, action } => { diff --git a/crates/xtask/src/main.rs b/crates/xtask/src/main.rs index f40e0398..eec64d23 100644 --- a/crates/xtask/src/main.rs +++ b/crates/xtask/src/main.rs @@ -154,19 +154,9 @@ enum AppletCommand { #[command(flatten)] options: action::ConnectionOptions, #[command(flatten)] - action: action::Transfer, + transfer: action::Transfer, #[command(subcommand)] - command: Option, - }, -} - -#[derive(clap::Subcommand)] -enum AppletInstallCommand { - /// Waits until the applet exits. - #[group(id = "AppletInstallCommand::Wait")] - Wait { - #[command(flatten)] - action: action::AppletExitStatus, + wait: Option, }, } @@ -418,19 +408,13 @@ impl AppletCommand { async fn execute(self, main: &MainOptions) -> Result<()> { match self { AppletCommand::Runner(runner) => runner.execute(main).await, - AppletCommand::Install { options, action, command } => { + AppletCommand::Install { options, transfer, mut wait } => { let applet = "target/wasefire/applet.wasm".into(); - let action = action::AppletInstall { applet, transfer: action }; - let mut connection = options.connect().await?; - action.run(&mut connection).await?; - match command { - None => Ok(()), - Some(AppletInstallCommand::Wait { mut action }) => { - action.wait.ensure_wait(); - action.ensure_exit(); - action.run(&mut connection).await - } + if let Some(action::AppletInstallWait::Wait { action }) = &mut wait { + action.ensure_exit(); } + let action = action::AppletInstall { applet, transfer, wait }; + action.run(&mut options.connect().await?).await } } }