OCaml: Handle some edge cases; refactor parser, main.ml; add config fetcher

This commit is contained in:
Juno Takano 2025-05-09 11:19:27 -03:00
commit cb56da1462
16 changed files with 229 additions and 105 deletions

View file

@ -1,40 +1,30 @@
let interpret (past : Schema.schema) (input : string list) : Schema.schema =
let present : Schema.schema =
{ past with output = { past.output with main = "" } }
in
let interpret (past : Schema.schema) (arguments : string list) : Schema.schema =
let say (message : string) : Schema.schema =
{ present with output = { present.output with main = message } }
{ past with output = { past.output with main = message } }
in
let configured_present = System.Process.Command.check_su_command present in
(* poor legibility, but otherwise flagged as non-exhaustive *)
match configured_present.meta.status with
| n when n <> 0 -> configured_present
| _ ->
(*
TODO: return a schema with orders, instead of calling side-effects
directly, making this more of a parser and less of a glorified switch
*)
match input with
match arguments with
| "pkg" :: tail -> System.Package.merge past tail
| "os" :: _ -> say System.Os.identify
| "user" :: _ -> say (System.Process.Reader.read [||] "whoami").output
| "echo" :: tail -> say (String.concat " " tail)
| ("version" | "-v" | "--version") :: _ ->
say (Schema.format_version present.meta.version)
| ("help" | "-h" | "--help") :: _ -> say present.meta.help.long
say (Schema.format_version past.meta.version)
| ("help" | "-h" | "--help") :: _ -> say past.meta.help.long
| head :: _ ->
{
present with
past with
output =
{
present.output with
past.output with
main =
"Unknown command: " ^ head ^ "\n" ^ present.meta.help.short;
"Unknown command: " ^ head ^ "\n" ^ past.meta.help.short;
};
meta = { present.meta with status = 1 };
meta = { past.meta with status = 1 };
}
| _ -> present
| _ -> past