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 get = () => {
let direnv_watch_str = Sys.getenv("DIRENV_WATCHES"); let direnv_watch_str = Sys.getenv("DIRENV_WATCHES");
let stdout_read_ch =
let (read_p, write_p) = Unix.pipe(); Unix.open_process_in("direnv show_dump " ++ direnv_watch_str);
let pid = switch (Yojson.Safe.from_channel(stdout_read_ch)) {
Unix.create_process( | exception (Yojson.Json_error(msg)) => Error(msg)
"direnv", | safe_t =>
[|"direnv", "show_dump", direnv_watch_str|], switch (watches_of_yojson(safe_t)) {
Unix.stdin, | exception _ =>
write_p, Error("Failed parsing watches; Has the direnv JSON shape changed?")
Unix.stderr, | body => Ok(body)
); }
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")
}; };
}; };