diff options
author | Daniil Baturin <daniil@baturin.org> | 2024-11-07 18:02:08 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-07 18:02:08 +0000 |
commit | 196fdd7fdf6dcf751b7364c59e34278bfd0193e3 (patch) | |
tree | cfeff0991481c8281e24cf1698b20a76854059a4 /src/vyconf_client_session.ml | |
parent | dd9271b4304c6b1a5a2576821d1b2b8fd3aa6bf5 (diff) | |
parent | 9b90d3cc4da72c13ef4270150e4b547ff03fc813 (diff) | |
download | vyconf-master.tar.gz vyconf-master.zip |
T6718: use the vyconf daemon for validation of set commands
Diffstat (limited to 'src/vyconf_client_session.ml')
-rw-r--r-- | src/vyconf_client_session.ml | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/vyconf_client_session.ml b/src/vyconf_client_session.ml new file mode 100644 index 0000000..70a2a13 --- /dev/null +++ b/src/vyconf_client_session.ml @@ -0,0 +1,64 @@ +open Vyconf_connect.Vyconf_pbt + +type op_t = + | OpSetupSession + | OpExists + | OpTeardownSession + | OpShowConfig + | OpValidate + +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 call_op ?(out_format="plain") ?(config_format="curly") socket token op path = + let config_format = config_format_of_string config_format in + let out_format = output_format_of_string out_format in + let run = + let%lwt client = + Vyconf_client.create ~token:token socket out_format config_format + in + let%lwt result = match op with + | None -> Error "Operation required" |> Lwt.return + | Some o -> + begin + match o with + | OpSetupSession -> + let%lwt resp = Vyconf_client.setup_session client "vyconf_client_session" in + begin + match resp with + | Ok c -> Vyconf_client.get_token c + | Error e -> Error e |> Lwt.return + end + | OpExists -> Vyconf_client.exists client path + | OpTeardownSession -> Vyconf_client.teardown_session client + | OpShowConfig -> Vyconf_client.show_config client path + | OpValidate -> Vyconf_client.validate client path + end + in + Lwt.return result + in + Lwt_main.run run + +let session_init ?(out_format="plain") ?(config_format="curly") socket = + call_op ~out_format:out_format ~config_format:config_format socket None (Some OpSetupSession) [] + +let session_free socket token = + call_op socket (Some token) (Some OpTeardownSession) [] + +let session_validate_path socket token path = + call_op socket (Some token) (Some OpValidate) path + +let session_show_config socket token path = + call_op socket (Some token) (Some OpShowConfig) path + +let session_path_exists socket token path = + call_op socket (Some token) (Some OpExists) path |