blob: eb1413660d20538c9ae9eb4b7d43c831f43add52 (
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
open Vyconfd_client.Vyconf_client
open Vyconf_connect.Vyconf_pbt
type op_t =
| OpSetEditLevel
| OpSetEditLevelUp
| OpResetEditLevel
| OpGetEditLevel
| OpEditLevelRoot
| OpShowConfig
| OpSessionChanged
| OpConfigUnsaved
| OpReferencePathExists
| OpGetPathType
| OpGetCompletionEnv
let op_of_arg s =
match s with
| "getEditEnv" -> OpSetEditLevel
| "getEditUpEnv" -> OpSetEditLevelUp
| "getEditResetEnv" -> OpResetEditLevel
| "getEditLevelStr" -> OpGetEditLevel
| "editLevelAtRoot" -> OpEditLevelRoot
| "showCfg" -> OpShowConfig
| "sessionChanged" -> OpSessionChanged
| "sessionUnsaved" -> OpConfigUnsaved
| "validateTmplPath" -> OpReferencePathExists
| "getNodeType" -> OpGetPathType
| "getCompletionEnv" -> OpGetCompletionEnv
| _ -> failwith (Printf.sprintf "Unknown operation %s" 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 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 get_session () =
let pid =
try (* set for config session *)
Int32.of_string (Sys.getenv "SESSION_PID")
with Not_found ->
Int32.of_int (Unix.getppid())
in
let user =
try Sys.getenv "USER"
with Not_found -> ""
in
let sudo_user =
try Sys.getenv "SUDO_USER"
with Not_found -> ""
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_compat" sudo_user user 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
| OpSetEditLevel -> set_edit_level c path
| OpSetEditLevelUp -> set_edit_level_up c
| OpResetEditLevel -> reset_edit_level c
| OpGetEditLevel -> get_edit_level c
| OpEditLevelRoot -> edit_level_root c
| OpShowConfig -> show_config c path
| OpSessionChanged -> session_changed c
| OpConfigUnsaved -> config_unsaved c None
| OpReferencePathExists -> reference_path_exists c path
| OpGetPathType -> get_path_type c path
| OpGetCompletionEnv -> get_completion_env c path
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 ->
begin
match s with
| "" -> Lwt.return 0
| _ ->
let%lwt () =
Lwt_io.write Lwt_io.stdout (Printf.sprintf "%s\n" s) in Lwt.return 0
end
| Error e ->
begin
match e with
| "" -> Lwt.return 1
| _ ->
let%lwt () =
Lwt_io.write Lwt_io.stderr (Printf.sprintf "%s\n" e) in Lwt.return 1
end
let () =
if (Array.length Sys.argv) < 2 then
let () = print_endline "Must specify operation" in exit 1
else
let path_list = Array.to_list (Array.sub Sys.argv 2 (Array.length Sys.argv - 2))
in
let op =
try
op_of_arg Sys.argv.(1)
with Failure msg -> let () = print_endline msg in exit 1
in
match op, path_list with
| OpSetEditLevel, []
| OpReferencePathExists, []
| OpGetPathType, [] ->
let () = print_endline "Must specify config path" in exit 1
| OpGetCompletionEnv, [] | OpGetCompletionEnv, [_] ->
let () = print_endline "Must specify command and at least one component" in exit 1
| _, _ ->
let result = Lwt_main.run (main op path_list) in exit result
|