Skip to content

Commit

Permalink
Use async
Browse files Browse the repository at this point in the history
Created using spr 1.3.5
  • Loading branch information
sunshowers committed Nov 7, 2023
1 parent 1592114 commit 2bd1293
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 59 deletions.
14 changes: 5 additions & 9 deletions wicket/src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::net::SocketAddrV6;

use anyhow::Result;
use clap::{Args, ColorChoice, Parser, Subcommand};
use tokio::runtime::Handle;

use super::{
preflight::PreflightArgs, rack_setup::SetupArgs,
Expand All @@ -34,24 +33,21 @@ pub(crate) struct ShellApp {
}

impl ShellApp {
pub(crate) fn exec(
pub(crate) async fn exec(
self,
log: slog::Logger,
handle: &Handle,
wicketd_addr: SocketAddrV6,
output: CommandOutput<'_>,
) -> Result<()> {
match self.command {
ShellCommand::UploadRepo(args) => {
args.exec(log, handle, wicketd_addr)
args.exec(log, wicketd_addr).await
}
ShellCommand::RackUpdate(args) => {
args.exec(log, handle, wicketd_addr, self.global_opts, output)
}
ShellCommand::Setup(args) => args.exec(log, handle, wicketd_addr),
ShellCommand::Preflight(args) => {
args.exec(log, handle, wicketd_addr)
args.exec(log, wicketd_addr, self.global_opts, output).await
}
ShellCommand::Setup(args) => args.exec(log, wicketd_addr).await,
ShellCommand::Preflight(args) => args.exec(log, wicketd_addr).await,
}
}
}
Expand Down
12 changes: 1 addition & 11 deletions wicket/src/cli/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::borrow::Cow;
use std::fmt::Display;
use std::net::SocketAddrV6;
use std::time::Duration;
use tokio::runtime::Handle;
use update_engine::events::StepEvent;
use update_engine::events::StepEventKind;
use update_engine::events::StepInfo;
Expand Down Expand Up @@ -48,16 +47,7 @@ pub(crate) enum PreflightArgs {
}

impl PreflightArgs {
pub(crate) fn exec(
self,
log: Logger,
handle: &Handle,
wicketd_addr: SocketAddrV6,
) -> Result<()> {
handle.block_on(self.exec_impl(log, wicketd_addr))
}

async fn exec_impl(
pub(crate) async fn exec(
self,
log: Logger,
wicketd_addr: SocketAddrV6,
Expand Down
12 changes: 1 addition & 11 deletions wicket/src/cli/rack_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::io::Read;
use std::mem;
use std::net::SocketAddrV6;
use std::time::Duration;
use tokio::runtime::Handle;
use wicket_common::rack_setup::PutRssUserConfigInsensitive;
use wicketd_client::types::CertificateUploadResponse;
use wicketd_client::types::NewPasswordHash;
Expand Down Expand Up @@ -62,16 +61,7 @@ pub(crate) enum SetupArgs {
}

impl SetupArgs {
pub(crate) fn exec(
self,
log: Logger,
handle: &Handle,
wicketd_addr: SocketAddrV6,
) -> Result<()> {
handle.block_on(self.exec_impl(log, wicketd_addr))
}

async fn exec_impl(
pub(crate) async fn exec(
self,
log: Logger,
wicketd_addr: SocketAddrV6,
Expand Down
15 changes: 2 additions & 13 deletions wicket/src/cli/rack_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{
use anyhow::{anyhow, bail, Context, Result};
use clap::{Args, Subcommand};
use slog::Logger;
use tokio::{runtime::Handle, sync::watch, task::JoinHandle};
use tokio::{sync::watch, task::JoinHandle};
use update_engine::display::{GroupDisplay, LineDisplayStyles};
use wicket_common::update_events::EventReport;
use wicketd_client::types::StartUpdateParams;
Expand All @@ -37,18 +37,7 @@ pub(crate) enum RackUpdateArgs {
}

impl RackUpdateArgs {
pub(crate) fn exec(
self,
log: Logger,
handle: &Handle,
wicketd_addr: SocketAddrV6,
global_opts: GlobalOpts,
output: CommandOutput<'_>,
) -> Result<()> {
handle.block_on(self.exec_impl(log, wicketd_addr, global_opts, output))
}

async fn exec_impl(
pub(crate) async fn exec(
self,
log: Logger,
wicketd_addr: SocketAddrV6,
Expand Down
6 changes: 2 additions & 4 deletions wicket/src/cli/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use buf_list::BufList;
use clap::Args;
use futures::StreamExt;
use reqwest::Body;
use tokio::runtime::Handle;
use tokio_util::io::ReaderStream;

use crate::wicketd::create_wicketd_client;
Expand All @@ -32,13 +31,12 @@ pub(crate) struct UploadArgs {
}

impl UploadArgs {
pub(crate) fn exec(
pub(crate) async fn exec(
self,
log: slog::Logger,
handle: &Handle,
wicketd_addr: SocketAddrV6,
) -> Result<()> {
handle.block_on(self.do_upload(log, wicketd_addr))
self.do_upload(log, wicketd_addr).await
}

async fn do_upload(
Expand Down
16 changes: 5 additions & 11 deletions wicket/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use camino::{Utf8Path, Utf8PathBuf};
use clap::Parser;
use omicron_common::{address::WICKETD_PORT, FileKv};
use slog::Drain;
use tokio::runtime::Handle;

use crate::{
cli::{CommandOutput, ShellApp},
Expand All @@ -31,12 +30,11 @@ pub fn exec() -> Result<()> {

let runtime = tokio::runtime::Runtime::new()
.context("creating tokio runtime")?;
exec_with_args(
runtime.handle(),
runtime.block_on(exec_with_args(
wicketd_addr,
args,
OutputKind::Terminal,
)
))
}
Err(_) => {
// Do not expose log messages via standard error since they'll show up
Expand All @@ -60,11 +58,7 @@ pub enum OutputKind<'a> {
Terminal,
}

pub fn exec_with_args<S>(
// While it is possible to obtain an ambient handle, it's more type-safe to
// require that users explicitly pass in a handle (this means that they're
// more likely to have created a runtime).
handle: &Handle,
pub async fn exec_with_args<S>(
wicketd_addr: SocketAddrV6,
args: Vec<S>,
output: OutputKind<'_>,
Expand All @@ -81,7 +75,7 @@ where
match output {
OutputKind::Captured { log, stdout, stderr } => {
let output = CommandOutput { stdout, stderr };
app.exec(log, handle, wicketd_addr, output)
app.exec(log, wicketd_addr, output).await
}
OutputKind::Terminal => {
let log = setup_log(
Expand All @@ -92,7 +86,7 @@ where
let mut stderr = std::io::stderr();
let output =
CommandOutput { stdout: &mut stdout, stderr: &mut stderr };
app.exec(log, handle, wicketd_addr, output)
app.exec(log, wicketd_addr, output).await
}
}
}
Expand Down

0 comments on commit 2bd1293

Please sign in to comment.