OCaml: Add separate logging channels and a dedicated 'command' type
This commit is contained in:
parent
fab7e2425a
commit
ac3dbe4d30
9 changed files with 72 additions and 36 deletions
|
|
@ -1,15 +1,30 @@
|
|||
let merge (schema: Schema.schema) (packages: string list) =
|
||||
let merge (schema: Schema.schema) (packages: string list): Schema.schema =
|
||||
|
||||
match packages with
|
||||
| [] -> { schema with output = { message = "No packages provided" } }
|
||||
| [] -> { schema with output = {
|
||||
schema.output with main = "No packages provided" }
|
||||
}
|
||||
| _ ->
|
||||
|
||||
let in_targets = List.flatten [["doas"; "apk"; "-i"; "add"]; packages] and
|
||||
out_targets = List.flatten [["doas"; "apk"; "-i"; "del"]; packages] in
|
||||
let commands: Process.Command.command list = [
|
||||
{
|
||||
name = "doas";
|
||||
arguments = ["doas"; "apk"; "-i"; "add"] @ packages;
|
||||
status = Unevaluated;
|
||||
};
|
||||
{
|
||||
name = "doas";
|
||||
arguments = ["doas"; "apk"; "-i"; "del"] @ packages;
|
||||
status = Unevaluated;
|
||||
}
|
||||
] in
|
||||
|
||||
Process.Fork.run "doas" in_targets;
|
||||
Process.Fork.run "doas" out_targets;
|
||||
let ran = Process.Fork.run_many commands in
|
||||
let formatted_ran = Process.Command.format_many ran in
|
||||
|
||||
{ schema with output = {
|
||||
message = "Done: " ^ (String.concat "\n" packages)
|
||||
}}
|
||||
{
|
||||
schema with output = {
|
||||
schema.output with log =
|
||||
"Done:\n" ^ (String.concat "\n" formatted_ran)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
13
ocaml/lib/system/process/command.ml
Normal file
13
ocaml/lib/system/process/command.ml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
open Utilities.Aliases
|
||||
|
||||
type status = Exit of int | Unevaluated
|
||||
type command = { name: string; arguments: string list; status: status }
|
||||
|
||||
let format (command: command): string =
|
||||
command.name ^
|
||||
" with arguments: " ^ (String.concat " " command.arguments) ^
|
||||
" and result " ^ match command.status with
|
||||
| Exit n -> str_int n
|
||||
| Unevaluated -> "Not evaluated"
|
||||
|
||||
let format_many (commands: command list): string list = List.map format commands
|
||||
|
|
@ -1,11 +1,9 @@
|
|||
open Utilities.Aliases
|
||||
|
||||
let run (command: string) (arguments: string list) =
|
||||
let run (command: Command.command): Command.command =
|
||||
match Unix.fork () with
|
||||
| 0 -> Unix.execvp command (Array.of_list arguments)
|
||||
| 0 -> Unix.execvp command.name (Array.of_list command.arguments)
|
||||
| pid -> let (_, status) = Unix.waitpid [] pid in
|
||||
match status with
|
||||
| Unix.WEXITED 0 -> ()
|
||||
| Unix.WEXITED n -> print ("Process exited with code " ^ str_int n)
|
||||
| Unix.WSIGNALED n -> print ("Process terminated by signal " ^ str_int n)
|
||||
| Unix.WSTOPPED n -> print ("Process stopped by signal " ^ str_int n)
|
||||
| WSTOPPED n | WSIGNALED n | WEXITED n -> { command with status = Exit n }
|
||||
|
||||
let run_many (commands: Command.command list): Command.command list =
|
||||
List.map run commands
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue