logarion/cli/index.ml

60 lines
2.5 KiB
OCaml

open Logarion
let index print title authors locations peers dir =
let fname = Filename.concat dir "index.pck" in
let pck = match Header_pack.of_string @@ File_store.to_string fname with
| Error s -> failwith s
| Ok pck -> let info = Header_pack.{ pck.info with
title = if title <> "" then title else pck.info.title;
people = if authors <> ""
then (String_set.list_of_csv authors) else pck.info.people;
locations = if locations <> ""
then (String_set.list_of_csv locations) else pck.info.locations;
} in
Header_pack.{ info; fields;
texts = of_text_list @@ File_store.fold ~dir
(fun a (t,_) -> of_text a t) [];
peers = if peers <> ""
then (str_list @@ String_set.list_of_csv peers) else pck.peers;
}
| exception (Sys_error _) -> Header_pack.{
info = {
version = version; id = Id.generate (); title;
people = String_set.list_of_csv authors;
locations = String_set.list_of_csv locations };
fields;
texts = of_text_list @@ File_store.fold ~dir
(fun a (t,_) -> of_text a t) [];
peers = str_list @@ String_set.list_of_csv peers;
} in
File_store.file fname (Header_pack.string pck);
let open Header_pack in
let s ss = String.concat "\n\t" ss in
if print then
Printf.printf "Title: %s\nAuthors: %s\nLocations:\n\t%s\nPeers:\n\t%s\n"
pck.info.title (String.concat "," pck.info.people)
(s pck.info.locations) (s (to_str_list pck.peers))
open Cmdliner
let term =
let print = Arg.(value & flag & info ["print"] ~doc:"print info") in
let title= Arg.(value & opt string "" & info ["t"; "title"]
~docv:"string" ~doc:"Title for index") in
let auth = Arg.(value & opt string "" & info ["a"; "authors"]
~docv:"comma-separated names" ~doc:"Index authors") in
let locs = Arg.(value & opt string "" & info ["l"; "locations"]
~docv:"comma-separated URLs" ~doc:"repository URLs") in
let peers= Arg.(value & opt string "" & info ["p"; "peers"]
~docv:"comma-separated URLs" ~doc:"URLs to other known text repositories") in
let dir = Arg.(value & pos 0 string "." & info []
~docv:"directory to index") in
let doc = "Generate an index.pck for texts in a directory" in
Term.(const index $ print $ title $ auth $ locs $ peers $ dir),
Term.info "index" ~doc
~man:[ `S "DESCRIPTION"; `Pre "An index contains:\n
* an info section with: title for the index, the authors, locations (URLs) the texts can be access\n
* listing of texts with: ID, date, title, authors, topics\n
* list of other text repositories (peers)\n\n
MessagePack format. <msgpack.org>" ]