about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <git@lukasepple.de>2020-04-30 18:51:39 +0200
committersternenseemann <git@lukasepple.de>2020-04-30 18:51:39 +0200
commit6c9228ccc8de0d758211ebbeeca1a77fdcc2d811 (patch)
treefc8d599bc51978537cd6132195bf401ad5a7b9a6
parentc6aa7e49b330153d52c4a62c37d6b6597f87d093 (diff)
Read from stdin if no file is provided
-rw-r--r--src/logbook.ml13
1 files changed, 7 insertions, 6 deletions
diff --git a/src/logbook.ml b/src/logbook.ml
index a57c427..55c0610 100644
--- a/src/logbook.ml
+++ b/src/logbook.ml
@@ -3,9 +3,12 @@ open Lwt.Infix
 open Cow
 
 let parse_file f =
-  Lwt_io.with_file ~mode:Lwt_io.Input f (fun c ->
+  let file_parser c =
     Lwt_io.read c >>= (fun s ->
-      return (Angstrom.parse_string ~consume:All Log.log_parser s)))
+      return (Angstrom.parse_string ~consume:All Log.log_parser s))
+  in match f with
+     | None -> file_parser Lwt_io.stdin
+     | Some filename -> Lwt_io.with_file ~mode:Lwt_io.Input filename file_parser
 
 let input_file = ref None
 let privacy = ref Log.Public
@@ -32,11 +35,9 @@ let usage =
 
 let _ =
   Arg.parse arglist (fun _ -> ()) usage;
-  match !input_file with
-  | None -> print_endline "No file supplied"
-  | Some f -> let log = Lwt_main.run (parse_file f >>= fun log ->
+  let log = Lwt_main.run (parse_file !input_file >>= fun log ->
       match log with
-      | Result.Error msg -> failwith msg
+      | Result.Error msg -> failwith ("Parse error (" ^ msg ^ ")")
       | Result.Ok log -> return log)
   in
   let log_markup =