Page
Library
Module
Module type
Parameter
Class
Class type
Source
OS.CmdCommand existence and run.
Note. Executables are searched the PATH unless they contain a '/' character. Empty command lines result in errors.
exists cmd is true if the executable of cmd can be found in the path and false otherwise.
must_exist cmd is Ok cmd if exists is true and an error otherwise.
The following set of combinators are designed to be used with Pervasives.(|>) operator. See a few examples.
WARNING Windows. The ~append:true options for appending to files are unsupported on Windows. This old feature request should be fixed upstream.
The type for run statuses the run information and the process exit status.
val success : ('a * run_status, 'e) result -> ('a, 'e) resultsuccess r is:
Ok v if r = Ok (v, (_, `Exited 0))Error _ otherwise. Non `Exited 0 statuses are turned into an error message.err_file f is a standard error that writes to file f. If append is true (defaults to false) the data is appended to f.
val err_null : run_errerr_null is err_file File.null.
val err_run_out : run_errerr_run_out is a standard error that is redirected to the run's standard output.
val err_stderr : run_errerr_stderr is a standard error that is redirected to the current process standard error.
val in_string : string -> run_inin_string s is a standard input that reads s.
val in_null : run_inin_null is in_file File.null.
val in_stdin : run_inin_stdin is a standard input that reads from the current process standard input.
The following functions trigger actual command runs, consume their standard output and return the command and its status. In pipelined runs, the reported status is the one of the first failing run in the pipeline.
Warning. When a value of type run_out has been "consumed" with one of the following functions it cannot be reused.
val out_string : ?trim:bool -> run_out -> (string * run_status, 'e) resultout_string ~trim o captures the standard output o as a string. If trim is true (default) the result is passed through String.trim.
val out_lines : ?trim:bool -> run_out -> (string list * run_status, 'e) resultout_lines is like out_string but the result is splitted on newlines ('\n'). If the standard output is empty then the empty list is returned.
val out_file :
?append:bool ->
Fpath.t ->
run_out ->
(unit * run_status, 'e) resultout_file f o writes the standard output o to file f. If append is true (defaults to false) the data is appended to f.
out_run_in o is a run input that can be used to feed the standard output of o to the standard input of another, single, command run. Note that when the function returns the command run of o may not be terminated yet. The run using the resulting input will report an unsucessful status or error of o rather than its own error.
val out_null : run_out -> (unit * run_status, 'e) resultout_null o is out_file File.null o.
val out_stdout : run_out -> (unit * run_status, 'e) resultto_stdout o redirects the standard output o to the current process standard output.
The following functions can be used if you only care about success.
to_string ~trim o is (out_string ~trim o |> success).
to_lines ~trim o is (out_lines ~trim o |> success).
to_file ?append f o is (out_file ?append f o |> success).
run_io ~env ~err cmd i represents the standard output of the command run cmd performed in process environment env with its standard error output handled according to err (defaults to err_stderr) and standard input connected to i. Note that the command run is not started before the output is consumed, see run standard outputs.
run_out ?env ?err cmd is (in_stdin |> run_io ?env ?err cmd).
run_in ?env ?err cmd i is (run_io ?env ?err cmd |> to_stdout).
run ?env ?err cmd is (in_stdin |> run_io ?env ?err cmd |> to_stdout).
run_status ?env ?err ?quiet cmd is (in_stdin |> run_io ?env ?err ?cmd |> out_stdout) and extracts the run status.
If quiet is true (defaults to false), in_stdin and out_stdout are respectively replaced by in_null and out_null and err defaults to err_null rather than err_stderr.
Identity miaouw.
let id s = OS.Cmd.(in_string s |> run_io Cmd.(v "cat") |> out_string)Get the current list of git tracked files in OCaml:
let git = Cmd.v "git"
let git_tracked () =
let git_ls_files = Cmd.(git % "ls-files") in
OS.Cmd.(run_out git_ls_files |> to_lines)Tarbzip the current list of git tracked files, without reading the tracked files in OCaml:
let tbz_git_tracked dst =
let git_ls_files = Cmd.(git % "ls-files") in
let tbz = Cmd.(v "tar" % "-cvzf" % p dst % "-T" % "-") in
OS.Cmd.(run_out git_ls_files |> out_run_in) >>= fun tracked ->
OS.Cmd.(tracked |> run_in tbz)Send the email mail.
let send_email mail =
let sendmail = Cmd.v "sendmail" in
OS.Cmd.(in_string mail |> run_in sendmail)