Skip to content

Commit

Permalink
Factorize logic to wait for exit status after applet install
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 committed Nov 7, 2024
1 parent e4eb022 commit f47e87f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 48 deletions.
1 change: 1 addition & 0 deletions crates/cli-tools/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions crates/cli-tools/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,34 @@ pub struct AppletInstall {

#[clap(flatten)]
pub transfer: Transfer,

#[command(subcommand)]
pub wait: Option<AppletInstallWait>,
}

#[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::<service::AppletInstall>(connection, applet, "Installed", None::<fn(_) -> _>)
.await
.await?;
match wait {
Some(AppletInstallWait::Wait { mut action }) => {
action.wait.ensure_wait();
action.run(connection).await
}
None => Ok(()),
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@

## 0.1.0

<!-- Increment to skip CHANGELOG.md test: 11 -->
<!-- Increment to skip CHANGELOG.md test: 12 -->
24 changes: 2 additions & 22 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ enum Action {
options: action::ConnectionOptions,
#[command(flatten)]
action: action::AppletInstall,
#[command(subcommand)]
command: Option<AppletInstallCommand>,
},

/// Updates an applet on a platform.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 } => {
Expand Down
30 changes: 7 additions & 23 deletions crates/xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,9 @@ enum AppletCommand {
#[command(flatten)]
options: action::ConnectionOptions,
#[command(flatten)]
action: action::Transfer,
transfer: action::Transfer,
#[command(subcommand)]
command: Option<AppletInstallCommand>,
},
}

#[derive(clap::Subcommand)]
enum AppletInstallCommand {
/// Waits until the applet exits.
#[group(id = "AppletInstallCommand::Wait")]
Wait {
#[command(flatten)]
action: action::AppletExitStatus,
wait: Option<action::AppletInstallWait>,
},
}

Expand Down Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit f47e87f

Please sign in to comment.