flake_env/lib/flake_env__util.re
Bryan Bennett 5e505b178b
WIP
2024-01-05 06:25:13 -05:00

55 lines
1.8 KiB
ReasonML

open Core;
module Unix = Core_unix;
let run_process = (name, args) => {
/*** Run a process [name] with args [args], returning (exit_code, stdout text) */
let stdout_chan = Unix.open_process_in(name ++ " " ++ (args |> String.concat(~sep=" ")));
let stdout_content = stdout_chan |> In_channel.input_all;
let exit_code = Unix.close_process_in(stdout_chan);
(exit_code, stdout_content)
};
let nix = (args) => run_process("nix", ["--extra-experimental-features", "\"nix-command flakes\" ", ...args]);
let hash_files = (filenames) => {
/*** Hash all entries in [filenames], returning a hex-encoded string of the hash of their contents */
let ctx = Sha1.init();
let () = filenames
|> Array.filter(~f=f => switch (Sys_unix.file_exists(f)) {
| `Yes => true
| _ => false
})
|> Array.iter(~f=(f) => {
f |> In_channel.create |> In_channel.input_all |> Sha1.update_string(ctx);
});
Sha1.finalize(ctx) |> Sha1.to_hex
};
let rec rmrf = (path) => {
switch (Unix.lstat(path).st_kind) {
| exception Unix.Unix_error(_, _, _) => ()
| S_REG => Unix.unlink(path)
| S_LNK => Unix.unlink(path)
| S_DIR => {
Sys_unix.readdir(path) |> Array.iter(~f=name => rmrf(Filename.concat(path, name)));
Unix.rmdir(path)
}
| S_CHR => Printf.eprintf("Don't know how to handle Character Device file\n")
| S_BLK => Printf.eprintf("Don't know how to handle Block Device file\n")
| S_FIFO => Printf.eprintf("Don't know how to handle FIFO file\n")
| S_SOCK => Printf.eprintf("Don't know how to handle Socket file\n")
}
};
let get_args = (argv) => {
switch (Array.length(argv)) {
| x when x >= 3 => {
let layout_directory = argv[1];
let flake_specifier = argv[2];
let other_args = snd(List.split_n(List.of_array(argv), 3));
Ok((layout_directory, flake_specifier, other_args))
}
| _ => Error()
}
};