Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
Added support for "wapm run [pirita-command]"
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Jul 12, 2022
1 parent 4c9f8d5 commit 46739a7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ hex = { version = "0.4", optional = true }
blake3 = { version = "0.3.1", optional = true }
indicatif = "0.16.2"
dialoguer = "0.10.1"
shellwords = "1.1.0"

[target.'cfg(not(target_os = "wasi"))'.dependencies]
whoami = "1.1.5"
Expand Down
66 changes: 66 additions & 0 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,73 @@ pub struct RunOpt {
args: Vec<OsString>,
}

#[derive(Debug)]
pub enum PiritaRunError {
Initialize(PiritaInitializeError),
Run(anyhow::Error),
}

#[derive(Debug)]
pub enum PiritaInitializeError {
CannotGetCurrentDir(std::io::Error),
CouldNotFindCommandInDotBin(std::io::Error),
}

pub fn try_run_pirita(run_options: &RunOpt) -> Result<(), PiritaRunError> {

let command_name = run_options.command.as_str();
let args = &run_options.args;
let current_dir = crate::config::Config::get_current_dir()
.map_err(|e| PiritaRunError::Initialize(PiritaInitializeError::CannotGetCurrentDir(e)))?;

let cmd = std::fs::read_to_string(current_dir.join("wapm_packages").join(".bin").join(command_name))
.map_err(|e| PiritaRunError::Initialize(PiritaInitializeError::CouldNotFindCommandInDotBin(e)))?;

let mut sw = shellwords::split(&cmd)
.map_err(|e| PiritaRunError::Run(e.into()))?;

if sw.get(0).map(|s| s.as_str()) != Some("wasmer") || sw.get(1).map(|s| s.as_str()) != Some("run") {
return Err(PiritaRunError::Run(anyhow!(
"Expected \"wasmer run\" command in command for {command_name:?}, got: {sw:?}"
)));
}

sw.remove(0);
sw.remove(0);

run_pirita(&sw)
.map_err(|e| PiritaRunError::Run(e))
}

fn run_pirita(args: &[String]) -> Result<(), anyhow::Error> {

let mut command = std::process::Command::new("wasmer");

command.arg("run");

for arg in args {
command.arg(arg);
}

let output = command.output()?;
if !output.stderr.is_empty() {
Err(anyhow!("{}", String::from_utf8_lossy(&output.stderr)))
} else if !output.stdout.is_empty() {
println!("{}", String::from_utf8_lossy(&output.stdout));
Ok(())
} else {
Ok(())
}
}

pub fn run(run_options: RunOpt) -> anyhow::Result<()> {

match try_run_pirita(&run_options) {
Ok(()) => return Ok(()),
Err(PiritaRunError::Initialize(_)) => { },
Err(PiritaRunError::Run(e)) => return Err(e),
}

let command_name = run_options.command.as_str();
let args = &run_options.args;
let current_dir = crate::config::Config::get_current_dir()?;
Expand Down

0 comments on commit 46739a7

Please sign in to comment.