Skip to content

Commit

Permalink
feat: the "wasmer run" command now respects "--quiet"
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Jun 19, 2023
1 parent f582770 commit f00edf6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 55 deletions.
94 changes: 43 additions & 51 deletions lib/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::commands::{
use crate::commands::{CreateObj, GenCHeader};
use crate::error::PrettyError;
use clap::{CommandFactory, Parser};
use wasmer_deploy_cli::cmd::CliCommand;

/// The main function for the Wasmer CLI tool.
pub fn wasmer_main() {
Expand All @@ -27,14 +28,11 @@ pub fn wasmer_main() {

fn wasmer_main_inner() -> Result<(), anyhow::Error> {
if is_binfmt_interpreter() {
Run::from_binfmt_args().execute();
Run::from_binfmt_args().execute(crate::logging::Output::default());
}

match Args::try_parse() {
Ok(args) => {
args.output.initialize_logging();
args.execute()
}
Ok(args) => args.execute(),
Err(e) => {
let might_be_wasmer_run = matches!(
e.kind(),
Expand All @@ -47,8 +45,9 @@ fn wasmer_main_inner() -> Result<(), anyhow::Error> {
// shorthand. Note that this has discoverability issues
// because it's not shown as part of the main argument
// parser's help, but that's fine.
crate::logging::Output::default().initialize_logging();
run.execute();
let output = crate::logging::Output::default();
output.initialize_logging();
run.execute(output);
}
}

Expand Down Expand Up @@ -87,16 +86,47 @@ impl Args {
output,
} = self;

output.initialize_logging();

if version {
return print_version(output.is_verbose());
}

if let Some(cmd) = cmd {
cmd.execute()
} else {
Args::command().print_long_help()?;
// Note: clap uses an exit code of 2 when CLI parsing fails
std::process::exit(2);
match cmd {
Some(Cmd::Run(options)) => options.execute(output),
Some(Cmd::SelfUpdate(options)) => options.execute(),
Some(Cmd::Cache(cache)) => cache.execute(),
Some(Cmd::Validate(validate)) => validate.execute(),
#[cfg(feature = "compiler")]
Some(Cmd::Compile(compile)) => compile.execute(),
#[cfg(any(feature = "static-artifact-create", feature = "wasmer-artifact-create"))]
Some(Cmd::CreateExe(create_exe)) => create_exe.execute(),
#[cfg(feature = "static-artifact-create")]
Some(Cmd::CreateObj(create_obj)) => create_obj.execute(),
Some(Cmd::Config(config)) => config.execute(),
Some(Cmd::Inspect(inspect)) => inspect.execute(),
Some(Cmd::Init(init)) => init.execute(),
Some(Cmd::Login(login)) => login.execute(),
Some(Cmd::Publish(publish)) => publish.execute(),
#[cfg(feature = "static-artifact-create")]
Some(Cmd::GenCHeader(gen_heder)) => gen_heder.execute(),
#[cfg(feature = "wast")]
Some(Cmd::Wast(wast)) => wast.execute(),
#[cfg(target_os = "linux")]
Some(Cmd::Binfmt(binfmt)) => binfmt.execute(),
Some(Cmd::Whoami(whoami)) => whoami.execute(),
Some(Cmd::Add(install)) => install.execute(),

// Deploy commands.
Some(Cmd::Deploy(c)) => c.run(),
Some(Cmd::App(apps)) => apps.run(),
Some(Cmd::Ssh(ssh)) => ssh.run(),
Some(Cmd::Namespace(namespace)) => namespace.run(),
None => {
Args::command().print_long_help()?;
// Note: clap uses an exit code of 2 when CLI parsing fails
std::process::exit(2);
}
}
}
}
Expand Down Expand Up @@ -242,44 +272,6 @@ enum Cmd {
Namespace(wasmer_deploy_cli::cmd::namespace::CmdNamespace),
}

impl Cmd {
fn execute(self) -> Result<(), anyhow::Error> {
use wasmer_deploy_cli::cmd::CliCommand;

match self {
Self::Run(options) => options.execute(),
Self::SelfUpdate(options) => options.execute(),
Self::Cache(cache) => cache.execute(),
Self::Validate(validate) => validate.execute(),
#[cfg(feature = "compiler")]
Self::Compile(compile) => compile.execute(),
#[cfg(any(feature = "static-artifact-create", feature = "wasmer-artifact-create"))]
Self::CreateExe(create_exe) => create_exe.execute(),
#[cfg(feature = "static-artifact-create")]
Self::CreateObj(create_obj) => create_obj.execute(),
Self::Config(config) => config.execute(),
Self::Inspect(inspect) => inspect.execute(),
Self::Init(init) => init.execute(),
Self::Login(login) => login.execute(),
Self::Publish(publish) => publish.execute(),
#[cfg(feature = "static-artifact-create")]
Self::GenCHeader(gen_heder) => gen_heder.execute(),
#[cfg(feature = "wast")]
Self::Wast(wast) => wast.execute(),
#[cfg(target_os = "linux")]
Self::Binfmt(binfmt) => binfmt.execute(),
Self::Whoami(whoami) => whoami.execute(),
Self::Add(install) => install.execute(),

// Deploy commands.
Self::Deploy(c) => c.run(),
Self::App(apps) => apps.run(),
Self::Ssh(ssh) => ssh.run(),
Self::Namespace(namespace) => namespace.run(),
}
}
}

fn is_binfmt_interpreter() -> bool {
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
Expand Down
9 changes: 5 additions & 4 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use wasmer_wasix::{
};
use webc::{metadata::Manifest, Container};

use crate::{commands::run::wasi::Wasi, error::PrettyError, store::StoreOptions};
use crate::{commands::run::wasi::Wasi, error::PrettyError, logging::Output, store::StoreOptions};

const TICK: Duration = Duration::from_millis(250);

Expand Down Expand Up @@ -86,12 +86,12 @@ pub struct Run {
}

impl Run {
pub fn execute(&self) -> ! {
let result = self.execute_inner();
pub fn execute(self, output: Output) -> ! {
let result = self.execute_inner(output);
exit_with_wasi_exit_code(result);
}

fn execute_inner(&self) -> Result<(), Error> {
fn execute_inner(self, output: Output) -> Result<(), Error> {
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
Expand All @@ -108,6 +108,7 @@ impl Run {
.prepare_runtime(store.engine().clone(), &self.wasmer_dir, handle)?;

let progress = MultiProgress::new();
progress.set_draw_target(output.draw_target());

// This is a slow operation, so let's temporarily wrap the runtime with
// something that displays progress
Expand Down
12 changes: 12 additions & 0 deletions lib/cli/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,16 @@ impl Output {
clap::ColorChoice::Never => false,
}
}

/// Get the draw target to be used with the `indicatif` crate.
///
/// Progress indicators won't draw anything if the user passed the `--quiet`
/// flag.
pub fn draw_target(&self) -> indicatif::ProgressDrawTarget {
if self.quiet {
return indicatif::ProgressDrawTarget::hidden();
}

indicatif::ProgressDrawTarget::stderr()
}
}

0 comments on commit f00edf6

Please sign in to comment.