From 2b79807fcee6f7f801e403745c1d182098dc70c3 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Fri, 16 Aug 2024 07:38:29 -0400 Subject: [PATCH] Simplify (and fix) direnv watch parsing --- lib/flake_env__watches.re | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/lib/flake_env__watches.re b/lib/flake_env__watches.re index cabfdfc..a707dd8 100644 --- a/lib/flake_env__watches.re +++ b/lib/flake_env__watches.re @@ -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) + } }; };