Skip to content

Commit ee37aca

Browse files
committed
exec, exec_cmd and exec_exit_code works
Currently requires Roc branch fix-match-open-union
1 parent dcab0b9 commit ee37aca

2 files changed

Lines changed: 34 additions & 44 deletions

File tree

examples/command.roc

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import pf.Cmd
77

88
main! = |_args| {
99
# Simplest way to execute a command (prints to your terminal).
10-
#Cmd.exec!("echo", ["Hello"])?
10+
Cmd.exec!("echo", ["Hello"])?
1111

1212
# To execute and capture the output (stdout and stderr) without inheriting your terminal.
1313
#cmd_output =
@@ -18,31 +18,21 @@ main! = |_args| {
1818
#Stdout.line!("${Str.inspect(cmd_output)}")?
1919

2020
# To run a command with environment variables.
21-
#Cmd.new("env")
22-
# .clear_envs() # You probably don't need to clear all other environment variables, this is just an example.
23-
# .env("FOO", "BAR")
24-
# .envs([("BAZ", "DUCK"), ("XYZ", "ABC")]) # Set multiple environment variables at once with `envs`
25-
# .args(["-v"])
26-
# .exec_cmd!()?
21+
Cmd.new("env")
22+
.clear_envs() # You probably don't need to clear all other environment variables, this is just an example.
23+
.env("FOO", "BAR")
24+
.envs([("BAZ", "DUCK"), ("XYZ", "ABC")]) # Set multiple environment variables at once with `envs`
25+
.args(["-v"])
26+
.exec_cmd!()?
2727

2828
# To execute and just get the exit code (prints to your terminal).
2929
# Prefer using `exec!` or `exec_cmd!`.
30-
output1 =
31-
Cmd.new("cattt") # line! prints do work if this is just `cat` (this is the non-error case)
30+
exit_code =
31+
Cmd.new("cat")
3232
.args(["non_existent.txt"])
33-
.exec_exit_code!()
33+
.exec_exit_code!()?
3434

35-
Stdout.line!("pre Inspect1")?
36-
37-
Stdout.line!("Inspect1: ${Str.inspect(output1)}")?
38-
39-
Stdout.line!("post Inspect1")?
40-
41-
output2 = output1?
42-
43-
Stdout.line!("Inspect2: ${Str.inspect(output2)}")?
44-
45-
Stdout.line!("done")?
35+
Stdout.line!("Exit code: ${exit_code.to_str()}")?
4636
4737
# TODO add exec_output_bytes
4838

platform/Cmd.roc

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ Cmd :: {
1515
## ```roc
1616
## Cmd.exec!("echo", ["hello world"])?
1717
## ```
18-
#exec! : Str, List(Str) => Try({}, [ExecFailed({ command : Str, exit_code : I32 }), FailedToGetExitCode { command : Str, err : IOErr }, ..])
19-
#exec! = |program, arguments| {
20-
# exit_code =
21-
# new(program)
22-
# .args(arguments)
23-
# .exec_exit_code!()?
18+
exec! : Str, List(Str) => Try({}, [ExecFailed({ command : Str, exit_code : I32 }), FailedToGetExitCode({ command : Str, err : IOErr }), ..])
19+
exec! = |program, arguments| {
20+
exit_code =
21+
new(program)
22+
.args(arguments)
23+
.exec_exit_code!()?
2424

25-
# if exit_code == 0 {
26-
# Ok({})
27-
# } else {
28-
# command = "${cmd_name} ${arguments.join_with(" ")}"
29-
# Err(ExecFailed({ command, exit_code }))
30-
# }
31-
#}
25+
if exit_code == 0 {
26+
Ok({})
27+
} else {
28+
command = "${program} ${arguments.join_with(" ")}"
29+
Err(ExecFailed({ command, exit_code }))
30+
}
31+
}
3232

3333
## Execute a Cmd (using the builder pattern).
3434
## Stdin, stdout, and stderr are inherited from the parent process.
@@ -42,16 +42,16 @@ Cmd :: {
4242
## .env("RUST_BACKTRACE", "1")
4343
## .exec_cmd!()?
4444
## ```
45-
#exec_cmd! : Cmd => Try({}, [ExecCmdFailed { command : Str, exit_code : I32 }, FailedToGetExitCode { command : Str, err : IOErr }, ..])
46-
#exec_cmd! = |cmd| {
47-
# exit_code = exec_exit_code!(cmd)?
48-
#
49-
# if exit_code == 0 {
50-
# Ok({})
51-
# } else {
52-
# Err(ExecCmdFailed({ command: to_str(cmd), exit_code }))
53-
# }
54-
#}
45+
exec_cmd! : Cmd => Try({}, [ExecCmdFailed({ command : Str, exit_code : I32 }), FailedToGetExitCode({ command : Str, err : IOErr }), ..])
46+
exec_cmd! = |cmd| {
47+
exit_code = exec_exit_code!(cmd)?
48+
49+
if exit_code == 0 {
50+
Ok({})
51+
} else {
52+
Err(ExecCmdFailed({ command: to_str(cmd), exit_code }))
53+
}
54+
}
5555

5656
## Execute command and capture stdout and stderr as UTF-8 strings.
5757
## Invalid UTF-8 sequences are replaced with the Unicode replacement character.

0 commit comments

Comments
 (0)