11app [main!] { pf: platform " ../platform/main.roc" }
22
33import pf.Stdout
4+ import pf.Stderr
45import pf.Cmd
56import pf.Arg exposing [Arg ]
67
@@ -11,48 +12,85 @@ import pf.Arg exposing [Arg]
1112main ! : List Arg => Result {} _
1213main ! = |_args |
1314
14- # Simplest way to execute a command
15- exec_example!({})?
15+ # Simplest way to execute a command (prints to your terminal).
16+ Cmd . exec !(" echo ", [ " Hello " ]) ? |err| EchoHelloFailed (err)
1617
17- # To execute and capture the output (includes stderr)
18- output_example!({})?
18+ # To execute and capture the output (stdout, stderr, and exit code) without inheriting your terminal.
19+ output_example!({}) ? |err| OutputExampleFailed (err)
1920
20- # To execute and get the exit code
21- status_example!({})?
21+ # To run a command with an environment variable.
22+ env_example!({}) ? |err| EnvExampleFailed (err)
23+
24+ # To execute and just get the exit code (prints to your terminal).
25+ status_example!({}) ? |err| StatusExampleFailed (err)
2226
2327 Ok ({})
2428
25- exec_example ! : {} => Result {} _
26- exec_example ! = |{}| Cmd . exec !("echo ", [" EXEC" ])
29+ # Execute command and capture the output (stdout, stderr, and exit code)
30+ output_example ! : {} => Result {} _
31+ output_example ! = |{}|
2732
28- # Run "env" with verbose option, clear all environment variables, and pass in
29- # "FOO" and "BAZ".
30- status_example ! : {} => Result {} _
31- status_example ! = |{}|
32- result =
33- Cmd . new ("env ")
34- |> Cmd . arg ("-v ")
35- |> Cmd . clear_envs
36- |> Cmd . envs ([("FOO ", " BAR" ), (" BAZ" , " DUCK" )])
37- |> Cmd . status !
33+ cmd_output =
34+ Cmd . new ("echo ")
35+ |> Cmd . args (["Hi "])
36+ |> Cmd . output !
3837
39- when result is
40- Ok (exit_code) if exit_code == 0 -> Ok ({})
41- Ok (exit_code) -> Stdout . line !("Child exited with non- zero code: ${Num . to_str (exit_code )}")
42- Err (err) -> Stdout . line !("Error executing command: ${Inspect . to_str (err )}")
38+ print_output!(cmd_output)
39+
40+
41+ print_output ! : Cmd . Output => Result {} _
42+ print_output ! = |cmd_output|
4343
44- # Run "env" with verbose option, clear all environment variables, and pass in
45- # only as an environment variable "FOO"
46- output_example ! : {} => Result {} _
47- output_example ! = |{}|
4844
49- output =
45+ when cmd_output. status is
46+ Ok (0 ) ->
47+ stdout_utf8 = Str . from_utf8 (cmd_output .stdout )?
48+ Stdout . line !("Command output: ${stdout_utf8}" )
49+
50+ Ok(exit_code) ->
51+ stdout_utf8 = Str.from_utf8_lossy(cmd_output.stdout)
52+ stderr_utf8 = Str.from_utf8_lossy(cmd_output.stderr)
53+ err_data =
54+ " " "
55+ Command failed:
56+ - exit code: ${Num . to_str (exit_code )}
57+ - stdout: ${stdout_utf8}
58+ - stderr: ${stderr_utf8}
59+ " " "
60+
61+ Stderr.line!(err_data)
62+
63+ Err(err) ->
64+ Stderr.line!(" Failed to get exit code for command, error: ${Inspect . to_str (err )}")
65+
66+
67+ # Run command with an environment variable
68+ env_example ! : {} => Result {} _
69+ env_example ! = |{}|
70+
71+ cmd_output =
5072 Cmd . new ("env ")
51- |> Cmd . clear_envs
73+ |> Cmd . clear_envs # You probably don't need to clear all other environment variables, this is just an example.
5274 |> Cmd . env ("FOO ", " BAR" )
75+ |> Cmd . envs ([("BAZ ", " DUCK" ), (" XYZ" , " ABC" )]) # Set multiple environment variables at once with `envs`
5376 |> Cmd . args (["-v "])
5477 |> Cmd . output !
5578
56- msg = Str . from_utf8 (output .stdout ) |> Result . with_default ("Failed to decode stdout" )
79+ print_output!(cmd_output)
80+
81+ # Execute command and capture the exit code
82+ status_example ! : {} => Result {} _
83+ status_example ! = |{}|
84+ cmd_result =
85+ Cmd . new ("echo ")
86+ |> Cmd . args (["Yo "])
87+ |> Cmd . status !
88+
89+ when cmd_result is
90+ Ok (0 ) -> Ok ({})
91+
92+ Ok (exit_code) ->
93+ Stderr . line !("Command failed with exit code: ${Num . to_str (exit_code )}")
5794
58- Stdout.write!(msg)
95+ Err (err) ->
96+ Stderr . line !("Failed to get exit code for command, error: ${Inspect . to_str (err )}")
0 commit comments