This version of the module is deprecated: Superseded by the shell package in moonbitlang/async (import moonbitlang/async/shell): use @shell.Cmd with run, status, output and each_line, and @shell.glob. This module gets no further updates.
A small shell-free process EDSL for MoonBit
moon runwasm --experimental-policy examples/moonrun-policy.json cmd/smokemoon add bobzhang/myshell///|
import {
"bobzhang/myshell",
"moonbitlang/async",
}///|
#cfg(not(platform="windows"))
async test {
let output = Cmd::Cmd("printf", ["hello"]).output()
assert_eq(output.exit_code, 0)
assert_eq(output.stdout, "hello")
}///|
#cfg(not(platform="windows"))
async test {
let output = Cmd::Cmd("printf", ["%s", "$(echo no) | *"]).output()
assert_eq(output.stdout, "$(echo no) | *")
}///|
test {
let git = Cmd::Cmd("git", [])
let status = git.args(["status", "--short"])
let diff = git.arg("diff")
debug_inspect(status.arguments(), content="[\"status\", \"--short\"]")
debug_inspect(diff.arguments(), content="[\"diff\"]")
debug_inspect(git.arguments(), content="[]")
}///|
#cfg(not(platform="windows"))
async test {
let plan = Pipeline::Pipeline([
Cmd("printf", ["alpha\nbeta\n"]),
Cmd("grep", ["beta"]),
Cmd("tr", ["a-z", "A-Z"]),
])
let output = plan.output()
assert_eq(output.stdout, "BETA\n")
assert_eq(output.stage_exit_codes, [0, 0, 0])
}///|
test {
let plan = Cmd::Cmd("printf", ["hello"])
.pipe(Cmd("cat", []))
.pipe(Cmd("wc", ["-c"]))
assert_eq(plan.commands().length(), 3)
}///|
#cfg(not(platform="windows"))
async test {
let plan = Cmd::Cmd("cat", []).stdin("hello").pipe(Cmd("tr", ["a-z", "A-Z"]))
let output = plan.output()
assert_eq(output.stdout, "HELLO")
}///|
#cfg(not(platform="windows"))
async test {
let output = Cmd::Cmd("cat", []).stdin_bytes(b"\x00\xff").output()
assert_eq(output.stdout_bytes, b"\x00\xff")
}///|
test {
let command = Cmd::Cmd("tool", ["check"])
.cwd("workspace")
.env("NO_COLOR", "1")
debug_inspect(command.working_directory(), content="Some(\"workspace\")")
assert_eq(command.environment()["NO_COLOR"], "1")
}///|
#cfg(not(platform="windows"))
async test {
assert_eq(Cmd::Cmd("false", []).status(), 1)
}///|
#cfg(not(platform="windows"))
async test {
let output = Cmd::Cmd("false", []).output()
if !output.success() {
assert_eq(output.exit_code, 1)
}
}///|
#cfg(not(platform="windows"))
async test {
let output = Pipeline::Pipeline([Cmd("false", []), Cmd("cat", [])]).output()
assert_eq(output.exit_code, 1)
assert_eq(output.stage_exit_codes, [1, 0])
}///|
#cfg(not(platform="windows"))
async test {
let output = Cmd::Cmd("/usr/bin/grep", [
"needle", "/definitely/not/a/real/myshell-file",
]).output()
assert_true(!output.stderr.is_empty())
assert_eq(output.stage_stderr.length(), 1)
}///|
#cfg(not(platform="windows"))
async test {
try Cmd::Cmd("sleep", ["1"]).output(timeout_ms=20) catch {
@async.TimeoutError => ()
_ => fail("expected TimeoutError")
} noraise {
_ => fail("expected TimeoutError")
}
}///|
let output = command.output(max_output_bytes=32 * 1024 * 1024)moon run -e 'import {
"bobzhang/myshell",
"moonbitlang/async",
}
async fn main {
let output = @myshell.Pipeline::Pipeline([
@myshell.Cmd::Cmd("/usr/bin/printf", ["hello\nworld\n"]).clear_env(),
@myshell.Cmd::Cmd("/usr/bin/grep", ["world"]).clear_env(),
]).output()
print(output.stdout)
}'test {
let cmd = Cmd::Cmd("git", ["status", "--short"])
inspect(cmd.program(), content="git")
debug_inspect(cmd.arguments(), content="[\"status\", \"--short\"]")
}pub enum Process {
}let default_max_output_bytes : Int