Skip to content

Commit a4f6ede

Browse files
committed
all Cmd functions working
1 parent ba02bbd commit a4f6ede

2 files changed

Lines changed: 35 additions & 29 deletions

File tree

examples/command.roc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ main! = |_args| {
3838
3939
# To execute and capture the output (stdout and stderr) in the original form as bytes without inheriting your terminal.
4040
# Prefer using `exec_output!`.
41-
#cmd_output_bytes =
42-
# Cmd.new("echo")
43-
# .args(["Hi"])
44-
# .exec_output_bytes!()?
41+
cmd_output_bytes =
42+
Cmd.new("echo")
43+
.args(["Hi"])
44+
.exec_output_bytes!()?
4545
46-
#Stdout.line!("${Str.inspect(cmd_output_bytes)}")?
46+
Stdout.line!("${Str.inspect(cmd_output_bytes)}")?
4747

4848
Ok({})
4949
}

platform/Cmd.roc

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ Cmd :: {
6767
##
6868
## Stdout.line!("Echo output: ${cmd_output.stdout_utf8}")?
6969
## ```
70-
#exec_output! : Cmd => Try(
71-
# { stdout_utf8 : Str, stderr_utf8_lossy : Str },
72-
# [
73-
# StdoutContainsInvalidUtf8({ cmd_str : Str, err : [BadUtf8 { index : U64, problem : Str.Utf8Problem }] }),
74-
# NonZeroExitCode({ command : Str, exit_code : I32, stdout_utf8_lossy : Str, stderr_utf8_lossy : Str }),
75-
# FailedToGetExitCode({ command : Str, err : IOErr }),
76-
# ..
77-
# ]
78-
#)
70+
exec_output! : Cmd => Try(
71+
{ stdout_utf8 : Str, stderr_utf8_lossy : Str },
72+
[
73+
StdoutContainsInvalidUtf8({ cmd_str : Str, err : [BadUtf8({ problem : _, index : U64 })] }),
74+
NonZeroExitCode({ command : Str, exit_code : I32, stdout_utf8_lossy : Str, stderr_utf8_lossy : Str }),
75+
FailedToGetExitCode({ command : Str, err : IOErr }),
76+
..
77+
]
78+
)
7979
exec_output! = |cmd| {
8080
exec_try = host_exec_output!(cmd)
8181

@@ -118,30 +118,32 @@ Cmd :: {
118118
## Stdout.line!("${Str.inspect(cmd_output_bytes)}")? # {stderr_bytes: [], stdout_bytes: [72, 105, 10]}
119119
## ```
120120
#exec_output_bytes! : Cmd => Try(
121-
# { stderr_bytes : List(U8), stdout_bytes : List(U8) }
121+
# { stderr_bytes : List(U8), stdout_bytes : List(U8) },
122122
# [
123123
# FailedToGetExitCodeB(IOErr), # TODO: perhaps no need for B?
124124
# NonZeroExitCode({ exit_code : I32, stderr_bytes : List(U8), stdout_bytes : List(U8) }),
125125
# ..
126126
# ]
127127
#)
128-
#exec_output_bytes! = |cmd| {
129-
# exec_try = CmdInternal.command_exec_output!(cmd) # TODO
128+
exec_output_bytes! = |cmd| {
129+
exec_try = host_exec_output!(cmd)
130130

131-
# match exec_try {
132-
# Ok({ stderr_bytes, stdout_bytes }) =>
133-
# Ok({ stdout_bytes, stderr_bytes })
131+
match exec_try {
132+
Ok({ stderr_bytes, stdout_bytes }) =>
133+
Ok({ stdout_bytes, stderr_bytes })
134134

135-
# Err(inside_try) =>
136-
# match inside_try {
137-
# Ok({ exit_code, stderr_bytes, stdout_bytes }) ->
138-
# Err(NonZeroExitCodeB({ exit_code, stdout_bytes, stderr_bytes }))
135+
Err(inside_try) =>
136+
match inside_try {
137+
Ok({ exit_code, stderr_bytes, stdout_bytes }) => {
138+
Err(NonZeroExitCodeB({ exit_code, stdout_bytes, stderr_bytes }))
139+
}
139140

140-
# Err(err) ->
141-
# Err(FailedToGetExitCodeB(InternalIOErr.handle_err(err)))
142-
# }
143-
# }
144-
#}
141+
Err(err) => {
142+
Err(FailedToGetExitCodeB(err))
143+
}
144+
}
145+
}
146+
}
145147

146148
## Execute a command and return its exit code.
147149
## Stdin, stdout, and stderr are inherited from the parent process.
@@ -245,19 +247,23 @@ Cmd :: {
245247

246248
host_exec_exit_code! : Cmd => Try(I32, IOErr)
247249

250+
248251
# Do not change the order of the fields! It will lead to a segfault.
252+
# TODO uncomment once #9216 is fixed
249253
#OutputFromHostSuccess : {
250254
# stderr_bytes : List(U8),
251255
# stdout_bytes : List(U8),
252256
#}
253257

254258
# Do not change the order of the fields! It will lead to a segfault.
259+
# TODO uncomment once #9216 is fixed
255260
#OutputFromHostFailure : {
256261
# stderr_bytes : List(U8),
257262
# stdout_bytes : List(U8),
258263
# exit_code : I32,
259264
#}
260265

266+
# TODO use OutputFromHostSuccess and OutputFromHostFailure once #9216 is fixed
261267
host_exec_output! : Cmd => Try({stderr_bytes : List(U8), stdout_bytes : List(U8)}, (Try({stderr_bytes : List(U8), stdout_bytes : List(U8), exit_code : I32}, IOErr)))
262268

263269
to_str : Cmd -> Str

0 commit comments

Comments
 (0)