blob: 0d1535e8c4ed196b2aba7716be280bbc35df6cc5 (
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
open Vyconfd_client.Vyconf_client
open Vyconf_connect.Vyconf_pbt
type op_t =
| OpSet
| OpDelete
| OpDiscard
| OpShowConfig
| OpSessionChanged
let op_of_string s =
match s with
| "vy_set" -> OpSet
| "vy_delete" -> OpDelete
| "vy_discard" -> OpDiscard
| "vy_show" -> OpShowConfig
| "vy_session_changed" -> OpSessionChanged
| _ -> failwith (Printf.sprintf "Unknown operation %s" s)
let config_format_of_string s =
match s with
| "curly" -> Curly
| "json" -> Json
| _ -> failwith (Printf.sprintf "Unknown config format %s, should be curly or json" s)
let output_format_of_string s =
match s with
| "plain" -> Out_plain
| "json" -> Out_json
| _ -> failwith (Printf.sprintf "Unknown output format %s, should be plain or json" s)
let in_cli_config_session () =
let env = Unix.environment () in
let res = Array.find_opt (fun c -> String.starts_with ~prefix:"_OFR_CONFIGURE" c) env
in
match res with
| Some _ -> true
| None -> false
let get_session () =
let pid = Int32.of_int (Unix.getppid()) in
let socket = "/var/run/vyconfd.sock" in
let config_format = config_format_of_string "curly" in
let out_format = output_format_of_string "plain" in
let%lwt client =
create socket out_format config_format
in
let%lwt resp = session_of_pid client pid in
match resp with
| Error _ -> setup_session client "vyconf_cli" pid
| _ as c -> c |> Lwt.return
let close_session () =
let%lwt client = get_session () in
match client with
| Ok c ->
teardown_session c
| Error e -> Error e |> Lwt.return
let main op path =
let%lwt client = get_session () in
let%lwt result =
match client with
| Ok c ->
begin
match op with
| OpSet -> set c path
| OpDelete -> delete c path
| OpDiscard -> discard c
| OpShowConfig -> show_config c path
| OpSessionChanged -> session_changed c
end
| Error e -> Error e |> Lwt.return
in
let () =
if not (in_cli_config_session ()) then
close_session () |> Lwt.ignore_result
in
match result with
| Ok s -> let%lwt () = Lwt_io.write Lwt_io.stdout s in Lwt.return 0
| Error e -> let%lwt () = Lwt_io.write Lwt_io.stderr (Printf.sprintf "%s\n" e) in Lwt.return 1
let () =
let path_list = Array.to_list (Array.sub Sys.argv 1 (Array.length Sys.argv - 1))
in
let op_str = FilePath.basename Sys.argv.(0) in
let op = op_of_string op_str in
let result = Lwt_main.run (main op path_list) in exit result
|