Simplify (and fix) direnv watch parsing

This commit is contained in:
Bryan Bennett 2024-08-16 07:38:29 -04:00
parent a26d9bbb17
commit 2b79807fce
No known key found for this signature in database
GPG key ID: EE149E4215408DE9

View file

@ -15,22 +15,16 @@ type watches = array(watch);
let get = () => {
let direnv_watch_str = Sys.getenv("DIRENV_WATCHES");
let (read_p, write_p) = Unix.pipe();
let pid =
Unix.create_process(
"direnv",
[|"direnv", "show_dump", direnv_watch_str|],
Unix.stdin,
write_p,
Unix.stderr,
);
let sub_stdout = Unix.in_channel_of_descr(read_p);
switch (Unix.waitpid([], pid)) {
| (reported_pid, WEXITED(0)) when reported_pid == pid =>
Ok(watches_of_yojson(Yojson.Safe.from_channel(sub_stdout)))
| _ => Error("Failed to parse watches")
let stdout_read_ch =
Unix.open_process_in("direnv show_dump " ++ direnv_watch_str);
switch (Yojson.Safe.from_channel(stdout_read_ch)) {
| exception (Yojson.Json_error(msg)) => Error(msg)
| safe_t =>
switch (watches_of_yojson(safe_t)) {
| exception _ =>
Error("Failed parsing watches; Has the direnv JSON shape changed?")
| body => Ok(body)
}
};
};