aboutsummaryrefslogtreecommitdiff
path: root/src/ucs.ml
blob: a789ecf070878fca1d20a93a5a53c7d24c9033c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(*
 *    Copyright (C) 2020  Alban Gruin
 *
 *    ucs is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as published
 *    by the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    ucs is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with ucs.  If not, see <http://www.gnu.org/licenses/>.
 *)

open Cmdliner

let tcp =
  (fun s ->
    match int_of_string_opt s with
    | Some p when p >= 0 && p < 65536 -> `Ok p
    | Some p -> `Error (Printf.sprintf "invalid value `%d', expected a number \
                                        between 0 and 65535" p)
    | None -> `Error (Printf.sprintf "invalid value `%s', expected an \
                                      integer" s)),
  Format.pp_print_int

let base_url =
  let doc = "Base URL for this instance" in
  Arg.(required & opt (some string) None & info ["b"; "base"] ~doc)

let celcat_url =
  let doc = "Celcat URL" in
  Arg.(required & opt (some string) None & info ["c"; "celcat"] ~doc)

let port =
  let doc = "TCP port" in
  Arg.(value & opt tcp 8080 & info ["p"; "port"] ~doc)

let socket =
  let doc = "UNIX socket" in
  Arg.(value & opt (some string) None & info ["s"; "socket"] ~doc)

let () =
  let run base_url celcat_url port socket =
    let celcat_uri = Uri.of_string celcat_url and
        mode = match socket with
      | Some s -> `Unix_domain_socket (`File s)
      | None -> `TCP (`Port port) in
    Lwt_main.run (Server.serve base_url celcat_uri mode) in
  let cmd =
    let doc = "micro celcatsanitizer: convert celcat \
               calendar to ICS files, on the fly." and
        exits = Term.default_exits in
    Term.(const run $ base_url $ celcat_url $ port $ socket),
    Term.info "ucs" ~version:"0.1" ~doc ~exits in
  Term.(exit @@ eval cmd)