summaryrefslogtreecommitdiff
path: root/src/vyconf_client.ml
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2024-11-07 18:02:08 +0000
committerGitHub <noreply@github.com>2024-11-07 18:02:08 +0000
commit196fdd7fdf6dcf751b7364c59e34278bfd0193e3 (patch)
treecfeff0991481c8281e24cf1698b20a76854059a4 /src/vyconf_client.ml
parentdd9271b4304c6b1a5a2576821d1b2b8fd3aa6bf5 (diff)
parent9b90d3cc4da72c13ef4270150e4b547ff03fc813 (diff)
downloadvyconf-196fdd7fdf6dcf751b7364c59e34278bfd0193e3.tar.gz
vyconf-196fdd7fdf6dcf751b7364c59e34278bfd0193e3.zip
Merge pull request #11 from jestabro/vyconf-minimal
T6718: use the vyconf daemon for validation of set commands
Diffstat (limited to 'src/vyconf_client.ml')
-rw-r--r--src/vyconf_client.ml45
1 files changed, 30 insertions, 15 deletions
diff --git a/src/vyconf_client.ml b/src/vyconf_client.ml
index db7d9c1..94348b2 100644
--- a/src/vyconf_client.ml
+++ b/src/vyconf_client.ml
@@ -1,5 +1,4 @@
-include Vyconf_pb
-include Vyconf_types
+include Vyconf_connect.Vyconf_pbt
type t = {
sock: Lwt_unix.file_descr;
@@ -22,8 +21,8 @@ let create ?(token=None) sockfile out_format conf_format =
let open Lwt_unix in
let sock = socket PF_UNIX SOCK_STREAM 0 in
let%lwt () = connect sock (ADDR_UNIX sockfile) in
- let ic = Lwt_io.of_fd Lwt_io.Input sock in
- let oc = Lwt_io.of_fd Lwt_io.Output sock in
+ let ic = Lwt_io.of_fd ~mode:Lwt_io.Input sock in
+ let oc = Lwt_io.of_fd ~mode:Lwt_io.Output sock in
Lwt.return {
sock=sock; ic=ic; oc=oc;
enc=(Pbrt.Encoder.create ()); closed=false;
@@ -43,11 +42,11 @@ let shutdown client =
let do_request client req =
let enc = Pbrt.Encoder.create () in
- let () = encode_request_envelope {token=client.session; request=req} enc in
+ let () = encode_pb_request_envelope {token=client.session; request=req} enc in
let msg = Pbrt.Encoder.to_bytes enc in
- let%lwt () = Message.write client.oc msg in
- let%lwt resp = Message.read client.ic in
- decode_response (Pbrt.Decoder.of_bytes resp) |> Lwt.return
+ let%lwt () = Vyconf_connect.Message.write client.oc msg in
+ let%lwt resp = Vyconf_connect.Message.read client.ic in
+ decode_pb_response (Pbrt.Decoder.of_bytes resp) |> Lwt.return
let get_status client =
let req = Status in
@@ -55,7 +54,7 @@ let get_status client =
Lwt.return resp
let setup_session ?(on_behalf_of=None) client client_app =
- if BatOption.is_some client.session then Lwt.return (Error "Client is already associated with a session") else
+ if Option.is_some client.session then Lwt.return (Error "Client is already associated with a session") else
let id = on_behalf_of |> (function None -> None | Some x -> (Some (Int32.of_int x))) in
let req = Setup_session {client_application=(Some client_app); on_behalf_of=id} in
let%lwt resp = do_request client req in
@@ -64,7 +63,16 @@ let setup_session ?(on_behalf_of=None) client client_app =
(match resp.output with
| Some token -> Ok {client with session=(Some token)}
| None -> Error "setup_session did not return a session token!") |> Lwt.return
- | _ -> Error (BatOption.default "Unknown error" resp.error) |> Lwt.return
+ | _ -> Error (Option.value resp.error ~default:"Unknown error") |> Lwt.return
+
+let teardown_session ?(on_behalf_of=None) client =
+ let id = on_behalf_of |> (function None -> None | Some x -> (Some (Int32.of_int x))) in
+ let req = Teardown {on_behalf_of=id} in
+ let%lwt resp = do_request client req in
+ match resp.status with
+ | Success -> Ok "" |> Lwt.return
+ | Fail -> Error (Option.value resp.error ~default:"") |> Lwt.return
+ | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return
let exists client path =
let req = Exists {path=path} in
@@ -72,33 +80,40 @@ let exists client path =
match resp.status with
| Success -> Lwt.return (Ok "")
| Fail -> Lwt.return (Error "")
- | _ -> Error (BatOption.default "" resp.error) |> Lwt.return
+ | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return
let get_value client path =
let req = Get_value {path=path; output_format=(Some client.out_format)} in
let%lwt resp = do_request client req in
match resp.status with
| Success -> unwrap resp.output |> Lwt.return
- | _ -> Error (BatOption.default ""resp.error) |> Lwt.return
+ | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return
let get_values client path =
let req = Get_values {path=path; output_format=(Some client.out_format)} in
let%lwt resp = do_request client req in
match resp.status with
| Success -> unwrap resp.output |> Lwt.return
- | _ -> Error (BatOption.default "" resp.error) |> Lwt.return
+ | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return
let list_children client path =
let req = List_children {path=path; output_format=(Some client.out_format)} in
let%lwt resp = do_request client req in
match resp.status with
| Success -> unwrap resp.output |> Lwt.return
- | _ -> Error (BatOption.default "" resp.error) |> Lwt.return
+ | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return
let show_config client path =
let req = Show_config {path=path; format=(Some client.conf_format)} in
let%lwt resp = do_request client req in
match resp.status with
| Success -> unwrap resp.output |> Lwt.return
- | _ -> Error (BatOption.default "" resp.error) |> Lwt.return
+ | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return
+let validate client path =
+ let req = Validate {path=path; output_format=(Some client.out_format)} in
+ let%lwt resp = do_request client req in
+ match resp.status with
+ | Success -> Lwt.return (Ok "")
+ | Fail -> Error (Option.value resp.error ~default:"") |> Lwt.return
+ | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return